smbd: Fix a typo
[Samba/wip.git] / source3 / smbd / open.c
blob237e90cfd3ecf9df490d1fd87bbce39660ff8bfd
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 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1568 "open entry for mid %llu\n",
1569 (unsigned int)request_time.tv_sec,
1570 (unsigned int)request_time.tv_usec,
1571 (unsigned long long)req->mid));
1573 if (!push_deferred_open_message_smb(req, request_time, timeout,
1574 state->id, (char *)state, sizeof(*state))) {
1575 TALLOC_FREE(lck);
1576 exit_server("push_deferred_open_message_smb failed");
1578 if (lck) {
1579 struct defer_open_state *watch_state;
1580 struct tevent_req *watch_req;
1581 bool ret;
1583 watch_state = talloc(req->sconn, struct defer_open_state);
1584 if (watch_state == NULL) {
1585 exit_server("talloc failed");
1587 watch_state->sconn = req->sconn;
1588 watch_state->mid = req->mid;
1590 DEBUG(10, ("defering mid %llu\n",
1591 (unsigned long long)req->mid));
1593 watch_req = dbwrap_record_watch_send(
1594 watch_state, req->sconn->ev_ctx, lck->data->record,
1595 req->sconn->msg_ctx);
1596 if (watch_req == NULL) {
1597 exit_server("Could not watch share mode record");
1599 tevent_req_set_callback(watch_req, defer_open_done,
1600 watch_state);
1602 ret = tevent_req_set_endtime(
1603 watch_req, req->sconn->ev_ctx,
1604 timeval_sum(&request_time, &timeout));
1605 SMB_ASSERT(ret);
1609 static void defer_open_done(struct tevent_req *req)
1611 struct defer_open_state *state = tevent_req_callback_data(
1612 req, struct defer_open_state);
1613 NTSTATUS status;
1614 bool ret;
1616 status = dbwrap_record_watch_recv(req, talloc_tos(), NULL);
1617 TALLOC_FREE(req);
1618 if (!NT_STATUS_IS_OK(status)) {
1619 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
1620 nt_errstr(status)));
1622 * Even if it failed, retry anyway. TODO: We need a way to
1623 * tell a re-scheduled open about that error.
1627 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state->mid));
1629 ret = schedule_deferred_open_message_smb(state->sconn, state->mid);
1630 SMB_ASSERT(ret);
1631 TALLOC_FREE(state);
1635 /****************************************************************************
1636 On overwrite open ensure that the attributes match.
1637 ****************************************************************************/
1639 static bool open_match_attributes(connection_struct *conn,
1640 uint32 old_dos_attr,
1641 uint32 new_dos_attr,
1642 mode_t existing_unx_mode,
1643 mode_t new_unx_mode,
1644 mode_t *returned_unx_mode)
1646 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1648 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1649 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1651 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1652 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1653 *returned_unx_mode = new_unx_mode;
1654 } else {
1655 *returned_unx_mode = (mode_t)0;
1658 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1659 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1660 "returned_unx_mode = 0%o\n",
1661 (unsigned int)old_dos_attr,
1662 (unsigned int)existing_unx_mode,
1663 (unsigned int)new_dos_attr,
1664 (unsigned int)*returned_unx_mode ));
1666 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1667 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1668 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1669 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1670 return False;
1673 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1674 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1675 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1676 return False;
1679 return True;
1682 /****************************************************************************
1683 Special FCB or DOS processing in the case of a sharing violation.
1684 Try and find a duplicated file handle.
1685 ****************************************************************************/
1687 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1688 connection_struct *conn,
1689 files_struct *fsp_to_dup_into,
1690 const struct smb_filename *smb_fname,
1691 struct file_id id,
1692 uint16 file_pid,
1693 uint64_t vuid,
1694 uint32 access_mask,
1695 uint32 share_access,
1696 uint32 create_options)
1698 files_struct *fsp;
1700 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1701 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1703 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1704 fsp = file_find_di_next(fsp)) {
1706 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1707 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1708 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1709 fsp->fh->fd, (unsigned long long)fsp->vuid,
1710 (unsigned int)fsp->file_pid,
1711 (unsigned int)fsp->fh->private_options,
1712 (unsigned int)fsp->access_mask ));
1714 if (fsp != fsp_to_dup_into &&
1715 fsp->fh->fd != -1 &&
1716 fsp->vuid == vuid &&
1717 fsp->file_pid == file_pid &&
1718 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1719 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1720 (fsp->access_mask & FILE_WRITE_DATA) &&
1721 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1722 strequal(fsp->fsp_name->stream_name,
1723 smb_fname->stream_name)) {
1724 DEBUG(10,("fcb_or_dos_open: file match\n"));
1725 break;
1729 if (!fsp) {
1730 return NT_STATUS_NOT_FOUND;
1733 /* quite an insane set of semantics ... */
1734 if (is_executable(smb_fname->base_name) &&
1735 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1736 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1737 return NT_STATUS_INVALID_PARAMETER;
1740 /* We need to duplicate this fsp. */
1741 return dup_file_fsp(req, fsp, access_mask, share_access,
1742 create_options, fsp_to_dup_into);
1745 static void schedule_defer_open(struct share_mode_lock *lck,
1746 struct file_id id,
1747 struct timeval request_time,
1748 struct smb_request *req)
1750 struct deferred_open_record state;
1752 /* This is a relative time, added to the absolute
1753 request_time value to get the absolute timeout time.
1754 Note that if this is the second or greater time we enter
1755 this codepath for this particular request mid then
1756 request_time is left as the absolute time of the *first*
1757 time this request mid was processed. This is what allows
1758 the request to eventually time out. */
1760 struct timeval timeout;
1762 /* Normally the smbd we asked should respond within
1763 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1764 * the client did, give twice the timeout as a safety
1765 * measure here in case the other smbd is stuck
1766 * somewhere else. */
1768 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1770 /* Nothing actually uses state.delayed_for_oplocks
1771 but it's handy to differentiate in debug messages
1772 between a 30 second delay due to oplock break, and
1773 a 1 second delay for share mode conflicts. */
1775 state.delayed_for_oplocks = True;
1776 state.async_open = false;
1777 state.id = id;
1779 if (!request_timed_out(request_time, timeout)) {
1780 defer_open(lck, request_time, timeout, req, &state);
1784 /****************************************************************************
1785 Reschedule an open call that went asynchronous.
1786 ****************************************************************************/
1788 static void schedule_async_open(struct timeval request_time,
1789 struct smb_request *req)
1791 struct deferred_open_record state;
1792 struct timeval timeout;
1794 timeout = timeval_set(20, 0);
1796 ZERO_STRUCT(state);
1797 state.delayed_for_oplocks = false;
1798 state.async_open = true;
1800 if (!request_timed_out(request_time, timeout)) {
1801 defer_open(NULL, request_time, timeout, req, &state);
1805 /****************************************************************************
1806 Work out what access_mask to use from what the client sent us.
1807 ****************************************************************************/
1809 static NTSTATUS smbd_calculate_maximum_allowed_access(
1810 connection_struct *conn,
1811 const struct smb_filename *smb_fname,
1812 bool use_privs,
1813 uint32_t *p_access_mask)
1815 struct security_descriptor *sd;
1816 uint32_t access_granted;
1817 NTSTATUS status;
1819 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
1820 *p_access_mask |= FILE_GENERIC_ALL;
1821 return NT_STATUS_OK;
1824 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1825 (SECINFO_OWNER |
1826 SECINFO_GROUP |
1827 SECINFO_DACL),
1828 talloc_tos(), &sd);
1830 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1832 * File did not exist
1834 *p_access_mask = FILE_GENERIC_ALL;
1835 return NT_STATUS_OK;
1837 if (!NT_STATUS_IS_OK(status)) {
1838 DEBUG(10,("Could not get acl on file %s: %s\n",
1839 smb_fname_str_dbg(smb_fname),
1840 nt_errstr(status)));
1841 return NT_STATUS_ACCESS_DENIED;
1845 * If we can access the path to this file, by
1846 * default we have FILE_READ_ATTRIBUTES from the
1847 * containing directory. See the section:
1848 * "Algorithm to Check Access to an Existing File"
1849 * in MS-FSA.pdf.
1851 * se_file_access_check()
1852 * also takes care of owner WRITE_DAC and READ_CONTROL.
1854 status = se_file_access_check(sd,
1855 get_current_nttok(conn),
1856 use_privs,
1857 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1858 &access_granted);
1860 TALLOC_FREE(sd);
1862 if (!NT_STATUS_IS_OK(status)) {
1863 DEBUG(10, ("Access denied on file %s: "
1864 "when calculating maximum access\n",
1865 smb_fname_str_dbg(smb_fname)));
1866 return NT_STATUS_ACCESS_DENIED;
1868 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1870 if (!(access_granted & DELETE_ACCESS)) {
1871 if (can_delete_file_in_directory(conn, smb_fname)) {
1872 *p_access_mask |= DELETE_ACCESS;
1876 return NT_STATUS_OK;
1879 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1880 const struct smb_filename *smb_fname,
1881 bool use_privs,
1882 uint32_t access_mask,
1883 uint32_t *access_mask_out)
1885 NTSTATUS status;
1886 uint32_t orig_access_mask = access_mask;
1887 uint32_t rejected_share_access;
1890 * Convert GENERIC bits to specific bits.
1893 se_map_generic(&access_mask, &file_generic_mapping);
1895 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1896 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1898 status = smbd_calculate_maximum_allowed_access(
1899 conn, smb_fname, use_privs, &access_mask);
1901 if (!NT_STATUS_IS_OK(status)) {
1902 return status;
1905 access_mask &= conn->share_access;
1908 rejected_share_access = access_mask & ~(conn->share_access);
1910 if (rejected_share_access) {
1911 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1912 "file %s: rejected by share access mask[0x%08X] "
1913 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1914 smb_fname_str_dbg(smb_fname),
1915 conn->share_access,
1916 orig_access_mask, access_mask,
1917 rejected_share_access));
1918 return NT_STATUS_ACCESS_DENIED;
1921 *access_mask_out = access_mask;
1922 return NT_STATUS_OK;
1925 /****************************************************************************
1926 Remove the deferred open entry under lock.
1927 ****************************************************************************/
1929 /****************************************************************************
1930 Return true if this is a state pointer to an asynchronous create.
1931 ****************************************************************************/
1933 bool is_deferred_open_async(const void *ptr)
1935 const struct deferred_open_record *state = (const struct deferred_open_record *)ptr;
1937 return state->async_open;
1940 static bool clear_ads(uint32_t create_disposition)
1942 bool ret = false;
1944 switch (create_disposition) {
1945 case FILE_SUPERSEDE:
1946 case FILE_OVERWRITE_IF:
1947 case FILE_OVERWRITE:
1948 ret = true;
1949 break;
1950 default:
1951 break;
1953 return ret;
1956 static int disposition_to_open_flags(uint32_t create_disposition)
1958 int ret = 0;
1961 * Currently we're using FILE_SUPERSEDE as the same as
1962 * FILE_OVERWRITE_IF but they really are
1963 * different. FILE_SUPERSEDE deletes an existing file
1964 * (requiring delete access) then recreates it.
1967 switch (create_disposition) {
1968 case FILE_SUPERSEDE:
1969 case FILE_OVERWRITE_IF:
1971 * If file exists replace/overwrite. If file doesn't
1972 * exist create.
1974 ret = O_CREAT|O_TRUNC;
1975 break;
1977 case FILE_OPEN:
1979 * If file exists open. If file doesn't exist error.
1981 ret = 0;
1982 break;
1984 case FILE_OVERWRITE:
1986 * If file exists overwrite. If file doesn't exist
1987 * error.
1989 ret = O_TRUNC;
1990 break;
1992 case FILE_CREATE:
1994 * If file exists error. If file doesn't exist create.
1996 ret = O_CREAT|O_EXCL;
1997 break;
1999 case FILE_OPEN_IF:
2001 * If file exists open. If file doesn't exist create.
2003 ret = O_CREAT;
2004 break;
2006 return ret;
2009 static int calculate_open_access_flags(uint32_t access_mask,
2010 int oplock_request,
2011 uint32_t private_flags)
2013 bool need_write, need_read;
2016 * Note that we ignore the append flag as append does not
2017 * mean the same thing under DOS and Unix.
2020 need_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA));
2021 if (!need_write) {
2022 return O_RDONLY;
2025 /* DENY_DOS opens are always underlying read-write on the
2026 file handle, no matter what the requested access mask
2027 says. */
2029 need_read =
2030 ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
2031 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
2032 FILE_READ_EA|FILE_EXECUTE));
2034 if (!need_read) {
2035 return O_WRONLY;
2037 return O_RDWR;
2040 /****************************************************************************
2041 Open a file with a share mode. Passed in an already created files_struct *.
2042 ****************************************************************************/
2044 static NTSTATUS open_file_ntcreate(connection_struct *conn,
2045 struct smb_request *req,
2046 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
2047 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
2048 uint32 create_disposition, /* FILE_OPEN_IF etc. */
2049 uint32 create_options, /* options such as delete on close. */
2050 uint32 new_dos_attributes, /* attributes used for new file. */
2051 int oplock_request, /* internal Samba oplock codes. */
2052 /* Information (FILE_EXISTS etc.) */
2053 uint32_t private_flags, /* Samba specific flags. */
2054 int *pinfo,
2055 files_struct *fsp)
2057 struct smb_filename *smb_fname = fsp->fsp_name;
2058 int flags=0;
2059 int flags2=0;
2060 bool file_existed = VALID_STAT(smb_fname->st);
2061 bool def_acl = False;
2062 bool posix_open = False;
2063 bool new_file_created = False;
2064 bool first_open_attempt = true;
2065 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
2066 mode_t new_unx_mode = (mode_t)0;
2067 mode_t unx_mode = (mode_t)0;
2068 int info;
2069 uint32 existing_dos_attributes = 0;
2070 struct timeval request_time = timeval_zero();
2071 struct share_mode_lock *lck = NULL;
2072 uint32 open_access_mask = access_mask;
2073 NTSTATUS status;
2074 char *parent_dir;
2075 SMB_STRUCT_STAT saved_stat = smb_fname->st;
2076 struct timespec old_write_time;
2077 struct file_id id;
2079 if (conn->printer) {
2081 * Printers are handled completely differently.
2082 * Most of the passed parameters are ignored.
2085 if (pinfo) {
2086 *pinfo = FILE_WAS_CREATED;
2089 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
2090 smb_fname_str_dbg(smb_fname)));
2092 if (!req) {
2093 DEBUG(0,("open_file_ntcreate: printer open without "
2094 "an SMB request!\n"));
2095 return NT_STATUS_INTERNAL_ERROR;
2098 return print_spool_open(fsp, smb_fname->base_name,
2099 req->vuid);
2102 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
2103 NULL)) {
2104 return NT_STATUS_NO_MEMORY;
2107 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2108 posix_open = True;
2109 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2110 new_dos_attributes = 0;
2111 } else {
2112 /* Windows allows a new file to be created and
2113 silently removes a FILE_ATTRIBUTE_DIRECTORY
2114 sent by the client. Do the same. */
2116 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
2118 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2119 * created new. */
2120 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2121 smb_fname, parent_dir);
2124 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2125 "access_mask=0x%x share_access=0x%x "
2126 "create_disposition = 0x%x create_options=0x%x "
2127 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2128 smb_fname_str_dbg(smb_fname), new_dos_attributes,
2129 access_mask, share_access, create_disposition,
2130 create_options, (unsigned int)unx_mode, oplock_request,
2131 (unsigned int)private_flags));
2133 if (req == NULL) {
2134 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
2135 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) != 0));
2136 } else {
2137 /* And req != NULL means no INTERNAL_OPEN_ONLY */
2138 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) == 0));
2142 * Only non-internal opens can be deferred at all
2145 if (req) {
2146 void *ptr;
2147 if (get_deferred_open_message_state(req,
2148 &request_time,
2149 &ptr)) {
2150 /* Remember the absolute time of the original
2151 request with this mid. We'll use it later to
2152 see if this has timed out. */
2154 /* If it was an async create retry, the file
2155 didn't exist. */
2157 if (is_deferred_open_async(ptr)) {
2158 SET_STAT_INVALID(smb_fname->st);
2159 file_existed = false;
2162 /* Ensure we don't reprocess this message. */
2163 remove_deferred_open_message_smb(req->sconn, req->mid);
2165 first_open_attempt = false;
2169 if (!posix_open) {
2170 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
2171 if (file_existed) {
2172 existing_dos_attributes = dos_mode(conn, smb_fname);
2176 /* ignore any oplock requests if oplocks are disabled */
2177 if (!lp_oplocks(SNUM(conn)) ||
2178 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
2179 /* Mask off everything except the private Samba bits. */
2180 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2183 /* this is for OS/2 long file names - say we don't support them */
2184 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
2185 /* OS/2 Workplace shell fix may be main code stream in a later
2186 * release. */
2187 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2188 "supported.\n"));
2189 if (use_nt_status()) {
2190 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2192 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2195 switch( create_disposition ) {
2196 case FILE_OPEN:
2197 /* If file exists open. If file doesn't exist error. */
2198 if (!file_existed) {
2199 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2200 "requested for file %s and file "
2201 "doesn't exist.\n",
2202 smb_fname_str_dbg(smb_fname)));
2203 errno = ENOENT;
2204 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2206 break;
2208 case FILE_OVERWRITE:
2209 /* If file exists overwrite. If file doesn't exist
2210 * error. */
2211 if (!file_existed) {
2212 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2213 "requested for file %s and file "
2214 "doesn't exist.\n",
2215 smb_fname_str_dbg(smb_fname) ));
2216 errno = ENOENT;
2217 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2219 break;
2221 case FILE_CREATE:
2222 /* If file exists error. If file doesn't exist
2223 * create. */
2224 if (file_existed) {
2225 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2226 "requested for file %s and file "
2227 "already exists.\n",
2228 smb_fname_str_dbg(smb_fname)));
2229 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2230 errno = EISDIR;
2231 } else {
2232 errno = EEXIST;
2234 return map_nt_error_from_unix(errno);
2236 break;
2238 case FILE_SUPERSEDE:
2239 case FILE_OVERWRITE_IF:
2240 case FILE_OPEN_IF:
2241 break;
2242 default:
2243 return NT_STATUS_INVALID_PARAMETER;
2246 flags2 = disposition_to_open_flags(create_disposition);
2248 /* We only care about matching attributes on file exists and
2249 * overwrite. */
2251 if (!posix_open && file_existed &&
2252 ((create_disposition == FILE_OVERWRITE) ||
2253 (create_disposition == FILE_OVERWRITE_IF))) {
2254 if (!open_match_attributes(conn, existing_dos_attributes,
2255 new_dos_attributes,
2256 smb_fname->st.st_ex_mode,
2257 unx_mode, &new_unx_mode)) {
2258 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2259 "for file %s (%x %x) (0%o, 0%o)\n",
2260 smb_fname_str_dbg(smb_fname),
2261 existing_dos_attributes,
2262 new_dos_attributes,
2263 (unsigned int)smb_fname->st.st_ex_mode,
2264 (unsigned int)unx_mode ));
2265 errno = EACCES;
2266 return NT_STATUS_ACCESS_DENIED;
2270 status = smbd_calculate_access_mask(conn, smb_fname,
2271 false,
2272 access_mask,
2273 &access_mask);
2274 if (!NT_STATUS_IS_OK(status)) {
2275 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2276 "on file %s returned %s\n",
2277 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2278 return status;
2281 open_access_mask = access_mask;
2283 if (flags2 & O_TRUNC) {
2284 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2287 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2288 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2289 access_mask));
2292 * Note that we ignore the append flag as append does not
2293 * mean the same thing under DOS and Unix.
2296 flags = calculate_open_access_flags(access_mask, oplock_request,
2297 private_flags);
2300 * Currently we only look at FILE_WRITE_THROUGH for create options.
2303 #if defined(O_SYNC)
2304 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2305 flags2 |= O_SYNC;
2307 #endif /* O_SYNC */
2309 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2310 flags2 |= O_APPEND;
2313 if (!posix_open && !CAN_WRITE(conn)) {
2315 * We should really return a permission denied error if either
2316 * O_CREAT or O_TRUNC are set, but for compatibility with
2317 * older versions of Samba we just AND them out.
2319 flags2 &= ~(O_CREAT|O_TRUNC);
2322 if (first_open_attempt && lp_kernel_oplocks(SNUM(conn))) {
2324 * With kernel oplocks the open breaking an oplock
2325 * blocks until the oplock holder has given up the
2326 * oplock or closed the file. We prevent this by first
2327 * trying to open the file with O_NONBLOCK (see "man
2328 * fcntl" on Linux). For the second try, triggered by
2329 * an oplock break response, we do not need this
2330 * anymore.
2332 * This is true under the assumption that only Samba
2333 * requests kernel oplocks. Once someone else like
2334 * NFSv4 starts to use that API, we will have to
2335 * modify this by communicating with the NFSv4 server.
2337 flags2 |= O_NONBLOCK;
2341 * Ensure we can't write on a read-only share or file.
2344 if (flags != O_RDONLY && file_existed &&
2345 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2346 DEBUG(5,("open_file_ntcreate: write access requested for "
2347 "file %s on read only %s\n",
2348 smb_fname_str_dbg(smb_fname),
2349 !CAN_WRITE(conn) ? "share" : "file" ));
2350 errno = EACCES;
2351 return NT_STATUS_ACCESS_DENIED;
2354 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2355 fsp->share_access = share_access;
2356 fsp->fh->private_options = private_flags;
2357 fsp->access_mask = open_access_mask; /* We change this to the
2358 * requested access_mask after
2359 * the open is done. */
2360 fsp->posix_open = posix_open;
2362 /* Ensure no SAMBA_PRIVATE bits can be set. */
2363 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2365 if (timeval_is_zero(&request_time)) {
2366 request_time = fsp->open_time;
2370 * Ensure we pay attention to default ACLs on directories if required.
2373 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2374 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2375 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2378 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2379 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2380 (unsigned int)flags, (unsigned int)flags2,
2381 (unsigned int)unx_mode, (unsigned int)access_mask,
2382 (unsigned int)open_access_mask));
2384 fsp_open = open_file(fsp, conn, req, parent_dir,
2385 flags|flags2, unx_mode, access_mask,
2386 open_access_mask, &new_file_created);
2388 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_NETWORK_BUSY)) {
2389 struct deferred_open_record state;
2392 * EWOULDBLOCK/EAGAIN maps to NETWORK_BUSY.
2394 if (file_existed && S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
2395 DEBUG(10, ("FIFO busy\n"));
2396 return NT_STATUS_NETWORK_BUSY;
2398 if (req == NULL) {
2399 DEBUG(10, ("Internal open busy\n"));
2400 return NT_STATUS_NETWORK_BUSY;
2404 * From here on we assume this is an oplock break triggered
2407 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
2408 if (lck == NULL) {
2409 state.delayed_for_oplocks = false;
2410 state.async_open = false;
2411 state.id = fsp->file_id;
2412 defer_open(NULL, request_time, timeval_set(0, 0),
2413 req, &state);
2414 DEBUG(10, ("No share mode lock found after "
2415 "EWOULDBLOCK, retrying sync\n"));
2416 return NT_STATUS_SHARING_VIOLATION;
2419 if (!validate_oplock_types(lck)) {
2420 smb_panic("validate_oplock_types failed");
2423 if (delay_for_oplock(fsp, 0, lck, false, create_disposition)) {
2424 schedule_defer_open(lck, fsp->file_id, request_time, req);
2425 TALLOC_FREE(lck);
2426 DEBUG(10, ("Sent oplock break request to kernel "
2427 "oplock holder\n"));
2428 return NT_STATUS_SHARING_VIOLATION;
2432 * No oplock from Samba around. Immediately retry with
2433 * a blocking open.
2435 state.delayed_for_oplocks = false;
2436 state.async_open = false;
2437 state.id = fsp->file_id;
2438 defer_open(lck, request_time, timeval_set(0, 0), req, &state);
2439 TALLOC_FREE(lck);
2440 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2441 "Retrying sync\n"));
2442 return NT_STATUS_SHARING_VIOLATION;
2445 if (!NT_STATUS_IS_OK(fsp_open)) {
2446 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2447 schedule_async_open(request_time, req);
2449 return fsp_open;
2452 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2454 * The file did exist, but some other (local or NFS)
2455 * process either renamed/unlinked and re-created the
2456 * file with different dev/ino after we walked the path,
2457 * but before we did the open. We could retry the
2458 * open but it's a rare enough case it's easier to
2459 * just fail the open to prevent creating any problems
2460 * in the open file db having the wrong dev/ino key.
2462 fd_close(fsp);
2463 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2464 "Old (dev=0x%llu, ino =0x%llu). "
2465 "New (dev=0x%llu, ino=0x%llu). Failing open "
2466 " with NT_STATUS_ACCESS_DENIED.\n",
2467 smb_fname_str_dbg(smb_fname),
2468 (unsigned long long)saved_stat.st_ex_dev,
2469 (unsigned long long)saved_stat.st_ex_ino,
2470 (unsigned long long)smb_fname->st.st_ex_dev,
2471 (unsigned long long)smb_fname->st.st_ex_ino));
2472 return NT_STATUS_ACCESS_DENIED;
2475 old_write_time = smb_fname->st.st_ex_mtime;
2478 * Deal with the race condition where two smbd's detect the
2479 * file doesn't exist and do the create at the same time. One
2480 * of them will win and set a share mode, the other (ie. this
2481 * one) should check if the requested share mode for this
2482 * create is allowed.
2486 * Now the file exists and fsp is successfully opened,
2487 * fsp->dev and fsp->inode are valid and should replace the
2488 * dev=0,inode=0 from a non existent file. Spotted by
2489 * Nadav Danieli <nadavd@exanet.com>. JRA.
2492 id = fsp->file_id;
2494 lck = get_share_mode_lock(talloc_tos(), id,
2495 conn->connectpath,
2496 smb_fname, &old_write_time);
2498 if (lck == NULL) {
2499 DEBUG(0, ("open_file_ntcreate: Could not get share "
2500 "mode lock for %s\n",
2501 smb_fname_str_dbg(smb_fname)));
2502 fd_close(fsp);
2503 return NT_STATUS_SHARING_VIOLATION;
2506 /* Get the types we need to examine. */
2507 if (!validate_oplock_types(lck)) {
2508 smb_panic("validate_oplock_types failed");
2511 if (has_delete_on_close(lck, fsp->name_hash)) {
2512 TALLOC_FREE(lck);
2513 fd_close(fsp);
2514 return NT_STATUS_DELETE_PENDING;
2517 status = open_mode_check(conn, lck,
2518 access_mask, share_access);
2520 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION) ||
2521 (lck->data->num_share_modes > 0)) {
2523 * This comes from ancient times out of open_mode_check. I
2524 * have no clue whether this is still necessary. I can't think
2525 * of a case where this would actually matter further down in
2526 * this function. I leave it here for further investigation
2527 * :-)
2529 file_existed = true;
2532 if ((req != NULL) &&
2533 delay_for_oplock(
2534 fsp, oplock_request, lck,
2535 NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION),
2536 create_disposition)) {
2537 schedule_defer_open(lck, fsp->file_id, request_time, req);
2538 TALLOC_FREE(lck);
2539 fd_close(fsp);
2540 return NT_STATUS_SHARING_VIOLATION;
2543 if (!NT_STATUS_IS_OK(status)) {
2544 uint32 can_access_mask;
2545 bool can_access = True;
2547 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2549 /* Check if this can be done with the deny_dos and fcb
2550 * calls. */
2551 if (private_flags &
2552 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2553 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2554 if (req == NULL) {
2555 DEBUG(0, ("DOS open without an SMB "
2556 "request!\n"));
2557 TALLOC_FREE(lck);
2558 fd_close(fsp);
2559 return NT_STATUS_INTERNAL_ERROR;
2562 /* Use the client requested access mask here,
2563 * not the one we open with. */
2564 status = fcb_or_dos_open(req,
2565 conn,
2566 fsp,
2567 smb_fname,
2569 req->smbpid,
2570 req->vuid,
2571 access_mask,
2572 share_access,
2573 create_options);
2575 if (NT_STATUS_IS_OK(status)) {
2576 TALLOC_FREE(lck);
2577 if (pinfo) {
2578 *pinfo = FILE_WAS_OPENED;
2580 return NT_STATUS_OK;
2585 * This next line is a subtlety we need for
2586 * MS-Access. If a file open will fail due to share
2587 * permissions and also for security (access) reasons,
2588 * we need to return the access failed error, not the
2589 * share error. We can't open the file due to kernel
2590 * oplock deadlock (it's possible we failed above on
2591 * the open_mode_check()) so use a userspace check.
2594 if (flags & O_RDWR) {
2595 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2596 } else if (flags & O_WRONLY) {
2597 can_access_mask = FILE_WRITE_DATA;
2598 } else {
2599 can_access_mask = FILE_READ_DATA;
2602 if (((can_access_mask & FILE_WRITE_DATA) &&
2603 !CAN_WRITE(conn)) ||
2604 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2605 smb_fname,
2606 false,
2607 can_access_mask))) {
2608 can_access = False;
2612 * If we're returning a share violation, ensure we
2613 * cope with the braindead 1 second delay (SMB1 only).
2616 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2617 !conn->sconn->using_smb2 &&
2618 lp_defer_sharing_violations()) {
2619 struct timeval timeout;
2620 struct deferred_open_record state;
2621 int timeout_usecs;
2623 /* this is a hack to speed up torture tests
2624 in 'make test' */
2625 timeout_usecs = lp_parm_int(SNUM(conn),
2626 "smbd","sharedelay",
2627 SHARING_VIOLATION_USEC_WAIT);
2629 /* This is a relative time, added to the absolute
2630 request_time value to get the absolute timeout time.
2631 Note that if this is the second or greater time we enter
2632 this codepath for this particular request mid then
2633 request_time is left as the absolute time of the *first*
2634 time this request mid was processed. This is what allows
2635 the request to eventually time out. */
2637 timeout = timeval_set(0, timeout_usecs);
2639 /* Nothing actually uses state.delayed_for_oplocks
2640 but it's handy to differentiate in debug messages
2641 between a 30 second delay due to oplock break, and
2642 a 1 second delay for share mode conflicts. */
2644 state.delayed_for_oplocks = False;
2645 state.async_open = false;
2646 state.id = id;
2648 if ((req != NULL)
2649 && !request_timed_out(request_time,
2650 timeout)) {
2651 defer_open(lck, request_time, timeout,
2652 req, &state);
2656 TALLOC_FREE(lck);
2657 fd_close(fsp);
2658 if (can_access) {
2660 * We have detected a sharing violation here
2661 * so return the correct error code
2663 status = NT_STATUS_SHARING_VIOLATION;
2664 } else {
2665 status = NT_STATUS_ACCESS_DENIED;
2667 return status;
2670 grant_fsp_oplock_type(fsp, lck, oplock_request);
2673 * We have the share entry *locked*.....
2676 /* Delete streams if create_disposition requires it */
2677 if (!new_file_created && clear_ads(create_disposition) &&
2678 !is_ntfs_stream_smb_fname(smb_fname)) {
2679 status = delete_all_streams(conn, smb_fname->base_name);
2680 if (!NT_STATUS_IS_OK(status)) {
2681 TALLOC_FREE(lck);
2682 fd_close(fsp);
2683 return status;
2687 /* note that we ignore failure for the following. It is
2688 basically a hack for NFS, and NFS will never set one of
2689 these only read them. Nobody but Samba can ever set a deny
2690 mode and we have already checked our more authoritative
2691 locking database for permission to set this deny mode. If
2692 the kernel refuses the operations then the kernel is wrong.
2693 note that GPFS supports it as well - jmcd */
2695 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
2696 int ret_flock;
2697 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2698 if(ret_flock == -1 ){
2700 TALLOC_FREE(lck);
2701 fd_close(fsp);
2703 return NT_STATUS_SHARING_VIOLATION;
2708 * At this point onwards, we can guarantee that the share entry
2709 * is locked, whether we created the file or not, and that the
2710 * deny mode is compatible with all current opens.
2714 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2715 * but we don't have to store this - just ignore it on access check.
2717 if (conn->sconn->using_smb2) {
2719 * SMB2 doesn't return it (according to Microsoft tests).
2720 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2721 * File created with access = 0x7 (Read, Write, Delete)
2722 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2724 fsp->access_mask = access_mask;
2725 } else {
2726 /* But SMB1 does. */
2727 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2730 if (file_existed) {
2731 /* stat opens on existing files don't get oplocks. */
2732 if (is_stat_open(open_access_mask)) {
2733 fsp->oplock_type = NO_OPLOCK;
2737 if (new_file_created) {
2738 info = FILE_WAS_CREATED;
2739 } else {
2740 if (flags2 & O_TRUNC) {
2741 info = FILE_WAS_OVERWRITTEN;
2742 } else {
2743 info = FILE_WAS_OPENED;
2747 if (pinfo) {
2748 *pinfo = info;
2752 * Setup the oplock info in both the shared memory and
2753 * file structs.
2756 status = set_file_oplock(fsp);
2757 if (!NT_STATUS_IS_OK(status)) {
2759 * Could not get the kernel oplock
2761 fsp->oplock_type = NO_OPLOCK;
2764 if (!set_share_mode(lck, fsp, get_current_uid(conn),
2765 req ? req->mid : 0,
2766 fsp->oplock_type)) {
2767 TALLOC_FREE(lck);
2768 fd_close(fsp);
2769 return NT_STATUS_NO_MEMORY;
2772 /* Handle strange delete on close create semantics. */
2773 if (create_options & FILE_DELETE_ON_CLOSE) {
2775 status = can_set_delete_on_close(fsp, new_dos_attributes);
2777 if (!NT_STATUS_IS_OK(status)) {
2778 /* Remember to delete the mode we just added. */
2779 del_share_mode(lck, fsp);
2780 TALLOC_FREE(lck);
2781 fd_close(fsp);
2782 return status;
2784 /* Note that here we set the *inital* delete on close flag,
2785 not the regular one. The magic gets handled in close. */
2786 fsp->initial_delete_on_close = True;
2789 if (info != FILE_WAS_OPENED) {
2790 /* Files should be initially set as archive */
2791 if (lp_map_archive(SNUM(conn)) ||
2792 lp_store_dos_attributes(SNUM(conn))) {
2793 if (!posix_open) {
2794 if (file_set_dosmode(conn, smb_fname,
2795 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2796 parent_dir, true) == 0) {
2797 unx_mode = smb_fname->st.st_ex_mode;
2803 /* Determine sparse flag. */
2804 if (posix_open) {
2805 /* POSIX opens are sparse by default. */
2806 fsp->is_sparse = true;
2807 } else {
2808 fsp->is_sparse = (file_existed &&
2809 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2813 * Take care of inherited ACLs on created files - if default ACL not
2814 * selected.
2817 if (!posix_open && new_file_created && !def_acl) {
2819 int saved_errno = errno; /* We might get ENOSYS in the next
2820 * call.. */
2822 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2823 errno == ENOSYS) {
2824 errno = saved_errno; /* Ignore ENOSYS */
2827 } else if (new_unx_mode) {
2829 int ret = -1;
2831 /* Attributes need changing. File already existed. */
2834 int saved_errno = errno; /* We might get ENOSYS in the
2835 * next call.. */
2836 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2838 if (ret == -1 && errno == ENOSYS) {
2839 errno = saved_errno; /* Ignore ENOSYS */
2840 } else {
2841 DEBUG(5, ("open_file_ntcreate: reset "
2842 "attributes of file %s to 0%o\n",
2843 smb_fname_str_dbg(smb_fname),
2844 (unsigned int)new_unx_mode));
2845 ret = 0; /* Don't do the fchmod below. */
2849 if ((ret == -1) &&
2850 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2851 DEBUG(5, ("open_file_ntcreate: failed to reset "
2852 "attributes of file %s to 0%o\n",
2853 smb_fname_str_dbg(smb_fname),
2854 (unsigned int)new_unx_mode));
2859 * Deal with other opens having a modified write time.
2861 struct timespec write_time = get_share_mode_write_time(lck);
2863 if (!null_timespec(write_time)) {
2864 update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
2868 TALLOC_FREE(lck);
2870 return NT_STATUS_OK;
2873 static NTSTATUS mkdir_internal(connection_struct *conn,
2874 struct smb_filename *smb_dname,
2875 uint32 file_attributes)
2877 mode_t mode;
2878 char *parent_dir = NULL;
2879 NTSTATUS status;
2880 bool posix_open = false;
2881 bool need_re_stat = false;
2882 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2884 if (!CAN_WRITE(conn) || (access_mask & ~(conn->share_access))) {
2885 DEBUG(5,("mkdir_internal: failing share access "
2886 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
2887 return NT_STATUS_ACCESS_DENIED;
2890 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2891 NULL)) {
2892 return NT_STATUS_NO_MEMORY;
2895 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2896 posix_open = true;
2897 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2898 } else {
2899 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2902 status = check_parent_access(conn,
2903 smb_dname,
2904 access_mask);
2905 if(!NT_STATUS_IS_OK(status)) {
2906 DEBUG(5,("mkdir_internal: check_parent_access "
2907 "on directory %s for path %s returned %s\n",
2908 parent_dir,
2909 smb_dname->base_name,
2910 nt_errstr(status) ));
2911 return status;
2914 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2915 return map_nt_error_from_unix(errno);
2918 /* Ensure we're checking for a symlink here.... */
2919 /* We don't want to get caught by a symlink racer. */
2921 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2922 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2923 smb_fname_str_dbg(smb_dname), strerror(errno)));
2924 return map_nt_error_from_unix(errno);
2927 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2928 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2929 smb_fname_str_dbg(smb_dname)));
2930 return NT_STATUS_NOT_A_DIRECTORY;
2933 if (lp_store_dos_attributes(SNUM(conn))) {
2934 if (!posix_open) {
2935 file_set_dosmode(conn, smb_dname,
2936 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2937 parent_dir, true);
2941 if (lp_inherit_permissions(SNUM(conn))) {
2942 inherit_access_posix_acl(conn, parent_dir,
2943 smb_dname->base_name, mode);
2944 need_re_stat = true;
2947 if (!posix_open) {
2949 * Check if high bits should have been set,
2950 * then (if bits are missing): add them.
2951 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2952 * dir.
2954 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2955 (mode & ~smb_dname->st.st_ex_mode)) {
2956 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2957 (smb_dname->st.st_ex_mode |
2958 (mode & ~smb_dname->st.st_ex_mode)));
2959 need_re_stat = true;
2963 /* Change the owner if required. */
2964 if (lp_inherit_owner(SNUM(conn))) {
2965 change_dir_owner_to_parent(conn, parent_dir,
2966 smb_dname->base_name,
2967 &smb_dname->st);
2968 need_re_stat = true;
2971 if (need_re_stat) {
2972 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2973 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2974 smb_fname_str_dbg(smb_dname), strerror(errno)));
2975 return map_nt_error_from_unix(errno);
2979 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2980 smb_dname->base_name);
2982 return NT_STATUS_OK;
2985 /****************************************************************************
2986 Open a directory from an NT SMB call.
2987 ****************************************************************************/
2989 static NTSTATUS open_directory(connection_struct *conn,
2990 struct smb_request *req,
2991 struct smb_filename *smb_dname,
2992 uint32 access_mask,
2993 uint32 share_access,
2994 uint32 create_disposition,
2995 uint32 create_options,
2996 uint32 file_attributes,
2997 int *pinfo,
2998 files_struct **result)
3000 files_struct *fsp = NULL;
3001 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
3002 struct share_mode_lock *lck = NULL;
3003 NTSTATUS status;
3004 struct timespec mtimespec;
3005 int info = 0;
3007 if (is_ntfs_stream_smb_fname(smb_dname)) {
3008 DEBUG(2, ("open_directory: %s is a stream name!\n",
3009 smb_fname_str_dbg(smb_dname)));
3010 return NT_STATUS_NOT_A_DIRECTORY;
3013 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
3014 /* Ensure we have a directory attribute. */
3015 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
3018 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
3019 "share_access = 0x%x create_options = 0x%x, "
3020 "create_disposition = 0x%x, file_attributes = 0x%x\n",
3021 smb_fname_str_dbg(smb_dname),
3022 (unsigned int)access_mask,
3023 (unsigned int)share_access,
3024 (unsigned int)create_options,
3025 (unsigned int)create_disposition,
3026 (unsigned int)file_attributes));
3028 status = smbd_calculate_access_mask(conn, smb_dname, false,
3029 access_mask, &access_mask);
3030 if (!NT_STATUS_IS_OK(status)) {
3031 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
3032 "on file %s returned %s\n",
3033 smb_fname_str_dbg(smb_dname),
3034 nt_errstr(status)));
3035 return status;
3038 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3039 !security_token_has_privilege(get_current_nttok(conn),
3040 SEC_PRIV_SECURITY)) {
3041 DEBUG(10, ("open_directory: open on %s "
3042 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3043 smb_fname_str_dbg(smb_dname)));
3044 return NT_STATUS_PRIVILEGE_NOT_HELD;
3047 switch( create_disposition ) {
3048 case FILE_OPEN:
3050 if (!dir_existed) {
3051 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3054 info = FILE_WAS_OPENED;
3055 break;
3057 case FILE_CREATE:
3059 /* If directory exists error. If directory doesn't
3060 * exist create. */
3062 if (dir_existed) {
3063 status = NT_STATUS_OBJECT_NAME_COLLISION;
3064 DEBUG(2, ("open_directory: unable to create "
3065 "%s. Error was %s\n",
3066 smb_fname_str_dbg(smb_dname),
3067 nt_errstr(status)));
3068 return status;
3071 status = mkdir_internal(conn, smb_dname,
3072 file_attributes);
3074 if (!NT_STATUS_IS_OK(status)) {
3075 DEBUG(2, ("open_directory: unable to create "
3076 "%s. Error was %s\n",
3077 smb_fname_str_dbg(smb_dname),
3078 nt_errstr(status)));
3079 return status;
3082 info = FILE_WAS_CREATED;
3083 break;
3085 case FILE_OPEN_IF:
3087 * If directory exists open. If directory doesn't
3088 * exist create.
3091 if (dir_existed) {
3092 status = NT_STATUS_OK;
3093 info = FILE_WAS_OPENED;
3094 } else {
3095 status = mkdir_internal(conn, smb_dname,
3096 file_attributes);
3098 if (NT_STATUS_IS_OK(status)) {
3099 info = FILE_WAS_CREATED;
3100 } else {
3101 /* Cope with create race. */
3102 if (!NT_STATUS_EQUAL(status,
3103 NT_STATUS_OBJECT_NAME_COLLISION)) {
3104 DEBUG(2, ("open_directory: unable to create "
3105 "%s. Error was %s\n",
3106 smb_fname_str_dbg(smb_dname),
3107 nt_errstr(status)));
3108 return status;
3110 info = FILE_WAS_OPENED;
3114 break;
3116 case FILE_SUPERSEDE:
3117 case FILE_OVERWRITE:
3118 case FILE_OVERWRITE_IF:
3119 default:
3120 DEBUG(5,("open_directory: invalid create_disposition "
3121 "0x%x for directory %s\n",
3122 (unsigned int)create_disposition,
3123 smb_fname_str_dbg(smb_dname)));
3124 return NT_STATUS_INVALID_PARAMETER;
3127 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3128 DEBUG(5,("open_directory: %s is not a directory !\n",
3129 smb_fname_str_dbg(smb_dname)));
3130 return NT_STATUS_NOT_A_DIRECTORY;
3133 if (info == FILE_WAS_OPENED) {
3134 status = smbd_check_access_rights(conn,
3135 smb_dname,
3136 false,
3137 access_mask);
3138 if (!NT_STATUS_IS_OK(status)) {
3139 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3140 "file %s failed with %s\n",
3141 smb_fname_str_dbg(smb_dname),
3142 nt_errstr(status)));
3143 return status;
3147 status = file_new(req, conn, &fsp);
3148 if(!NT_STATUS_IS_OK(status)) {
3149 return status;
3153 * Setup the files_struct for it.
3156 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3157 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3158 fsp->file_pid = req ? req->smbpid : 0;
3159 fsp->can_lock = False;
3160 fsp->can_read = False;
3161 fsp->can_write = False;
3163 fsp->share_access = share_access;
3164 fsp->fh->private_options = 0;
3166 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3168 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3169 fsp->print_file = NULL;
3170 fsp->modified = False;
3171 fsp->oplock_type = NO_OPLOCK;
3172 fsp->sent_oplock_break = NO_BREAK_SENT;
3173 fsp->is_directory = True;
3174 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
3175 status = fsp_set_smb_fname(fsp, smb_dname);
3176 if (!NT_STATUS_IS_OK(status)) {
3177 file_free(req, fsp);
3178 return status;
3181 /* Don't store old timestamps for directory
3182 handles in the internal database. We don't
3183 update them in there if new objects
3184 are creaded in the directory. Currently
3185 we only update timestamps on file writes.
3186 See bug #9870.
3188 ZERO_STRUCT(mtimespec);
3190 if (access_mask & (FILE_LIST_DIRECTORY|
3191 FILE_ADD_FILE|
3192 FILE_ADD_SUBDIRECTORY|
3193 FILE_TRAVERSE|
3194 DELETE_ACCESS|
3195 FILE_DELETE_CHILD)) {
3196 #ifdef O_DIRECTORY
3197 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3198 #else
3199 /* POSIX allows us to open a directory with O_RDONLY. */
3200 status = fd_open(conn, fsp, O_RDONLY, 0);
3201 #endif
3202 if (!NT_STATUS_IS_OK(status)) {
3203 DEBUG(5, ("open_directory: Could not open fd for "
3204 "%s (%s)\n",
3205 smb_fname_str_dbg(smb_dname),
3206 nt_errstr(status)));
3207 file_free(req, fsp);
3208 return status;
3210 } else {
3211 fsp->fh->fd = -1;
3212 DEBUG(10, ("Not opening Directory %s\n",
3213 smb_fname_str_dbg(smb_dname)));
3216 status = vfs_stat_fsp(fsp);
3217 if (!NT_STATUS_IS_OK(status)) {
3218 fd_close(fsp);
3219 file_free(req, fsp);
3220 return status;
3223 /* Ensure there was no race condition. */
3224 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
3225 DEBUG(5,("open_directory: stat struct differs for "
3226 "directory %s.\n",
3227 smb_fname_str_dbg(smb_dname)));
3228 fd_close(fsp);
3229 file_free(req, fsp);
3230 return NT_STATUS_ACCESS_DENIED;
3233 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3234 conn->connectpath, smb_dname,
3235 &mtimespec);
3237 if (lck == NULL) {
3238 DEBUG(0, ("open_directory: Could not get share mode lock for "
3239 "%s\n", smb_fname_str_dbg(smb_dname)));
3240 fd_close(fsp);
3241 file_free(req, fsp);
3242 return NT_STATUS_SHARING_VIOLATION;
3245 if (has_delete_on_close(lck, fsp->name_hash)) {
3246 TALLOC_FREE(lck);
3247 fd_close(fsp);
3248 file_free(req, fsp);
3249 return NT_STATUS_DELETE_PENDING;
3252 status = open_mode_check(conn, lck,
3253 access_mask, share_access);
3255 if (!NT_STATUS_IS_OK(status)) {
3256 TALLOC_FREE(lck);
3257 fd_close(fsp);
3258 file_free(req, fsp);
3259 return status;
3262 if (!set_share_mode(lck, fsp, get_current_uid(conn),
3263 req ? req->mid : 0, NO_OPLOCK)) {
3264 TALLOC_FREE(lck);
3265 fd_close(fsp);
3266 file_free(req, fsp);
3267 return NT_STATUS_NO_MEMORY;
3270 /* For directories the delete on close bit at open time seems
3271 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3272 if (create_options & FILE_DELETE_ON_CLOSE) {
3273 status = can_set_delete_on_close(fsp, 0);
3274 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3275 del_share_mode(lck, fsp);
3276 TALLOC_FREE(lck);
3277 fd_close(fsp);
3278 file_free(req, fsp);
3279 return status;
3282 if (NT_STATUS_IS_OK(status)) {
3283 /* Note that here we set the *inital* delete on close flag,
3284 not the regular one. The magic gets handled in close. */
3285 fsp->initial_delete_on_close = True;
3291 * Deal with other opens having a modified write time. Is this
3292 * possible for directories?
3294 struct timespec write_time = get_share_mode_write_time(lck);
3296 if (!null_timespec(write_time)) {
3297 update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
3301 TALLOC_FREE(lck);
3303 if (pinfo) {
3304 *pinfo = info;
3307 *result = fsp;
3308 return NT_STATUS_OK;
3311 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3312 struct smb_filename *smb_dname)
3314 NTSTATUS status;
3315 files_struct *fsp;
3317 status = SMB_VFS_CREATE_FILE(
3318 conn, /* conn */
3319 req, /* req */
3320 0, /* root_dir_fid */
3321 smb_dname, /* fname */
3322 FILE_READ_ATTRIBUTES, /* access_mask */
3323 FILE_SHARE_NONE, /* share_access */
3324 FILE_CREATE, /* create_disposition*/
3325 FILE_DIRECTORY_FILE, /* create_options */
3326 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3327 0, /* oplock_request */
3328 0, /* allocation_size */
3329 0, /* private_flags */
3330 NULL, /* sd */
3331 NULL, /* ea_list */
3332 &fsp, /* result */
3333 NULL); /* pinfo */
3335 if (NT_STATUS_IS_OK(status)) {
3336 close_file(req, fsp, NORMAL_CLOSE);
3339 return status;
3342 /****************************************************************************
3343 Receive notification that one of our open files has been renamed by another
3344 smbd process.
3345 ****************************************************************************/
3347 void msg_file_was_renamed(struct messaging_context *msg,
3348 void *private_data,
3349 uint32_t msg_type,
3350 struct server_id server_id,
3351 DATA_BLOB *data)
3353 files_struct *fsp;
3354 char *frm = (char *)data->data;
3355 struct file_id id;
3356 const char *sharepath;
3357 const char *base_name;
3358 const char *stream_name;
3359 struct smb_filename *smb_fname = NULL;
3360 size_t sp_len, bn_len;
3361 NTSTATUS status;
3362 struct smbd_server_connection *sconn =
3363 talloc_get_type_abort(private_data,
3364 struct smbd_server_connection);
3366 if (data->data == NULL
3367 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3368 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3369 (int)data->length));
3370 return;
3373 /* Unpack the message. */
3374 pull_file_id_24(frm, &id);
3375 sharepath = &frm[24];
3376 sp_len = strlen(sharepath);
3377 base_name = sharepath + sp_len + 1;
3378 bn_len = strlen(base_name);
3379 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3381 /* stream_name must always be NULL if there is no stream. */
3382 if (stream_name[0] == '\0') {
3383 stream_name = NULL;
3386 smb_fname = synthetic_smb_fname(talloc_tos(), base_name,
3387 stream_name, NULL);
3388 if (smb_fname == NULL) {
3389 return;
3392 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3393 "file_id %s\n",
3394 sharepath, smb_fname_str_dbg(smb_fname),
3395 file_id_string_tos(&id)));
3397 for(fsp = file_find_di_first(sconn, id); fsp;
3398 fsp = file_find_di_next(fsp)) {
3399 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3401 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3402 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3403 smb_fname_str_dbg(smb_fname)));
3404 status = fsp_set_smb_fname(fsp, smb_fname);
3405 if (!NT_STATUS_IS_OK(status)) {
3406 goto out;
3408 } else {
3409 /* TODO. JRA. */
3410 /* Now we have the complete path we can work out if this is
3411 actually within this share and adjust newname accordingly. */
3412 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3413 "not sharepath %s) "
3414 "%s from %s -> %s\n",
3415 fsp->conn->connectpath,
3416 sharepath,
3417 fsp_fnum_dbg(fsp),
3418 fsp_str_dbg(fsp),
3419 smb_fname_str_dbg(smb_fname)));
3422 out:
3423 TALLOC_FREE(smb_fname);
3424 return;
3428 * If a main file is opened for delete, all streams need to be checked for
3429 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3430 * If that works, delete them all by setting the delete on close and close.
3433 NTSTATUS open_streams_for_delete(connection_struct *conn,
3434 const char *fname)
3436 struct stream_struct *stream_info = NULL;
3437 files_struct **streams = NULL;
3438 int i;
3439 unsigned int num_streams = 0;
3440 TALLOC_CTX *frame = talloc_stackframe();
3441 NTSTATUS status;
3443 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3444 &num_streams, &stream_info);
3446 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3447 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3448 DEBUG(10, ("no streams around\n"));
3449 TALLOC_FREE(frame);
3450 return NT_STATUS_OK;
3453 if (!NT_STATUS_IS_OK(status)) {
3454 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3455 nt_errstr(status)));
3456 goto fail;
3459 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3460 num_streams));
3462 if (num_streams == 0) {
3463 TALLOC_FREE(frame);
3464 return NT_STATUS_OK;
3467 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3468 if (streams == NULL) {
3469 DEBUG(0, ("talloc failed\n"));
3470 status = NT_STATUS_NO_MEMORY;
3471 goto fail;
3474 for (i=0; i<num_streams; i++) {
3475 struct smb_filename *smb_fname;
3477 if (strequal(stream_info[i].name, "::$DATA")) {
3478 streams[i] = NULL;
3479 continue;
3482 smb_fname = synthetic_smb_fname(
3483 talloc_tos(), fname, stream_info[i].name, NULL);
3484 if (smb_fname == NULL) {
3485 status = NT_STATUS_NO_MEMORY;
3486 goto fail;
3489 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3490 DEBUG(10, ("Unable to stat stream: %s\n",
3491 smb_fname_str_dbg(smb_fname)));
3494 status = SMB_VFS_CREATE_FILE(
3495 conn, /* conn */
3496 NULL, /* req */
3497 0, /* root_dir_fid */
3498 smb_fname, /* fname */
3499 DELETE_ACCESS, /* access_mask */
3500 (FILE_SHARE_READ | /* share_access */
3501 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3502 FILE_OPEN, /* create_disposition*/
3503 0, /* create_options */
3504 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3505 0, /* oplock_request */
3506 0, /* allocation_size */
3507 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3508 NULL, /* sd */
3509 NULL, /* ea_list */
3510 &streams[i], /* result */
3511 NULL); /* pinfo */
3513 if (!NT_STATUS_IS_OK(status)) {
3514 DEBUG(10, ("Could not open stream %s: %s\n",
3515 smb_fname_str_dbg(smb_fname),
3516 nt_errstr(status)));
3518 TALLOC_FREE(smb_fname);
3519 break;
3521 TALLOC_FREE(smb_fname);
3525 * don't touch the variable "status" beyond this point :-)
3528 for (i -= 1 ; i >= 0; i--) {
3529 if (streams[i] == NULL) {
3530 continue;
3533 DEBUG(10, ("Closing stream # %d, %s\n", i,
3534 fsp_str_dbg(streams[i])));
3535 close_file(NULL, streams[i], NORMAL_CLOSE);
3538 fail:
3539 TALLOC_FREE(frame);
3540 return status;
3543 /*********************************************************************
3544 Create a default ACL by inheriting from the parent. If no inheritance
3545 from the parent available, don't set anything. This will leave the actual
3546 permissions the new file or directory already got from the filesystem
3547 as the NT ACL when read.
3548 *********************************************************************/
3550 static NTSTATUS inherit_new_acl(files_struct *fsp)
3552 TALLOC_CTX *frame = talloc_stackframe();
3553 char *parent_name = NULL;
3554 struct security_descriptor *parent_desc = NULL;
3555 NTSTATUS status = NT_STATUS_OK;
3556 struct security_descriptor *psd = NULL;
3557 const struct dom_sid *owner_sid = NULL;
3558 const struct dom_sid *group_sid = NULL;
3559 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3560 struct security_token *token = fsp->conn->session_info->security_token;
3561 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3562 bool inheritable_components = false;
3563 bool try_builtin_administrators = false;
3564 const struct dom_sid *BA_U_sid = NULL;
3565 const struct dom_sid *BA_G_sid = NULL;
3566 bool try_system = false;
3567 const struct dom_sid *SY_U_sid = NULL;
3568 const struct dom_sid *SY_G_sid = NULL;
3569 size_t size = 0;
3571 if (!parent_dirname(frame, fsp->fsp_name->base_name, &parent_name, NULL)) {
3572 TALLOC_FREE(frame);
3573 return NT_STATUS_NO_MEMORY;
3576 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3577 parent_name,
3578 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3579 frame,
3580 &parent_desc);
3581 if (!NT_STATUS_IS_OK(status)) {
3582 TALLOC_FREE(frame);
3583 return status;
3586 inheritable_components = sd_has_inheritable_components(parent_desc,
3587 fsp->is_directory);
3589 if (!inheritable_components && !inherit_owner) {
3590 TALLOC_FREE(frame);
3591 /* Nothing to inherit and not setting owner. */
3592 return NT_STATUS_OK;
3595 /* Create an inherited descriptor from the parent. */
3597 if (DEBUGLEVEL >= 10) {
3598 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3599 fsp_str_dbg(fsp) ));
3600 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3603 /* Inherit from parent descriptor if "inherit owner" set. */
3604 if (inherit_owner) {
3605 owner_sid = parent_desc->owner_sid;
3606 group_sid = parent_desc->group_sid;
3609 if (owner_sid == NULL) {
3610 if (security_token_has_builtin_administrators(token)) {
3611 try_builtin_administrators = true;
3612 } else if (security_token_is_system(token)) {
3613 try_builtin_administrators = true;
3614 try_system = true;
3618 if (group_sid == NULL &&
3619 token->num_sids == PRIMARY_GROUP_SID_INDEX)
3621 if (security_token_is_system(token)) {
3622 try_builtin_administrators = true;
3623 try_system = true;
3627 if (try_builtin_administrators) {
3628 struct unixid ids;
3629 bool ok;
3631 ZERO_STRUCT(ids);
3632 ok = sids_to_unixids(&global_sid_Builtin_Administrators, 1, &ids);
3633 if (ok) {
3634 switch (ids.type) {
3635 case ID_TYPE_BOTH:
3636 BA_U_sid = &global_sid_Builtin_Administrators;
3637 BA_G_sid = &global_sid_Builtin_Administrators;
3638 break;
3639 case ID_TYPE_UID:
3640 BA_U_sid = &global_sid_Builtin_Administrators;
3641 break;
3642 case ID_TYPE_GID:
3643 BA_G_sid = &global_sid_Builtin_Administrators;
3644 break;
3645 default:
3646 break;
3651 if (try_system) {
3652 struct unixid ids;
3653 bool ok;
3655 ZERO_STRUCT(ids);
3656 ok = sids_to_unixids(&global_sid_System, 1, &ids);
3657 if (ok) {
3658 switch (ids.type) {
3659 case ID_TYPE_BOTH:
3660 SY_U_sid = &global_sid_System;
3661 SY_G_sid = &global_sid_System;
3662 break;
3663 case ID_TYPE_UID:
3664 SY_U_sid = &global_sid_System;
3665 break;
3666 case ID_TYPE_GID:
3667 SY_G_sid = &global_sid_System;
3668 break;
3669 default:
3670 break;
3675 if (owner_sid == NULL) {
3676 owner_sid = BA_U_sid;
3679 if (owner_sid == NULL) {
3680 owner_sid = SY_U_sid;
3683 if (group_sid == NULL) {
3684 group_sid = SY_G_sid;
3687 if (try_system && group_sid == NULL) {
3688 group_sid = BA_G_sid;
3691 if (owner_sid == NULL) {
3692 owner_sid = &token->sids[PRIMARY_USER_SID_INDEX];
3694 if (group_sid == NULL) {
3695 if (token->num_sids == PRIMARY_GROUP_SID_INDEX) {
3696 group_sid = &token->sids[PRIMARY_USER_SID_INDEX];
3697 } else {
3698 group_sid = &token->sids[PRIMARY_GROUP_SID_INDEX];
3702 status = se_create_child_secdesc(frame,
3703 &psd,
3704 &size,
3705 parent_desc,
3706 owner_sid,
3707 group_sid,
3708 fsp->is_directory);
3709 if (!NT_STATUS_IS_OK(status)) {
3710 TALLOC_FREE(frame);
3711 return status;
3714 /* If inheritable_components == false,
3715 se_create_child_secdesc()
3716 creates a security desriptor with a NULL dacl
3717 entry, but with SEC_DESC_DACL_PRESENT. We need
3718 to remove that flag. */
3720 if (!inheritable_components) {
3721 security_info_sent &= ~SECINFO_DACL;
3722 psd->type &= ~SEC_DESC_DACL_PRESENT;
3725 if (DEBUGLEVEL >= 10) {
3726 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3727 fsp_str_dbg(fsp) ));
3728 NDR_PRINT_DEBUG(security_descriptor, psd);
3731 if (inherit_owner) {
3732 /* We need to be root to force this. */
3733 become_root();
3735 status = SMB_VFS_FSET_NT_ACL(fsp,
3736 security_info_sent,
3737 psd);
3738 if (inherit_owner) {
3739 unbecome_root();
3741 TALLOC_FREE(frame);
3742 return status;
3746 * Wrapper around open_file_ntcreate and open_directory
3749 static NTSTATUS create_file_unixpath(connection_struct *conn,
3750 struct smb_request *req,
3751 struct smb_filename *smb_fname,
3752 uint32_t access_mask,
3753 uint32_t share_access,
3754 uint32_t create_disposition,
3755 uint32_t create_options,
3756 uint32_t file_attributes,
3757 uint32_t oplock_request,
3758 uint64_t allocation_size,
3759 uint32_t private_flags,
3760 struct security_descriptor *sd,
3761 struct ea_list *ea_list,
3763 files_struct **result,
3764 int *pinfo)
3766 int info = FILE_WAS_OPENED;
3767 files_struct *base_fsp = NULL;
3768 files_struct *fsp = NULL;
3769 NTSTATUS status;
3771 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3772 "file_attributes = 0x%x, share_access = 0x%x, "
3773 "create_disposition = 0x%x create_options = 0x%x "
3774 "oplock_request = 0x%x private_flags = 0x%x "
3775 "ea_list = 0x%p, sd = 0x%p, "
3776 "fname = %s\n",
3777 (unsigned int)access_mask,
3778 (unsigned int)file_attributes,
3779 (unsigned int)share_access,
3780 (unsigned int)create_disposition,
3781 (unsigned int)create_options,
3782 (unsigned int)oplock_request,
3783 (unsigned int)private_flags,
3784 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3786 if (create_options & FILE_OPEN_BY_FILE_ID) {
3787 status = NT_STATUS_NOT_SUPPORTED;
3788 goto fail;
3791 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3792 status = NT_STATUS_INVALID_PARAMETER;
3793 goto fail;
3796 if (req == NULL) {
3797 oplock_request |= INTERNAL_OPEN_ONLY;
3800 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3801 && (access_mask & DELETE_ACCESS)
3802 && !is_ntfs_stream_smb_fname(smb_fname)) {
3804 * We can't open a file with DELETE access if any of the
3805 * streams is open without FILE_SHARE_DELETE
3807 status = open_streams_for_delete(conn, smb_fname->base_name);
3809 if (!NT_STATUS_IS_OK(status)) {
3810 goto fail;
3814 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3815 !security_token_has_privilege(get_current_nttok(conn),
3816 SEC_PRIV_SECURITY)) {
3817 DEBUG(10, ("create_file_unixpath: open on %s "
3818 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3819 smb_fname_str_dbg(smb_fname)));
3820 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3821 goto fail;
3824 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3825 && is_ntfs_stream_smb_fname(smb_fname)
3826 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3827 uint32 base_create_disposition;
3828 struct smb_filename *smb_fname_base = NULL;
3830 if (create_options & FILE_DIRECTORY_FILE) {
3831 status = NT_STATUS_NOT_A_DIRECTORY;
3832 goto fail;
3835 switch (create_disposition) {
3836 case FILE_OPEN:
3837 base_create_disposition = FILE_OPEN;
3838 break;
3839 default:
3840 base_create_disposition = FILE_OPEN_IF;
3841 break;
3844 /* Create an smb_filename with stream_name == NULL. */
3845 smb_fname_base = synthetic_smb_fname(talloc_tos(),
3846 smb_fname->base_name,
3847 NULL, NULL);
3848 if (smb_fname_base == NULL) {
3849 status = NT_STATUS_NO_MEMORY;
3850 goto fail;
3853 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3854 DEBUG(10, ("Unable to stat stream: %s\n",
3855 smb_fname_str_dbg(smb_fname_base)));
3856 } else {
3858 * https://bugzilla.samba.org/show_bug.cgi?id=10229
3859 * We need to check if the requested access mask
3860 * could be used to open the underlying file (if
3861 * it existed), as we're passing in zero for the
3862 * access mask to the base filename.
3864 status = check_base_file_access(conn,
3865 smb_fname_base,
3866 access_mask);
3868 if (!NT_STATUS_IS_OK(status)) {
3869 DEBUG(10, ("Permission check "
3870 "for base %s failed: "
3871 "%s\n", smb_fname->base_name,
3872 nt_errstr(status)));
3873 goto fail;
3877 /* Open the base file. */
3878 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3879 FILE_SHARE_READ
3880 | FILE_SHARE_WRITE
3881 | FILE_SHARE_DELETE,
3882 base_create_disposition,
3883 0, 0, 0, 0, 0, NULL, NULL,
3884 &base_fsp, NULL);
3885 TALLOC_FREE(smb_fname_base);
3887 if (!NT_STATUS_IS_OK(status)) {
3888 DEBUG(10, ("create_file_unixpath for base %s failed: "
3889 "%s\n", smb_fname->base_name,
3890 nt_errstr(status)));
3891 goto fail;
3893 /* we don't need to low level fd */
3894 fd_close(base_fsp);
3898 * If it's a request for a directory open, deal with it separately.
3901 if (create_options & FILE_DIRECTORY_FILE) {
3903 if (create_options & FILE_NON_DIRECTORY_FILE) {
3904 status = NT_STATUS_INVALID_PARAMETER;
3905 goto fail;
3908 /* Can't open a temp directory. IFS kit test. */
3909 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3910 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3911 status = NT_STATUS_INVALID_PARAMETER;
3912 goto fail;
3916 * We will get a create directory here if the Win32
3917 * app specified a security descriptor in the
3918 * CreateDirectory() call.
3921 oplock_request = 0;
3922 status = open_directory(
3923 conn, req, smb_fname, access_mask, share_access,
3924 create_disposition, create_options, file_attributes,
3925 &info, &fsp);
3926 } else {
3929 * Ordinary file case.
3932 status = file_new(req, conn, &fsp);
3933 if(!NT_STATUS_IS_OK(status)) {
3934 goto fail;
3937 status = fsp_set_smb_fname(fsp, smb_fname);
3938 if (!NT_STATUS_IS_OK(status)) {
3939 goto fail;
3942 if (base_fsp) {
3944 * We're opening the stream element of a
3945 * base_fsp we already opened. Set up the
3946 * base_fsp pointer.
3948 fsp->base_fsp = base_fsp;
3951 if (allocation_size) {
3952 fsp->initial_allocation_size = smb_roundup(fsp->conn,
3953 allocation_size);
3956 status = open_file_ntcreate(conn,
3957 req,
3958 access_mask,
3959 share_access,
3960 create_disposition,
3961 create_options,
3962 file_attributes,
3963 oplock_request,
3964 private_flags,
3965 &info,
3966 fsp);
3968 if(!NT_STATUS_IS_OK(status)) {
3969 file_free(req, fsp);
3970 fsp = NULL;
3973 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3975 /* A stream open never opens a directory */
3977 if (base_fsp) {
3978 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3979 goto fail;
3983 * Fail the open if it was explicitly a non-directory
3984 * file.
3987 if (create_options & FILE_NON_DIRECTORY_FILE) {
3988 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3989 goto fail;
3992 oplock_request = 0;
3993 status = open_directory(
3994 conn, req, smb_fname, access_mask,
3995 share_access, create_disposition,
3996 create_options, file_attributes,
3997 &info, &fsp);
4001 if (!NT_STATUS_IS_OK(status)) {
4002 goto fail;
4005 fsp->base_fsp = base_fsp;
4007 if ((ea_list != NULL) &&
4008 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
4009 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
4010 if (!NT_STATUS_IS_OK(status)) {
4011 goto fail;
4015 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
4016 status = NT_STATUS_ACCESS_DENIED;
4017 goto fail;
4020 /* Save the requested allocation size. */
4021 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
4022 if (allocation_size
4023 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
4024 fsp->initial_allocation_size = smb_roundup(
4025 fsp->conn, allocation_size);
4026 if (fsp->is_directory) {
4027 /* Can't set allocation size on a directory. */
4028 status = NT_STATUS_ACCESS_DENIED;
4029 goto fail;
4031 if (vfs_allocate_file_space(
4032 fsp, fsp->initial_allocation_size) == -1) {
4033 status = NT_STATUS_DISK_FULL;
4034 goto fail;
4036 } else {
4037 fsp->initial_allocation_size = smb_roundup(
4038 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
4040 } else {
4041 fsp->initial_allocation_size = 0;
4044 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
4045 fsp->base_fsp == NULL) {
4046 if (sd != NULL) {
4048 * According to the MS documentation, the only time the security
4049 * descriptor is applied to the opened file is iff we *created* the
4050 * file; an existing file stays the same.
4052 * Also, it seems (from observation) that you can open the file with
4053 * any access mask but you can still write the sd. We need to override
4054 * the granted access before we call set_sd
4055 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
4058 uint32_t sec_info_sent;
4059 uint32_t saved_access_mask = fsp->access_mask;
4061 sec_info_sent = get_sec_info(sd);
4063 fsp->access_mask = FILE_GENERIC_ALL;
4065 if (sec_info_sent & (SECINFO_OWNER|
4066 SECINFO_GROUP|
4067 SECINFO_DACL|
4068 SECINFO_SACL)) {
4069 status = set_sd(fsp, sd, sec_info_sent);
4072 fsp->access_mask = saved_access_mask;
4074 if (!NT_STATUS_IS_OK(status)) {
4075 goto fail;
4077 } else if (lp_inherit_acls(SNUM(conn))) {
4078 /* Inherit from parent. Errors here are not fatal. */
4079 status = inherit_new_acl(fsp);
4080 if (!NT_STATUS_IS_OK(status)) {
4081 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
4082 fsp_str_dbg(fsp),
4083 nt_errstr(status) ));
4088 if ((conn->fs_capabilities & FILE_FILE_COMPRESSION)
4089 && (create_options & FILE_NO_COMPRESSION)
4090 && (info == FILE_WAS_CREATED)) {
4091 status = SMB_VFS_SET_COMPRESSION(conn, fsp, fsp,
4092 COMPRESSION_FORMAT_NONE);
4093 if (!NT_STATUS_IS_OK(status)) {
4094 DEBUG(1, ("failed to disable compression: %s\n",
4095 nt_errstr(status)));
4099 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
4101 *result = fsp;
4102 if (pinfo != NULL) {
4103 *pinfo = info;
4106 smb_fname->st = fsp->fsp_name->st;
4108 return NT_STATUS_OK;
4110 fail:
4111 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
4113 if (fsp != NULL) {
4114 if (base_fsp && fsp->base_fsp == base_fsp) {
4116 * The close_file below will close
4117 * fsp->base_fsp.
4119 base_fsp = NULL;
4121 close_file(req, fsp, ERROR_CLOSE);
4122 fsp = NULL;
4124 if (base_fsp != NULL) {
4125 close_file(req, base_fsp, ERROR_CLOSE);
4126 base_fsp = NULL;
4128 return status;
4132 * Calculate the full path name given a relative fid.
4134 NTSTATUS get_relative_fid_filename(connection_struct *conn,
4135 struct smb_request *req,
4136 uint16_t root_dir_fid,
4137 const struct smb_filename *smb_fname,
4138 struct smb_filename **smb_fname_out)
4140 files_struct *dir_fsp;
4141 char *parent_fname = NULL;
4142 char *new_base_name = NULL;
4143 NTSTATUS status;
4145 if (root_dir_fid == 0 || !smb_fname) {
4146 status = NT_STATUS_INTERNAL_ERROR;
4147 goto out;
4150 dir_fsp = file_fsp(req, root_dir_fid);
4152 if (dir_fsp == NULL) {
4153 status = NT_STATUS_INVALID_HANDLE;
4154 goto out;
4157 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
4158 status = NT_STATUS_INVALID_HANDLE;
4159 goto out;
4162 if (!dir_fsp->is_directory) {
4165 * Check to see if this is a mac fork of some kind.
4168 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
4169 is_ntfs_stream_smb_fname(smb_fname)) {
4170 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
4171 goto out;
4175 we need to handle the case when we get a
4176 relative open relative to a file and the
4177 pathname is blank - this is a reopen!
4178 (hint from demyn plantenberg)
4181 status = NT_STATUS_INVALID_HANDLE;
4182 goto out;
4185 if (ISDOT(dir_fsp->fsp_name->base_name)) {
4187 * We're at the toplevel dir, the final file name
4188 * must not contain ./, as this is filtered out
4189 * normally by srvstr_get_path and unix_convert
4190 * explicitly rejects paths containing ./.
4192 parent_fname = talloc_strdup(talloc_tos(), "");
4193 if (parent_fname == NULL) {
4194 status = NT_STATUS_NO_MEMORY;
4195 goto out;
4197 } else {
4198 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
4201 * Copy in the base directory name.
4204 parent_fname = talloc_array(talloc_tos(), char,
4205 dir_name_len+2);
4206 if (parent_fname == NULL) {
4207 status = NT_STATUS_NO_MEMORY;
4208 goto out;
4210 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
4211 dir_name_len+1);
4214 * Ensure it ends in a '/'.
4215 * We used TALLOC_SIZE +2 to add space for the '/'.
4218 if(dir_name_len
4219 && (parent_fname[dir_name_len-1] != '\\')
4220 && (parent_fname[dir_name_len-1] != '/')) {
4221 parent_fname[dir_name_len] = '/';
4222 parent_fname[dir_name_len+1] = '\0';
4226 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
4227 smb_fname->base_name);
4228 if (new_base_name == NULL) {
4229 status = NT_STATUS_NO_MEMORY;
4230 goto out;
4233 status = filename_convert(req,
4234 conn,
4235 req->flags2 & FLAGS2_DFS_PATHNAMES,
4236 new_base_name,
4238 NULL,
4239 smb_fname_out);
4240 if (!NT_STATUS_IS_OK(status)) {
4241 goto out;
4244 out:
4245 TALLOC_FREE(parent_fname);
4246 TALLOC_FREE(new_base_name);
4247 return status;
4250 NTSTATUS create_file_default(connection_struct *conn,
4251 struct smb_request *req,
4252 uint16_t root_dir_fid,
4253 struct smb_filename *smb_fname,
4254 uint32_t access_mask,
4255 uint32_t share_access,
4256 uint32_t create_disposition,
4257 uint32_t create_options,
4258 uint32_t file_attributes,
4259 uint32_t oplock_request,
4260 uint64_t allocation_size,
4261 uint32_t private_flags,
4262 struct security_descriptor *sd,
4263 struct ea_list *ea_list,
4264 files_struct **result,
4265 int *pinfo)
4267 int info = FILE_WAS_OPENED;
4268 files_struct *fsp = NULL;
4269 NTSTATUS status;
4270 bool stream_name = false;
4272 DEBUG(10,("create_file: access_mask = 0x%x "
4273 "file_attributes = 0x%x, share_access = 0x%x, "
4274 "create_disposition = 0x%x create_options = 0x%x "
4275 "oplock_request = 0x%x "
4276 "private_flags = 0x%x "
4277 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4278 "fname = %s\n",
4279 (unsigned int)access_mask,
4280 (unsigned int)file_attributes,
4281 (unsigned int)share_access,
4282 (unsigned int)create_disposition,
4283 (unsigned int)create_options,
4284 (unsigned int)oplock_request,
4285 (unsigned int)private_flags,
4286 (unsigned int)root_dir_fid,
4287 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4290 * Calculate the filename from the root_dir_if if necessary.
4293 if (root_dir_fid != 0) {
4294 struct smb_filename *smb_fname_out = NULL;
4295 status = get_relative_fid_filename(conn, req, root_dir_fid,
4296 smb_fname, &smb_fname_out);
4297 if (!NT_STATUS_IS_OK(status)) {
4298 goto fail;
4300 smb_fname = smb_fname_out;
4304 * Check to see if this is a mac fork of some kind.
4307 stream_name = is_ntfs_stream_smb_fname(smb_fname);
4308 if (stream_name) {
4309 enum FAKE_FILE_TYPE fake_file_type;
4311 fake_file_type = is_fake_file(smb_fname);
4313 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
4316 * Here we go! support for changing the disk quotas
4317 * --metze
4319 * We need to fake up to open this MAGIC QUOTA file
4320 * and return a valid FID.
4322 * w2k close this file directly after openening xp
4323 * also tries a QUERY_FILE_INFO on the file and then
4324 * close it
4326 status = open_fake_file(req, conn, req->vuid,
4327 fake_file_type, smb_fname,
4328 access_mask, &fsp);
4329 if (!NT_STATUS_IS_OK(status)) {
4330 goto fail;
4333 ZERO_STRUCT(smb_fname->st);
4334 goto done;
4337 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
4338 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4339 goto fail;
4343 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
4344 int ret;
4345 smb_fname->stream_name = NULL;
4346 /* We have to handle this error here. */
4347 if (create_options & FILE_DIRECTORY_FILE) {
4348 status = NT_STATUS_NOT_A_DIRECTORY;
4349 goto fail;
4351 if (lp_posix_pathnames()) {
4352 ret = SMB_VFS_LSTAT(conn, smb_fname);
4353 } else {
4354 ret = SMB_VFS_STAT(conn, smb_fname);
4357 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
4358 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4359 goto fail;
4363 status = create_file_unixpath(
4364 conn, req, smb_fname, access_mask, share_access,
4365 create_disposition, create_options, file_attributes,
4366 oplock_request, allocation_size, private_flags,
4367 sd, ea_list,
4368 &fsp, &info);
4370 if (!NT_STATUS_IS_OK(status)) {
4371 goto fail;
4374 done:
4375 DEBUG(10, ("create_file: info=%d\n", info));
4377 *result = fsp;
4378 if (pinfo != NULL) {
4379 *pinfo = info;
4381 return NT_STATUS_OK;
4383 fail:
4384 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
4386 if (fsp != NULL) {
4387 close_file(req, fsp, ERROR_CLOSE);
4388 fsp = NULL;
4390 return status;