smbd: Fix bug 10593
[Samba.git] / source3 / smbd / open.c
blobb913c9c576debd5ca6747e553258f50253ed29ea
1 /*
2 Unix SMB/CIFS implementation.
3 file opening and share modes
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2004
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "printing.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "fake_file.h"
28 #include "../libcli/security/security.h"
29 #include "../librpc/gen_ndr/ndr_security.h"
30 #include "../librpc/gen_ndr/open_files.h"
31 #include "../librpc/gen_ndr/idmap.h"
32 #include "../librpc/gen_ndr/ioctl.h"
33 #include "passdb/lookup_sid.h"
34 #include "auth.h"
35 #include "serverid.h"
36 #include "messages.h"
37 #include "source3/lib/dbwrap/dbwrap_watch.h"
39 extern const struct generic_mapping file_generic_mapping;
41 struct deferred_open_record {
42 bool delayed_for_oplocks;
43 bool async_open;
44 struct file_id id;
47 /****************************************************************************
48 If the requester wanted DELETE_ACCESS and was rejected because
49 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
50 overrides this.
51 ****************************************************************************/
53 static bool parent_override_delete(connection_struct *conn,
54 const struct smb_filename *smb_fname,
55 uint32_t access_mask,
56 uint32_t rejected_mask)
58 if ((access_mask & DELETE_ACCESS) &&
59 (rejected_mask & DELETE_ACCESS) &&
60 can_delete_file_in_directory(conn, smb_fname)) {
61 return true;
63 return false;
66 /****************************************************************************
67 Check if we have open rights.
68 ****************************************************************************/
70 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
71 const struct smb_filename *smb_fname,
72 bool use_privs,
73 uint32_t access_mask)
75 /* Check if we have rights to open. */
76 NTSTATUS status;
77 struct security_descriptor *sd = NULL;
78 uint32_t rejected_share_access;
79 uint32_t rejected_mask = access_mask;
80 uint32_t do_not_check_mask = 0;
82 rejected_share_access = access_mask & ~(conn->share_access);
84 if (rejected_share_access) {
85 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
86 "on %s (0x%x)\n",
87 (unsigned int)access_mask,
88 smb_fname_str_dbg(smb_fname),
89 (unsigned int)rejected_share_access ));
90 return NT_STATUS_ACCESS_DENIED;
93 if (!use_privs && get_current_uid(conn) == (uid_t)0) {
94 /* I'm sorry sir, I didn't know you were root... */
95 DEBUG(10,("smbd_check_access_rights: root override "
96 "on %s. Granting 0x%x\n",
97 smb_fname_str_dbg(smb_fname),
98 (unsigned int)access_mask ));
99 return NT_STATUS_OK;
102 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
103 DEBUG(10,("smbd_check_access_rights: not checking ACL "
104 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
105 smb_fname_str_dbg(smb_fname),
106 (unsigned int)access_mask ));
107 return NT_STATUS_OK;
110 if (access_mask == DELETE_ACCESS &&
111 VALID_STAT(smb_fname->st) &&
112 S_ISLNK(smb_fname->st.st_ex_mode)) {
113 /* We can always delete a symlink. */
114 DEBUG(10,("smbd_check_access_rights: not checking ACL "
115 "on DELETE_ACCESS on symlink %s.\n",
116 smb_fname_str_dbg(smb_fname) ));
117 return NT_STATUS_OK;
120 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
121 (SECINFO_OWNER |
122 SECINFO_GROUP |
123 SECINFO_DACL), talloc_tos(), &sd);
125 if (!NT_STATUS_IS_OK(status)) {
126 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
127 "on %s: %s\n",
128 smb_fname_str_dbg(smb_fname),
129 nt_errstr(status)));
131 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
132 goto access_denied;
135 return status;
139 * If we can access the path to this file, by
140 * default we have FILE_READ_ATTRIBUTES from the
141 * containing directory. See the section:
142 * "Algorithm to Check Access to an Existing File"
143 * in MS-FSA.pdf.
145 * se_file_access_check() also takes care of
146 * owner WRITE_DAC and READ_CONTROL.
148 do_not_check_mask = FILE_READ_ATTRIBUTES;
151 * Samba 3.6 and earlier granted execute access even
152 * if the ACL did not contain execute rights.
153 * Samba 4.0 is more correct and checks it.
154 * The compatibilty mode allows to skip this check
155 * to smoothen upgrades.
157 if (lp_acl_allow_execute_always(SNUM(conn))) {
158 do_not_check_mask |= FILE_EXECUTE;
161 status = se_file_access_check(sd,
162 get_current_nttok(conn),
163 use_privs,
164 (access_mask & ~do_not_check_mask),
165 &rejected_mask);
167 DEBUG(10,("smbd_check_access_rights: file %s requesting "
168 "0x%x returning 0x%x (%s)\n",
169 smb_fname_str_dbg(smb_fname),
170 (unsigned int)access_mask,
171 (unsigned int)rejected_mask,
172 nt_errstr(status) ));
174 if (!NT_STATUS_IS_OK(status)) {
175 if (DEBUGLEVEL >= 10) {
176 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
177 smb_fname_str_dbg(smb_fname) ));
178 NDR_PRINT_DEBUG(security_descriptor, sd);
182 TALLOC_FREE(sd);
184 if (NT_STATUS_IS_OK(status) ||
185 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
186 return status;
189 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
191 access_denied:
193 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
194 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
195 !lp_store_dos_attributes(SNUM(conn)) &&
196 (lp_map_readonly(SNUM(conn)) ||
197 lp_map_archive(SNUM(conn)) ||
198 lp_map_hidden(SNUM(conn)) ||
199 lp_map_system(SNUM(conn)))) {
200 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
202 DEBUG(10,("smbd_check_access_rights: "
203 "overrode "
204 "FILE_WRITE_ATTRIBUTES "
205 "on file %s\n",
206 smb_fname_str_dbg(smb_fname)));
209 if (parent_override_delete(conn,
210 smb_fname,
211 access_mask,
212 rejected_mask)) {
213 /* Were we trying to do an open
214 * for delete and didn't get DELETE
215 * access (only) ? Check if the
216 * directory allows DELETE_CHILD.
217 * See here:
218 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
219 * for details. */
221 rejected_mask &= ~DELETE_ACCESS;
223 DEBUG(10,("smbd_check_access_rights: "
224 "overrode "
225 "DELETE_ACCESS on "
226 "file %s\n",
227 smb_fname_str_dbg(smb_fname)));
230 if (rejected_mask != 0) {
231 return NT_STATUS_ACCESS_DENIED;
233 return NT_STATUS_OK;
236 static NTSTATUS check_parent_access(struct connection_struct *conn,
237 struct smb_filename *smb_fname,
238 uint32_t access_mask)
240 NTSTATUS status;
241 char *parent_dir = NULL;
242 struct security_descriptor *parent_sd = NULL;
243 uint32_t access_granted = 0;
245 if (!parent_dirname(talloc_tos(),
246 smb_fname->base_name,
247 &parent_dir,
248 NULL)) {
249 return NT_STATUS_NO_MEMORY;
252 if (get_current_uid(conn) == (uid_t)0) {
253 /* I'm sorry sir, I didn't know you were root... */
254 DEBUG(10,("check_parent_access: root override "
255 "on %s. Granting 0x%x\n",
256 smb_fname_str_dbg(smb_fname),
257 (unsigned int)access_mask ));
258 return NT_STATUS_OK;
261 status = SMB_VFS_GET_NT_ACL(conn,
262 parent_dir,
263 SECINFO_DACL,
264 talloc_tos(),
265 &parent_sd);
267 if (!NT_STATUS_IS_OK(status)) {
268 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
269 "%s with error %s\n",
270 parent_dir,
271 nt_errstr(status)));
272 return status;
276 * If we can access the path to this file, by
277 * default we have FILE_READ_ATTRIBUTES from the
278 * containing directory. See the section:
279 * "Algorithm to Check Access to an Existing File"
280 * in MS-FSA.pdf.
282 * se_file_access_check() also takes care of
283 * owner WRITE_DAC and READ_CONTROL.
285 status = se_file_access_check(parent_sd,
286 get_current_nttok(conn),
287 false,
288 (access_mask & ~FILE_READ_ATTRIBUTES),
289 &access_granted);
290 if(!NT_STATUS_IS_OK(status)) {
291 DEBUG(5,("check_parent_access: access check "
292 "on directory %s for "
293 "path %s for mask 0x%x returned (0x%x) %s\n",
294 parent_dir,
295 smb_fname->base_name,
296 access_mask,
297 access_granted,
298 nt_errstr(status) ));
299 return status;
302 return NT_STATUS_OK;
305 /****************************************************************************
306 Ensure when opening a base file for a stream open that we have permissions
307 to do so given the access mask on the base file.
308 ****************************************************************************/
310 static NTSTATUS check_base_file_access(struct connection_struct *conn,
311 struct smb_filename *smb_fname,
312 uint32_t access_mask)
314 NTSTATUS status;
316 status = smbd_calculate_access_mask(conn, smb_fname,
317 false,
318 access_mask,
319 &access_mask);
320 if (!NT_STATUS_IS_OK(status)) {
321 DEBUG(10, ("smbd_calculate_access_mask "
322 "on file %s returned %s\n",
323 smb_fname_str_dbg(smb_fname),
324 nt_errstr(status)));
325 return status;
328 if (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) {
329 uint32_t dosattrs;
330 if (!CAN_WRITE(conn)) {
331 return NT_STATUS_ACCESS_DENIED;
333 dosattrs = dos_mode(conn, smb_fname);
334 if (IS_DOS_READONLY(dosattrs)) {
335 return NT_STATUS_ACCESS_DENIED;
339 return smbd_check_access_rights(conn,
340 smb_fname,
341 false,
342 access_mask);
345 /****************************************************************************
346 fd support routines - attempt to do a dos_open.
347 ****************************************************************************/
349 NTSTATUS fd_open(struct connection_struct *conn,
350 files_struct *fsp,
351 int flags,
352 mode_t mode)
354 struct smb_filename *smb_fname = fsp->fsp_name;
355 NTSTATUS status = NT_STATUS_OK;
357 #ifdef O_NOFOLLOW
359 * Never follow symlinks on a POSIX client. The
360 * client should be doing this.
363 if (fsp->posix_open || !lp_follow_symlinks(SNUM(conn))) {
364 flags |= O_NOFOLLOW;
366 #endif
368 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
369 if (fsp->fh->fd == -1) {
370 int posix_errno = errno;
371 #ifdef O_NOFOLLOW
372 #if defined(ENOTSUP) && defined(OSF1)
373 /* handle special Tru64 errno */
374 if (errno == ENOTSUP) {
375 posix_errno = ELOOP;
377 #endif /* ENOTSUP */
378 #ifdef EFTYPE
379 /* fix broken NetBSD errno */
380 if (errno == EFTYPE) {
381 posix_errno = ELOOP;
383 #endif /* EFTYPE */
384 /* fix broken FreeBSD errno */
385 if (errno == EMLINK) {
386 posix_errno = ELOOP;
388 #endif /* O_NOFOLLOW */
389 status = map_nt_error_from_unix(posix_errno);
390 if (errno == EMFILE) {
391 static time_t last_warned = 0L;
393 if (time((time_t *) NULL) > last_warned) {
394 DEBUG(0,("Too many open files, unable "
395 "to open more! smbd's max "
396 "open files = %d\n",
397 lp_max_open_files()));
398 last_warned = time((time_t *) NULL);
404 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
405 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
406 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
408 return status;
411 /****************************************************************************
412 Close the file associated with a fsp.
413 ****************************************************************************/
415 NTSTATUS fd_close(files_struct *fsp)
417 int ret;
419 if (fsp->dptr) {
420 dptr_CloseDir(fsp);
422 if (fsp->fh->fd == -1) {
423 return NT_STATUS_OK; /* What we used to call a stat open. */
425 if (fsp->fh->ref_count > 1) {
426 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
429 ret = SMB_VFS_CLOSE(fsp);
430 fsp->fh->fd = -1;
431 if (ret == -1) {
432 return map_nt_error_from_unix(errno);
434 return NT_STATUS_OK;
437 /****************************************************************************
438 Change the ownership of a file to that of the parent directory.
439 Do this by fd if possible.
440 ****************************************************************************/
442 void change_file_owner_to_parent(connection_struct *conn,
443 const char *inherit_from_dir,
444 files_struct *fsp)
446 struct smb_filename *smb_fname_parent;
447 int ret;
449 smb_fname_parent = synthetic_smb_fname(talloc_tos(), inherit_from_dir,
450 NULL, NULL);
451 if (smb_fname_parent == NULL) {
452 return;
455 ret = SMB_VFS_STAT(conn, smb_fname_parent);
456 if (ret == -1) {
457 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
458 "directory %s. Error was %s\n",
459 smb_fname_str_dbg(smb_fname_parent),
460 strerror(errno)));
461 TALLOC_FREE(smb_fname_parent);
462 return;
465 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
466 /* Already this uid - no need to change. */
467 DEBUG(10,("change_file_owner_to_parent: file %s "
468 "is already owned by uid %d\n",
469 fsp_str_dbg(fsp),
470 (int)fsp->fsp_name->st.st_ex_uid ));
471 TALLOC_FREE(smb_fname_parent);
472 return;
475 become_root();
476 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
477 unbecome_root();
478 if (ret == -1) {
479 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
480 "file %s to parent directory uid %u. Error "
481 "was %s\n", fsp_str_dbg(fsp),
482 (unsigned int)smb_fname_parent->st.st_ex_uid,
483 strerror(errno) ));
484 } else {
485 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
486 "parent directory uid %u.\n", fsp_str_dbg(fsp),
487 (unsigned int)smb_fname_parent->st.st_ex_uid));
488 /* Ensure the uid entry is updated. */
489 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
492 TALLOC_FREE(smb_fname_parent);
495 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
496 const char *inherit_from_dir,
497 const char *fname,
498 SMB_STRUCT_STAT *psbuf)
500 struct smb_filename *smb_fname_parent;
501 struct smb_filename *smb_fname_cwd = NULL;
502 char *saved_dir = NULL;
503 TALLOC_CTX *ctx = talloc_tos();
504 NTSTATUS status = NT_STATUS_OK;
505 int ret;
507 smb_fname_parent = synthetic_smb_fname(ctx, inherit_from_dir,
508 NULL, NULL);
509 if (smb_fname_parent == NULL) {
510 return NT_STATUS_NO_MEMORY;
513 ret = SMB_VFS_STAT(conn, smb_fname_parent);
514 if (ret == -1) {
515 status = map_nt_error_from_unix(errno);
516 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
517 "directory %s. Error was %s\n",
518 smb_fname_str_dbg(smb_fname_parent),
519 strerror(errno)));
520 goto out;
523 /* We've already done an lstat into psbuf, and we know it's a
524 directory. If we can cd into the directory and the dev/ino
525 are the same then we can safely chown without races as
526 we're locking the directory in place by being in it. This
527 should work on any UNIX (thanks tridge :-). JRA.
530 saved_dir = vfs_GetWd(ctx,conn);
531 if (!saved_dir) {
532 status = map_nt_error_from_unix(errno);
533 DEBUG(0,("change_dir_owner_to_parent: failed to get "
534 "current working directory. Error was %s\n",
535 strerror(errno)));
536 goto out;
539 /* Chdir into the new path. */
540 if (vfs_ChDir(conn, fname) == -1) {
541 status = map_nt_error_from_unix(errno);
542 DEBUG(0,("change_dir_owner_to_parent: failed to change "
543 "current working directory to %s. Error "
544 "was %s\n", fname, strerror(errno) ));
545 goto chdir;
548 smb_fname_cwd = synthetic_smb_fname(ctx, ".", NULL, NULL);
549 if (smb_fname_cwd == NULL) {
550 status = NT_STATUS_NO_MEMORY;
551 goto chdir;
554 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
555 if (ret == -1) {
556 status = map_nt_error_from_unix(errno);
557 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
558 "directory '.' (%s) Error was %s\n",
559 fname, strerror(errno)));
560 goto chdir;
563 /* Ensure we're pointing at the same place. */
564 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
565 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
566 DEBUG(0,("change_dir_owner_to_parent: "
567 "device/inode on directory %s changed. "
568 "Refusing to chown !\n", fname ));
569 status = NT_STATUS_ACCESS_DENIED;
570 goto chdir;
573 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
574 /* Already this uid - no need to change. */
575 DEBUG(10,("change_dir_owner_to_parent: directory %s "
576 "is already owned by uid %d\n",
577 fname,
578 (int)smb_fname_cwd->st.st_ex_uid ));
579 status = NT_STATUS_OK;
580 goto chdir;
583 become_root();
584 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
585 (gid_t)-1);
586 unbecome_root();
587 if (ret == -1) {
588 status = map_nt_error_from_unix(errno);
589 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
590 "directory %s to parent directory uid %u. "
591 "Error was %s\n", fname,
592 (unsigned int)smb_fname_parent->st.st_ex_uid,
593 strerror(errno) ));
594 } else {
595 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
596 "directory %s to parent directory uid %u.\n",
597 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
598 /* Ensure the uid entry is updated. */
599 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
602 chdir:
603 vfs_ChDir(conn,saved_dir);
604 out:
605 TALLOC_FREE(smb_fname_parent);
606 TALLOC_FREE(smb_fname_cwd);
607 return status;
610 /****************************************************************************
611 Open a file - returning a guaranteed ATOMIC indication of if the
612 file was created or not.
613 ****************************************************************************/
615 static NTSTATUS fd_open_atomic(struct connection_struct *conn,
616 files_struct *fsp,
617 int flags,
618 mode_t mode,
619 bool *file_created)
621 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
622 bool file_existed = VALID_STAT(fsp->fsp_name->st);
624 *file_created = false;
626 if (!(flags & O_CREAT)) {
628 * We're not creating the file, just pass through.
630 return fd_open(conn, fsp, flags, mode);
633 if (flags & O_EXCL) {
635 * Fail if already exists, just pass through.
637 status = fd_open(conn, fsp, flags, mode);
640 * Here we've opened with O_CREAT|O_EXCL. If that went
641 * NT_STATUS_OK, we *know* we created this file.
643 *file_created = NT_STATUS_IS_OK(status);
645 return status;
649 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
650 * To know absolutely if we created the file or not,
651 * we can never call O_CREAT without O_EXCL. So if
652 * we think the file existed, try without O_CREAT|O_EXCL.
653 * If we think the file didn't exist, try with
654 * O_CREAT|O_EXCL. Keep bouncing between these two
655 * requests until either the file is created, or
656 * opened. Either way, we keep going until we get
657 * a returnable result (error, or open/create).
660 while(1) {
661 int curr_flags = flags;
663 if (file_existed) {
664 /* Just try open, do not create. */
665 curr_flags &= ~(O_CREAT);
666 status = fd_open(conn, fsp, curr_flags, mode);
667 if (NT_STATUS_EQUAL(status,
668 NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
670 * Someone deleted it in the meantime.
671 * Retry with O_EXCL.
673 file_existed = false;
674 DEBUG(10,("fd_open_atomic: file %s existed. "
675 "Retry.\n",
676 smb_fname_str_dbg(fsp->fsp_name)));
677 continue;
679 } else {
680 /* Try create exclusively, fail if it exists. */
681 curr_flags |= O_EXCL;
682 status = fd_open(conn, fsp, curr_flags, mode);
683 if (NT_STATUS_EQUAL(status,
684 NT_STATUS_OBJECT_NAME_COLLISION)) {
686 * Someone created it in the meantime.
687 * Retry without O_CREAT.
689 file_existed = true;
690 DEBUG(10,("fd_open_atomic: file %s "
691 "did not exist. Retry.\n",
692 smb_fname_str_dbg(fsp->fsp_name)));
693 continue;
695 if (NT_STATUS_IS_OK(status)) {
697 * Here we've opened with O_CREAT|O_EXCL
698 * and got success. We *know* we created
699 * this file.
701 *file_created = true;
704 /* Create is done, or failed. */
705 break;
707 return status;
710 /****************************************************************************
711 Open a file.
712 ****************************************************************************/
714 static NTSTATUS open_file(files_struct *fsp,
715 connection_struct *conn,
716 struct smb_request *req,
717 const char *parent_dir,
718 int flags,
719 mode_t unx_mode,
720 uint32 access_mask, /* client requested access mask. */
721 uint32 open_access_mask, /* what we're actually using in the open. */
722 bool *p_file_created)
724 struct smb_filename *smb_fname = fsp->fsp_name;
725 NTSTATUS status = NT_STATUS_OK;
726 int accmode = (flags & O_ACCMODE);
727 int local_flags = flags;
728 bool file_existed = VALID_STAT(fsp->fsp_name->st);
730 fsp->fh->fd = -1;
731 errno = EPERM;
733 /* Check permissions */
736 * This code was changed after seeing a client open request
737 * containing the open mode of (DENY_WRITE/read-only) with
738 * the 'create if not exist' bit set. The previous code
739 * would fail to open the file read only on a read-only share
740 * as it was checking the flags parameter directly against O_RDONLY,
741 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
742 * JRA.
745 if (!CAN_WRITE(conn)) {
746 /* It's a read-only share - fail if we wanted to write. */
747 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
748 DEBUG(3,("Permission denied opening %s\n",
749 smb_fname_str_dbg(smb_fname)));
750 return NT_STATUS_ACCESS_DENIED;
752 if (flags & O_CREAT) {
753 /* We don't want to write - but we must make sure that
754 O_CREAT doesn't create the file if we have write
755 access into the directory.
757 flags &= ~(O_CREAT|O_EXCL);
758 local_flags &= ~(O_CREAT|O_EXCL);
763 * This little piece of insanity is inspired by the
764 * fact that an NT client can open a file for O_RDONLY,
765 * but set the create disposition to FILE_EXISTS_TRUNCATE.
766 * If the client *can* write to the file, then it expects to
767 * truncate the file, even though it is opening for readonly.
768 * Quicken uses this stupid trick in backup file creation...
769 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
770 * for helping track this one down. It didn't bite us in 2.0.x
771 * as we always opened files read-write in that release. JRA.
774 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
775 DEBUG(10,("open_file: truncate requested on read-only open "
776 "for file %s\n", smb_fname_str_dbg(smb_fname)));
777 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
780 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
781 (!file_existed && (local_flags & O_CREAT)) ||
782 ((local_flags & O_TRUNC) == O_TRUNC) ) {
783 const char *wild;
784 int ret;
786 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
788 * We would block on opening a FIFO with no one else on the
789 * other end. Do what we used to do and add O_NONBLOCK to the
790 * open flags. JRA.
793 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
794 local_flags &= ~O_TRUNC; /* Can't truncate a FIFO. */
795 local_flags |= O_NONBLOCK;
797 #endif
799 /* Don't create files with Microsoft wildcard characters. */
800 if (fsp->base_fsp) {
802 * wildcard characters are allowed in stream names
803 * only test the basefilename
805 wild = fsp->base_fsp->fsp_name->base_name;
806 } else {
807 wild = smb_fname->base_name;
809 if ((local_flags & O_CREAT) && !file_existed &&
810 ms_has_wild(wild)) {
811 return NT_STATUS_OBJECT_NAME_INVALID;
814 /* Can we access this file ? */
815 if (!fsp->base_fsp) {
816 /* Only do this check on non-stream open. */
817 if (file_existed) {
818 status = smbd_check_access_rights(conn,
819 smb_fname,
820 false,
821 access_mask);
822 } else if (local_flags & O_CREAT){
823 status = check_parent_access(conn,
824 smb_fname,
825 SEC_DIR_ADD_FILE);
826 } else {
827 /* File didn't exist and no O_CREAT. */
828 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
830 if (!NT_STATUS_IS_OK(status)) {
831 DEBUG(10,("open_file: "
832 "%s on file "
833 "%s returned %s\n",
834 file_existed ?
835 "smbd_check_access_rights" :
836 "check_parent_access",
837 smb_fname_str_dbg(smb_fname),
838 nt_errstr(status) ));
839 return status;
843 /* Actually do the open */
844 status = fd_open_atomic(conn, fsp, local_flags,
845 unx_mode, p_file_created);
846 if (!NT_STATUS_IS_OK(status)) {
847 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
848 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
849 nt_errstr(status),local_flags,flags));
850 return status;
853 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
854 if (ret == -1) {
855 /* If we have an fd, this stat should succeed. */
856 DEBUG(0,("Error doing fstat on open file %s "
857 "(%s)\n",
858 smb_fname_str_dbg(smb_fname),
859 strerror(errno) ));
860 status = map_nt_error_from_unix(errno);
861 fd_close(fsp);
862 return status;
865 if (*p_file_created) {
866 /* We created this file. */
868 bool need_re_stat = false;
869 /* Do all inheritance work after we've
870 done a successful fstat call and filled
871 in the stat struct in fsp->fsp_name. */
873 /* Inherit the ACL if required */
874 if (lp_inherit_permissions(SNUM(conn))) {
875 inherit_access_posix_acl(conn, parent_dir,
876 smb_fname->base_name,
877 unx_mode);
878 need_re_stat = true;
881 /* Change the owner if required. */
882 if (lp_inherit_owner(SNUM(conn))) {
883 change_file_owner_to_parent(conn, parent_dir,
884 fsp);
885 need_re_stat = true;
888 if (need_re_stat) {
889 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
890 /* If we have an fd, this stat should succeed. */
891 if (ret == -1) {
892 DEBUG(0,("Error doing fstat on open file %s "
893 "(%s)\n",
894 smb_fname_str_dbg(smb_fname),
895 strerror(errno) ));
899 notify_fname(conn, NOTIFY_ACTION_ADDED,
900 FILE_NOTIFY_CHANGE_FILE_NAME,
901 smb_fname->base_name);
903 } else {
904 fsp->fh->fd = -1; /* What we used to call a stat open. */
905 if (!file_existed) {
906 /* File must exist for a stat open. */
907 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
910 status = smbd_check_access_rights(conn,
911 smb_fname,
912 false,
913 access_mask);
915 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
916 fsp->posix_open &&
917 S_ISLNK(smb_fname->st.st_ex_mode)) {
918 /* This is a POSIX stat open for delete
919 * or rename on a symlink that points
920 * nowhere. Allow. */
921 DEBUG(10,("open_file: allowing POSIX "
922 "open on bad symlink %s\n",
923 smb_fname_str_dbg(smb_fname)));
924 status = NT_STATUS_OK;
927 if (!NT_STATUS_IS_OK(status)) {
928 DEBUG(10,("open_file: "
929 "smbd_check_access_rights on file "
930 "%s returned %s\n",
931 smb_fname_str_dbg(smb_fname),
932 nt_errstr(status) ));
933 return status;
938 * POSIX allows read-only opens of directories. We don't
939 * want to do this (we use a different code path for this)
940 * so catch a directory open and return an EISDIR. JRA.
943 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
944 fd_close(fsp);
945 errno = EISDIR;
946 return NT_STATUS_FILE_IS_A_DIRECTORY;
949 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
950 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
951 fsp->file_pid = req ? req->smbpid : 0;
952 fsp->can_lock = True;
953 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
954 fsp->can_write =
955 CAN_WRITE(conn) &&
956 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
957 fsp->print_file = NULL;
958 fsp->modified = False;
959 fsp->sent_oplock_break = NO_BREAK_SENT;
960 fsp->is_directory = False;
961 if (conn->aio_write_behind_list &&
962 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
963 conn->case_sensitive)) {
964 fsp->aio_write_behind = True;
967 fsp->wcp = NULL; /* Write cache pointer. */
969 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
970 conn->session_info->unix_info->unix_name,
971 smb_fname_str_dbg(smb_fname),
972 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
973 conn->num_files_open));
975 errno = 0;
976 return NT_STATUS_OK;
979 /****************************************************************************
980 Check if we can open a file with a share mode.
981 Returns True if conflict, False if not.
982 ****************************************************************************/
984 static bool share_conflict(struct share_mode_entry *entry,
985 uint32 access_mask,
986 uint32 share_access)
988 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
989 "entry->share_access = 0x%x, "
990 "entry->private_options = 0x%x\n",
991 (unsigned int)entry->access_mask,
992 (unsigned int)entry->share_access,
993 (unsigned int)entry->private_options));
995 if (server_id_is_disconnected(&entry->pid)) {
997 * note: cleanup should have been done by
998 * delay_for_batch_oplocks()
1000 return false;
1003 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
1004 (unsigned int)access_mask, (unsigned int)share_access));
1006 if ((entry->access_mask & (FILE_WRITE_DATA|
1007 FILE_APPEND_DATA|
1008 FILE_READ_DATA|
1009 FILE_EXECUTE|
1010 DELETE_ACCESS)) == 0) {
1011 DEBUG(10,("share_conflict: No conflict due to "
1012 "entry->access_mask = 0x%x\n",
1013 (unsigned int)entry->access_mask ));
1014 return False;
1017 if ((access_mask & (FILE_WRITE_DATA|
1018 FILE_APPEND_DATA|
1019 FILE_READ_DATA|
1020 FILE_EXECUTE|
1021 DELETE_ACCESS)) == 0) {
1022 DEBUG(10,("share_conflict: No conflict due to "
1023 "access_mask = 0x%x\n",
1024 (unsigned int)access_mask ));
1025 return False;
1028 #if 1 /* JRA TEST - Superdebug. */
1029 #define CHECK_MASK(num, am, right, sa, share) \
1030 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1031 (unsigned int)(num), (unsigned int)(am), \
1032 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1033 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1034 (unsigned int)(num), (unsigned int)(sa), \
1035 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1036 if (((am) & (right)) && !((sa) & (share))) { \
1037 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1038 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1039 (unsigned int)(share) )); \
1040 return True; \
1042 #else
1043 #define CHECK_MASK(num, am, right, sa, share) \
1044 if (((am) & (right)) && !((sa) & (share))) { \
1045 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1046 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1047 (unsigned int)(share) )); \
1048 return True; \
1050 #endif
1052 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1053 share_access, FILE_SHARE_WRITE);
1054 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1055 entry->share_access, FILE_SHARE_WRITE);
1057 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
1058 share_access, FILE_SHARE_READ);
1059 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
1060 entry->share_access, FILE_SHARE_READ);
1062 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
1063 share_access, FILE_SHARE_DELETE);
1064 CHECK_MASK(6, access_mask, DELETE_ACCESS,
1065 entry->share_access, FILE_SHARE_DELETE);
1067 DEBUG(10,("share_conflict: No conflict.\n"));
1068 return False;
1071 #if defined(DEVELOPER)
1072 static void validate_my_share_entries(struct smbd_server_connection *sconn,
1073 int num,
1074 struct share_mode_entry *share_entry)
1076 struct server_id self = messaging_server_id(sconn->msg_ctx);
1077 files_struct *fsp;
1079 if (!serverid_equal(&self, &share_entry->pid)) {
1080 return;
1083 if (share_entry->share_file_id == 0) {
1084 /* INTERNAL_OPEN_ONLY */
1085 return;
1088 if (!is_valid_share_mode_entry(share_entry)) {
1089 return;
1092 fsp = file_find_dif(sconn, share_entry->id,
1093 share_entry->share_file_id);
1094 if (!fsp) {
1095 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1096 share_mode_str(talloc_tos(), num, share_entry) ));
1097 smb_panic("validate_my_share_entries: Cannot match a "
1098 "share entry with an open file\n");
1101 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
1102 goto panic;
1105 return;
1107 panic:
1109 char *str;
1110 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1111 share_mode_str(talloc_tos(), num, share_entry) ));
1112 str = talloc_asprintf(talloc_tos(),
1113 "validate_my_share_entries: "
1114 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1115 fsp->fsp_name->base_name,
1116 (unsigned int)fsp->oplock_type,
1117 (unsigned int)share_entry->op_type );
1118 smb_panic(str);
1121 #endif
1123 bool is_stat_open(uint32 access_mask)
1125 const uint32_t stat_open_bits =
1126 (SYNCHRONIZE_ACCESS|
1127 FILE_READ_ATTRIBUTES|
1128 FILE_WRITE_ATTRIBUTES);
1130 return (((access_mask & stat_open_bits) != 0) &&
1131 ((access_mask & ~stat_open_bits) == 0));
1134 static bool has_delete_on_close(struct share_mode_lock *lck,
1135 uint32_t name_hash)
1137 struct share_mode_data *d = lck->data;
1138 uint32_t i;
1140 if (d->num_share_modes == 0) {
1141 return false;
1143 if (!is_delete_on_close_set(lck, name_hash)) {
1144 return false;
1146 for (i=0; i<d->num_share_modes; i++) {
1147 if (!share_mode_stale_pid(d, i)) {
1148 return true;
1151 return false;
1154 /****************************************************************************
1155 Deal with share modes
1156 Invariant: Share mode must be locked on entry and exit.
1157 Returns -1 on error, or number of share modes on success (may be zero).
1158 ****************************************************************************/
1160 static NTSTATUS open_mode_check(connection_struct *conn,
1161 struct share_mode_lock *lck,
1162 uint32 access_mask,
1163 uint32 share_access)
1165 int i;
1167 if(lck->data->num_share_modes == 0) {
1168 return NT_STATUS_OK;
1171 if (is_stat_open(access_mask)) {
1172 /* Stat open that doesn't trigger oplock breaks or share mode
1173 * checks... ! JRA. */
1174 return NT_STATUS_OK;
1178 * Check if the share modes will give us access.
1181 #if defined(DEVELOPER)
1182 for(i = 0; i < lck->data->num_share_modes; i++) {
1183 validate_my_share_entries(conn->sconn, i,
1184 &lck->data->share_modes[i]);
1186 #endif
1188 /* Now we check the share modes, after any oplock breaks. */
1189 for(i = 0; i < lck->data->num_share_modes; i++) {
1191 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1192 continue;
1195 /* someone else has a share lock on it, check to see if we can
1196 * too */
1197 if (share_conflict(&lck->data->share_modes[i],
1198 access_mask, share_access)) {
1200 if (share_mode_stale_pid(lck->data, i)) {
1201 continue;
1204 return NT_STATUS_SHARING_VIOLATION;
1208 return NT_STATUS_OK;
1212 * Send a break message to the oplock holder and delay the open for
1213 * our client.
1216 static NTSTATUS send_break_message(struct messaging_context *msg_ctx,
1217 const struct share_mode_entry *exclusive,
1218 uint16_t break_to)
1220 NTSTATUS status;
1221 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1223 DEBUG(10, ("Sending break request to PID %s\n",
1224 procid_str_static(&exclusive->pid)));
1226 /* Create the message. */
1227 share_mode_entry_to_message(msg, exclusive);
1229 /* Overload entry->op_type */
1230 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET, break_to);
1232 status = messaging_send_buf(msg_ctx, exclusive->pid,
1233 MSG_SMB_BREAK_REQUEST,
1234 (uint8 *)msg, sizeof(msg));
1235 if (!NT_STATUS_IS_OK(status)) {
1236 DEBUG(3, ("Could not send oplock break message: %s\n",
1237 nt_errstr(status)));
1240 return status;
1244 * Do internal consistency checks on the share mode for a file.
1247 static bool validate_oplock_types(struct share_mode_lock *lck)
1249 struct share_mode_data *d = lck->data;
1250 bool batch = false;
1251 bool ex_or_batch = false;
1252 bool level2 = false;
1253 bool no_oplock = false;
1254 uint32_t num_non_stat_opens = 0;
1255 uint32_t i;
1257 for (i=0; i<d->num_share_modes; i++) {
1258 struct share_mode_entry *e = &d->share_modes[i];
1260 if (!is_valid_share_mode_entry(e)) {
1261 continue;
1264 if (e->op_type == NO_OPLOCK && is_stat_open(e->access_mask)) {
1265 /* We ignore stat opens in the table - they
1266 always have NO_OPLOCK and never get or
1267 cause breaks. JRA. */
1268 continue;
1271 num_non_stat_opens += 1;
1273 if (BATCH_OPLOCK_TYPE(e->op_type)) {
1274 /* batch - can only be one. */
1275 if (share_mode_stale_pid(d, i)) {
1276 DEBUG(10, ("Found stale batch oplock\n"));
1277 continue;
1279 if (ex_or_batch || batch || level2 || no_oplock) {
1280 DEBUG(0, ("Bad batch oplock entry %u.",
1281 (unsigned)i));
1282 return false;
1284 batch = true;
1287 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1288 if (share_mode_stale_pid(d, i)) {
1289 DEBUG(10, ("Found stale duplicate oplock\n"));
1290 continue;
1292 /* Exclusive or batch - can only be one. */
1293 if (ex_or_batch || level2 || no_oplock) {
1294 DEBUG(0, ("Bad exclusive or batch oplock "
1295 "entry %u.", (unsigned)i));
1296 return false;
1298 ex_or_batch = true;
1301 if (LEVEL_II_OPLOCK_TYPE(e->op_type)) {
1302 if (batch || ex_or_batch) {
1303 if (share_mode_stale_pid(d, i)) {
1304 DEBUG(10, ("Found stale LevelII "
1305 "oplock\n"));
1306 continue;
1308 DEBUG(0, ("Bad levelII oplock entry %u.",
1309 (unsigned)i));
1310 return false;
1312 level2 = true;
1315 if (e->op_type == NO_OPLOCK) {
1316 if (batch || ex_or_batch) {
1317 if (share_mode_stale_pid(d, i)) {
1318 DEBUG(10, ("Found stale NO_OPLOCK "
1319 "entry\n"));
1320 continue;
1322 DEBUG(0, ("Bad no oplock entry %u.",
1323 (unsigned)i));
1324 return false;
1326 no_oplock = true;
1330 remove_stale_share_mode_entries(d);
1332 if ((batch || ex_or_batch) && (num_non_stat_opens != 1)) {
1333 DEBUG(1, ("got batch (%d) or ex (%d) non-exclusively (%d)\n",
1334 (int)batch, (int)ex_or_batch,
1335 (int)d->num_share_modes));
1336 return false;
1339 return true;
1342 static bool delay_for_oplock(files_struct *fsp,
1343 int oplock_request,
1344 struct share_mode_lock *lck,
1345 bool have_sharing_violation,
1346 uint32_t create_disposition)
1348 struct share_mode_data *d = lck->data;
1349 struct share_mode_entry *entry;
1350 uint32_t num_non_stat_opens = 0;
1351 uint32_t i;
1352 uint16_t break_to;
1354 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1355 return false;
1357 for (i=0; i<d->num_share_modes; i++) {
1358 struct share_mode_entry *e = &d->share_modes[i];
1359 if (e->op_type == NO_OPLOCK && is_stat_open(e->access_mask)) {
1360 continue;
1362 num_non_stat_opens += 1;
1365 * We found the a non-stat open, which in the exclusive/batch
1366 * case will be inspected further down.
1368 entry = e;
1370 if (num_non_stat_opens == 0) {
1372 * Nothing to wait for around
1374 return false;
1376 if (num_non_stat_opens != 1) {
1378 * More than one open around. There can't be any exclusive or
1379 * batch left, this is all level2.
1381 return false;
1384 if (server_id_is_disconnected(&entry->pid)) {
1386 * TODO: clean up.
1387 * This could be achieved by sending a break message
1388 * to ourselves. Special considerations for files
1389 * with delete_on_close flag set!
1391 * For now we keep it simple and do not
1392 * allow delete on close for durable handles.
1394 return false;
1397 switch (create_disposition) {
1398 case FILE_SUPERSEDE:
1399 case FILE_OVERWRITE_IF:
1400 break_to = NO_OPLOCK;
1401 break;
1402 default:
1403 break_to = LEVEL_II_OPLOCK;
1404 break;
1407 if (have_sharing_violation && (entry->op_type & BATCH_OPLOCK)) {
1408 if (share_mode_stale_pid(d, 0)) {
1409 return false;
1411 send_break_message(fsp->conn->sconn->msg_ctx, entry, break_to);
1412 return true;
1414 if (have_sharing_violation) {
1416 * Non-batch exclusive is not broken if we have a sharing
1417 * violation
1419 return false;
1421 if (LEVEL_II_OPLOCK_TYPE(entry->op_type) &&
1422 (break_to == NO_OPLOCK)) {
1423 if (share_mode_stale_pid(d, 0)) {
1424 return false;
1426 DEBUG(10, ("Asynchronously breaking level2 oplock for "
1427 "create_disposition=%u\n",
1428 (unsigned)create_disposition));
1429 send_break_message(fsp->conn->sconn->msg_ctx, entry, break_to);
1430 return false;
1432 if (!EXCLUSIVE_OPLOCK_TYPE(entry->op_type)) {
1434 * No break for NO_OPLOCK or LEVEL2_OPLOCK oplocks
1436 return false;
1438 if (share_mode_stale_pid(d, 0)) {
1439 return false;
1442 send_break_message(fsp->conn->sconn->msg_ctx, entry, break_to);
1443 return true;
1446 static bool file_has_brlocks(files_struct *fsp)
1448 struct byte_range_lock *br_lck;
1450 br_lck = brl_get_locks_readonly(fsp);
1451 if (!br_lck)
1452 return false;
1454 return (brl_num_locks(br_lck) > 0);
1457 static void grant_fsp_oplock_type(files_struct *fsp,
1458 struct share_mode_lock *lck,
1459 int oplock_request)
1461 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1462 lp_level2_oplocks(SNUM(fsp->conn));
1463 bool got_level2_oplock, got_a_none_oplock;
1464 uint32_t i;
1466 /* Start by granting what the client asked for,
1467 but ensure no SAMBA_PRIVATE bits can be set. */
1468 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1470 if (oplock_request & INTERNAL_OPEN_ONLY) {
1471 /* No oplocks on internal open. */
1472 fsp->oplock_type = NO_OPLOCK;
1473 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1474 fsp->oplock_type, fsp_str_dbg(fsp)));
1475 return;
1478 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1479 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1480 fsp_str_dbg(fsp)));
1481 fsp->oplock_type = NO_OPLOCK;
1484 if (is_stat_open(fsp->access_mask)) {
1485 /* Leave the value already set. */
1486 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1487 fsp->oplock_type, fsp_str_dbg(fsp)));
1488 return;
1491 got_level2_oplock = false;
1492 got_a_none_oplock = false;
1494 for (i=0; i<lck->data->num_share_modes; i++) {
1495 int op_type = lck->data->share_modes[i].op_type;
1497 if (LEVEL_II_OPLOCK_TYPE(op_type)) {
1498 got_level2_oplock = true;
1500 if (op_type == NO_OPLOCK) {
1501 got_a_none_oplock = true;
1506 * Match what was requested (fsp->oplock_type) with
1507 * what was found in the existing share modes.
1510 if (got_level2_oplock || got_a_none_oplock) {
1511 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1512 fsp->oplock_type = LEVEL_II_OPLOCK;
1517 * Don't grant level2 to clients that don't want them
1518 * or if we've turned them off.
1520 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1521 fsp->oplock_type = NO_OPLOCK;
1524 if (fsp->oplock_type == LEVEL_II_OPLOCK && !got_level2_oplock) {
1526 * We're the first level2 oplock. Indicate that in brlock.tdb.
1528 struct byte_range_lock *brl;
1530 brl = brl_get_locks(talloc_tos(), fsp);
1531 if (brl != NULL) {
1532 brl_set_have_read_oplocks(brl, true);
1533 TALLOC_FREE(brl);
1537 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1538 fsp->oplock_type, fsp_str_dbg(fsp)));
1541 static bool request_timed_out(struct timeval request_time,
1542 struct timeval timeout)
1544 struct timeval now, end_time;
1545 GetTimeOfDay(&now);
1546 end_time = timeval_sum(&request_time, &timeout);
1547 return (timeval_compare(&end_time, &now) < 0);
1550 struct defer_open_state {
1551 struct smbd_server_connection *sconn;
1552 uint64_t mid;
1555 static void defer_open_done(struct tevent_req *req);
1557 /****************************************************************************
1558 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1559 ****************************************************************************/
1561 static void defer_open(struct share_mode_lock *lck,
1562 struct timeval request_time,
1563 struct timeval timeout,
1564 struct smb_request *req,
1565 struct deferred_open_record *state)
1567 struct deferred_open_record *open_rec;
1569 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1570 "open entry for mid %llu\n",
1571 (unsigned int)request_time.tv_sec,
1572 (unsigned int)request_time.tv_usec,
1573 (unsigned long long)req->mid));
1575 open_rec = talloc(NULL, struct deferred_open_record);
1576 if (open_rec == NULL) {
1577 TALLOC_FREE(lck);
1578 exit_server("talloc failed");
1581 *open_rec = *state;
1583 if (lck) {
1584 struct defer_open_state *watch_state;
1585 struct tevent_req *watch_req;
1586 bool ret;
1588 watch_state = talloc(open_rec, struct defer_open_state);
1589 if (watch_state == NULL) {
1590 exit_server("talloc failed");
1592 watch_state->sconn = req->sconn;
1593 watch_state->mid = req->mid;
1595 DEBUG(10, ("defering mid %llu\n",
1596 (unsigned long long)req->mid));
1598 watch_req = dbwrap_record_watch_send(
1599 watch_state, req->sconn->ev_ctx, lck->data->record,
1600 req->sconn->msg_ctx);
1601 if (watch_req == NULL) {
1602 exit_server("Could not watch share mode record");
1604 tevent_req_set_callback(watch_req, defer_open_done,
1605 watch_state);
1607 ret = tevent_req_set_endtime(
1608 watch_req, req->sconn->ev_ctx,
1609 timeval_sum(&request_time, &timeout));
1610 SMB_ASSERT(ret);
1613 if (!push_deferred_open_message_smb(req, request_time, timeout,
1614 state->id, open_rec)) {
1615 TALLOC_FREE(lck);
1616 exit_server("push_deferred_open_message_smb failed");
1620 static void defer_open_done(struct tevent_req *req)
1622 struct defer_open_state *state = tevent_req_callback_data(
1623 req, struct defer_open_state);
1624 NTSTATUS status;
1625 bool ret;
1627 status = dbwrap_record_watch_recv(req, talloc_tos(), NULL);
1628 TALLOC_FREE(req);
1629 if (!NT_STATUS_IS_OK(status)) {
1630 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
1631 nt_errstr(status)));
1633 * Even if it failed, retry anyway. TODO: We need a way to
1634 * tell a re-scheduled open about that error.
1638 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state->mid));
1640 ret = schedule_deferred_open_message_smb(state->sconn, state->mid);
1641 SMB_ASSERT(ret);
1642 TALLOC_FREE(state);
1646 /****************************************************************************
1647 On overwrite open ensure that the attributes match.
1648 ****************************************************************************/
1650 static bool open_match_attributes(connection_struct *conn,
1651 uint32 old_dos_attr,
1652 uint32 new_dos_attr,
1653 mode_t existing_unx_mode,
1654 mode_t new_unx_mode,
1655 mode_t *returned_unx_mode)
1657 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1659 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1660 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1662 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1663 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1664 *returned_unx_mode = new_unx_mode;
1665 } else {
1666 *returned_unx_mode = (mode_t)0;
1669 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1670 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1671 "returned_unx_mode = 0%o\n",
1672 (unsigned int)old_dos_attr,
1673 (unsigned int)existing_unx_mode,
1674 (unsigned int)new_dos_attr,
1675 (unsigned int)*returned_unx_mode ));
1677 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1678 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1679 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1680 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1681 return False;
1684 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1685 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1686 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1687 return False;
1690 return True;
1693 /****************************************************************************
1694 Special FCB or DOS processing in the case of a sharing violation.
1695 Try and find a duplicated file handle.
1696 ****************************************************************************/
1698 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1699 connection_struct *conn,
1700 files_struct *fsp_to_dup_into,
1701 const struct smb_filename *smb_fname,
1702 struct file_id id,
1703 uint16 file_pid,
1704 uint64_t vuid,
1705 uint32 access_mask,
1706 uint32 share_access,
1707 uint32 create_options)
1709 files_struct *fsp;
1711 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1712 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1714 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1715 fsp = file_find_di_next(fsp)) {
1717 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1718 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1719 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1720 fsp->fh->fd, (unsigned long long)fsp->vuid,
1721 (unsigned int)fsp->file_pid,
1722 (unsigned int)fsp->fh->private_options,
1723 (unsigned int)fsp->access_mask ));
1725 if (fsp != fsp_to_dup_into &&
1726 fsp->fh->fd != -1 &&
1727 fsp->vuid == vuid &&
1728 fsp->file_pid == file_pid &&
1729 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1730 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1731 (fsp->access_mask & FILE_WRITE_DATA) &&
1732 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1733 strequal(fsp->fsp_name->stream_name,
1734 smb_fname->stream_name)) {
1735 DEBUG(10,("fcb_or_dos_open: file match\n"));
1736 break;
1740 if (!fsp) {
1741 return NT_STATUS_NOT_FOUND;
1744 /* quite an insane set of semantics ... */
1745 if (is_executable(smb_fname->base_name) &&
1746 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1747 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1748 return NT_STATUS_INVALID_PARAMETER;
1751 /* We need to duplicate this fsp. */
1752 return dup_file_fsp(req, fsp, access_mask, share_access,
1753 create_options, fsp_to_dup_into);
1756 static void schedule_defer_open(struct share_mode_lock *lck,
1757 struct file_id id,
1758 struct timeval request_time,
1759 struct smb_request *req)
1761 struct deferred_open_record state;
1763 /* This is a relative time, added to the absolute
1764 request_time value to get the absolute timeout time.
1765 Note that if this is the second or greater time we enter
1766 this codepath for this particular request mid then
1767 request_time is left as the absolute time of the *first*
1768 time this request mid was processed. This is what allows
1769 the request to eventually time out. */
1771 struct timeval timeout;
1773 /* Normally the smbd we asked should respond within
1774 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1775 * the client did, give twice the timeout as a safety
1776 * measure here in case the other smbd is stuck
1777 * somewhere else. */
1779 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1781 /* Nothing actually uses state.delayed_for_oplocks
1782 but it's handy to differentiate in debug messages
1783 between a 30 second delay due to oplock break, and
1784 a 1 second delay for share mode conflicts. */
1786 state.delayed_for_oplocks = True;
1787 state.async_open = false;
1788 state.id = id;
1790 if (!request_timed_out(request_time, timeout)) {
1791 defer_open(lck, request_time, timeout, req, &state);
1795 /****************************************************************************
1796 Reschedule an open call that went asynchronous.
1797 ****************************************************************************/
1799 static void schedule_async_open(struct timeval request_time,
1800 struct smb_request *req)
1802 struct deferred_open_record state;
1803 struct timeval timeout;
1805 timeout = timeval_set(20, 0);
1807 ZERO_STRUCT(state);
1808 state.delayed_for_oplocks = false;
1809 state.async_open = true;
1811 if (!request_timed_out(request_time, timeout)) {
1812 defer_open(NULL, request_time, timeout, req, &state);
1816 /****************************************************************************
1817 Work out what access_mask to use from what the client sent us.
1818 ****************************************************************************/
1820 static NTSTATUS smbd_calculate_maximum_allowed_access(
1821 connection_struct *conn,
1822 const struct smb_filename *smb_fname,
1823 bool use_privs,
1824 uint32_t *p_access_mask)
1826 struct security_descriptor *sd;
1827 uint32_t access_granted;
1828 NTSTATUS status;
1830 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
1831 *p_access_mask |= FILE_GENERIC_ALL;
1832 return NT_STATUS_OK;
1835 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1836 (SECINFO_OWNER |
1837 SECINFO_GROUP |
1838 SECINFO_DACL),
1839 talloc_tos(), &sd);
1841 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1843 * File did not exist
1845 *p_access_mask = FILE_GENERIC_ALL;
1846 return NT_STATUS_OK;
1848 if (!NT_STATUS_IS_OK(status)) {
1849 DEBUG(10,("Could not get acl on file %s: %s\n",
1850 smb_fname_str_dbg(smb_fname),
1851 nt_errstr(status)));
1852 return NT_STATUS_ACCESS_DENIED;
1856 * If we can access the path to this file, by
1857 * default we have FILE_READ_ATTRIBUTES from the
1858 * containing directory. See the section:
1859 * "Algorithm to Check Access to an Existing File"
1860 * in MS-FSA.pdf.
1862 * se_file_access_check()
1863 * also takes care of owner WRITE_DAC and READ_CONTROL.
1865 status = se_file_access_check(sd,
1866 get_current_nttok(conn),
1867 use_privs,
1868 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1869 &access_granted);
1871 TALLOC_FREE(sd);
1873 if (!NT_STATUS_IS_OK(status)) {
1874 DEBUG(10, ("Access denied on file %s: "
1875 "when calculating maximum access\n",
1876 smb_fname_str_dbg(smb_fname)));
1877 return NT_STATUS_ACCESS_DENIED;
1879 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1881 if (!(access_granted & DELETE_ACCESS)) {
1882 if (can_delete_file_in_directory(conn, smb_fname)) {
1883 *p_access_mask |= DELETE_ACCESS;
1887 return NT_STATUS_OK;
1890 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1891 const struct smb_filename *smb_fname,
1892 bool use_privs,
1893 uint32_t access_mask,
1894 uint32_t *access_mask_out)
1896 NTSTATUS status;
1897 uint32_t orig_access_mask = access_mask;
1898 uint32_t rejected_share_access;
1901 * Convert GENERIC bits to specific bits.
1904 se_map_generic(&access_mask, &file_generic_mapping);
1906 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1907 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1909 status = smbd_calculate_maximum_allowed_access(
1910 conn, smb_fname, use_privs, &access_mask);
1912 if (!NT_STATUS_IS_OK(status)) {
1913 return status;
1916 access_mask &= conn->share_access;
1919 rejected_share_access = access_mask & ~(conn->share_access);
1921 if (rejected_share_access) {
1922 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1923 "file %s: rejected by share access mask[0x%08X] "
1924 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1925 smb_fname_str_dbg(smb_fname),
1926 conn->share_access,
1927 orig_access_mask, access_mask,
1928 rejected_share_access));
1929 return NT_STATUS_ACCESS_DENIED;
1932 *access_mask_out = access_mask;
1933 return NT_STATUS_OK;
1936 /****************************************************************************
1937 Remove the deferred open entry under lock.
1938 ****************************************************************************/
1940 /****************************************************************************
1941 Return true if this is a state pointer to an asynchronous create.
1942 ****************************************************************************/
1944 bool is_deferred_open_async(const struct deferred_open_record *rec)
1946 return rec->async_open;
1949 static bool clear_ads(uint32_t create_disposition)
1951 bool ret = false;
1953 switch (create_disposition) {
1954 case FILE_SUPERSEDE:
1955 case FILE_OVERWRITE_IF:
1956 case FILE_OVERWRITE:
1957 ret = true;
1958 break;
1959 default:
1960 break;
1962 return ret;
1965 static int disposition_to_open_flags(uint32_t create_disposition)
1967 int ret = 0;
1970 * Currently we're using FILE_SUPERSEDE as the same as
1971 * FILE_OVERWRITE_IF but they really are
1972 * different. FILE_SUPERSEDE deletes an existing file
1973 * (requiring delete access) then recreates it.
1976 switch (create_disposition) {
1977 case FILE_SUPERSEDE:
1978 case FILE_OVERWRITE_IF:
1980 * If file exists replace/overwrite. If file doesn't
1981 * exist create.
1983 ret = O_CREAT|O_TRUNC;
1984 break;
1986 case FILE_OPEN:
1988 * If file exists open. If file doesn't exist error.
1990 ret = 0;
1991 break;
1993 case FILE_OVERWRITE:
1995 * If file exists overwrite. If file doesn't exist
1996 * error.
1998 ret = O_TRUNC;
1999 break;
2001 case FILE_CREATE:
2003 * If file exists error. If file doesn't exist create.
2005 ret = O_CREAT|O_EXCL;
2006 break;
2008 case FILE_OPEN_IF:
2010 * If file exists open. If file doesn't exist create.
2012 ret = O_CREAT;
2013 break;
2015 return ret;
2018 static int calculate_open_access_flags(uint32_t access_mask,
2019 int oplock_request,
2020 uint32_t private_flags)
2022 bool need_write, need_read;
2025 * Note that we ignore the append flag as append does not
2026 * mean the same thing under DOS and Unix.
2029 need_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA));
2030 if (!need_write) {
2031 return O_RDONLY;
2034 /* DENY_DOS opens are always underlying read-write on the
2035 file handle, no matter what the requested access mask
2036 says. */
2038 need_read =
2039 ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
2040 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
2041 FILE_READ_EA|FILE_EXECUTE));
2043 if (!need_read) {
2044 return O_WRONLY;
2046 return O_RDWR;
2049 /****************************************************************************
2050 Open a file with a share mode. Passed in an already created files_struct *.
2051 ****************************************************************************/
2053 static NTSTATUS open_file_ntcreate(connection_struct *conn,
2054 struct smb_request *req,
2055 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
2056 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
2057 uint32 create_disposition, /* FILE_OPEN_IF etc. */
2058 uint32 create_options, /* options such as delete on close. */
2059 uint32 new_dos_attributes, /* attributes used for new file. */
2060 int oplock_request, /* internal Samba oplock codes. */
2061 /* Information (FILE_EXISTS etc.) */
2062 uint32_t private_flags, /* Samba specific flags. */
2063 int *pinfo,
2064 files_struct *fsp)
2066 struct smb_filename *smb_fname = fsp->fsp_name;
2067 int flags=0;
2068 int flags2=0;
2069 bool file_existed = VALID_STAT(smb_fname->st);
2070 bool def_acl = False;
2071 bool posix_open = False;
2072 bool new_file_created = False;
2073 bool first_open_attempt = true;
2074 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
2075 mode_t new_unx_mode = (mode_t)0;
2076 mode_t unx_mode = (mode_t)0;
2077 int info;
2078 uint32 existing_dos_attributes = 0;
2079 struct timeval request_time = timeval_zero();
2080 struct share_mode_lock *lck = NULL;
2081 uint32 open_access_mask = access_mask;
2082 NTSTATUS status;
2083 char *parent_dir;
2084 SMB_STRUCT_STAT saved_stat = smb_fname->st;
2085 struct timespec old_write_time;
2086 struct file_id id;
2088 if (conn->printer) {
2090 * Printers are handled completely differently.
2091 * Most of the passed parameters are ignored.
2094 if (pinfo) {
2095 *pinfo = FILE_WAS_CREATED;
2098 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
2099 smb_fname_str_dbg(smb_fname)));
2101 if (!req) {
2102 DEBUG(0,("open_file_ntcreate: printer open without "
2103 "an SMB request!\n"));
2104 return NT_STATUS_INTERNAL_ERROR;
2107 return print_spool_open(fsp, smb_fname->base_name,
2108 req->vuid);
2111 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
2112 NULL)) {
2113 return NT_STATUS_NO_MEMORY;
2116 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2117 posix_open = True;
2118 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2119 new_dos_attributes = 0;
2120 } else {
2121 /* Windows allows a new file to be created and
2122 silently removes a FILE_ATTRIBUTE_DIRECTORY
2123 sent by the client. Do the same. */
2125 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
2127 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2128 * created new. */
2129 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2130 smb_fname, parent_dir);
2133 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2134 "access_mask=0x%x share_access=0x%x "
2135 "create_disposition = 0x%x create_options=0x%x "
2136 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2137 smb_fname_str_dbg(smb_fname), new_dos_attributes,
2138 access_mask, share_access, create_disposition,
2139 create_options, (unsigned int)unx_mode, oplock_request,
2140 (unsigned int)private_flags));
2142 if (req == NULL) {
2143 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
2144 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) != 0));
2145 } else {
2146 /* And req != NULL means no INTERNAL_OPEN_ONLY */
2147 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) == 0));
2151 * Only non-internal opens can be deferred at all
2154 if (req) {
2155 struct deferred_open_record *open_rec;
2156 if (get_deferred_open_message_state(req,
2157 &request_time,
2158 &open_rec)) {
2159 /* Remember the absolute time of the original
2160 request with this mid. We'll use it later to
2161 see if this has timed out. */
2163 /* If it was an async create retry, the file
2164 didn't exist. */
2166 if (is_deferred_open_async(open_rec)) {
2167 SET_STAT_INVALID(smb_fname->st);
2168 file_existed = false;
2171 /* Ensure we don't reprocess this message. */
2172 remove_deferred_open_message_smb(req->sconn, req->mid);
2174 first_open_attempt = false;
2178 if (!posix_open) {
2179 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
2180 if (file_existed) {
2181 existing_dos_attributes = dos_mode(conn, smb_fname);
2185 /* ignore any oplock requests if oplocks are disabled */
2186 if (!lp_oplocks(SNUM(conn)) ||
2187 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
2188 /* Mask off everything except the private Samba bits. */
2189 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2192 /* this is for OS/2 long file names - say we don't support them */
2193 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
2194 /* OS/2 Workplace shell fix may be main code stream in a later
2195 * release. */
2196 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2197 "supported.\n"));
2198 if (use_nt_status()) {
2199 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2201 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2204 switch( create_disposition ) {
2205 case FILE_OPEN:
2206 /* If file exists open. If file doesn't exist error. */
2207 if (!file_existed) {
2208 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2209 "requested for file %s and file "
2210 "doesn't exist.\n",
2211 smb_fname_str_dbg(smb_fname)));
2212 errno = ENOENT;
2213 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2215 break;
2217 case FILE_OVERWRITE:
2218 /* If file exists overwrite. If file doesn't exist
2219 * error. */
2220 if (!file_existed) {
2221 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2222 "requested for file %s and file "
2223 "doesn't exist.\n",
2224 smb_fname_str_dbg(smb_fname) ));
2225 errno = ENOENT;
2226 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2228 break;
2230 case FILE_CREATE:
2231 /* If file exists error. If file doesn't exist
2232 * create. */
2233 if (file_existed) {
2234 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2235 "requested for file %s and file "
2236 "already exists.\n",
2237 smb_fname_str_dbg(smb_fname)));
2238 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2239 errno = EISDIR;
2240 } else {
2241 errno = EEXIST;
2243 return map_nt_error_from_unix(errno);
2245 break;
2247 case FILE_SUPERSEDE:
2248 case FILE_OVERWRITE_IF:
2249 case FILE_OPEN_IF:
2250 break;
2251 default:
2252 return NT_STATUS_INVALID_PARAMETER;
2255 flags2 = disposition_to_open_flags(create_disposition);
2257 /* We only care about matching attributes on file exists and
2258 * overwrite. */
2260 if (!posix_open && file_existed &&
2261 ((create_disposition == FILE_OVERWRITE) ||
2262 (create_disposition == FILE_OVERWRITE_IF))) {
2263 if (!open_match_attributes(conn, existing_dos_attributes,
2264 new_dos_attributes,
2265 smb_fname->st.st_ex_mode,
2266 unx_mode, &new_unx_mode)) {
2267 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2268 "for file %s (%x %x) (0%o, 0%o)\n",
2269 smb_fname_str_dbg(smb_fname),
2270 existing_dos_attributes,
2271 new_dos_attributes,
2272 (unsigned int)smb_fname->st.st_ex_mode,
2273 (unsigned int)unx_mode ));
2274 errno = EACCES;
2275 return NT_STATUS_ACCESS_DENIED;
2279 status = smbd_calculate_access_mask(conn, smb_fname,
2280 false,
2281 access_mask,
2282 &access_mask);
2283 if (!NT_STATUS_IS_OK(status)) {
2284 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2285 "on file %s returned %s\n",
2286 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2287 return status;
2290 open_access_mask = access_mask;
2292 if (flags2 & O_TRUNC) {
2293 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2296 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2297 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2298 access_mask));
2301 * Note that we ignore the append flag as append does not
2302 * mean the same thing under DOS and Unix.
2305 flags = calculate_open_access_flags(access_mask, oplock_request,
2306 private_flags);
2309 * Currently we only look at FILE_WRITE_THROUGH for create options.
2312 #if defined(O_SYNC)
2313 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2314 flags2 |= O_SYNC;
2316 #endif /* O_SYNC */
2318 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2319 flags2 |= O_APPEND;
2322 if (!posix_open && !CAN_WRITE(conn)) {
2324 * We should really return a permission denied error if either
2325 * O_CREAT or O_TRUNC are set, but for compatibility with
2326 * older versions of Samba we just AND them out.
2328 flags2 &= ~(O_CREAT|O_TRUNC);
2331 if (first_open_attempt && lp_kernel_oplocks(SNUM(conn))) {
2333 * With kernel oplocks the open breaking an oplock
2334 * blocks until the oplock holder has given up the
2335 * oplock or closed the file. We prevent this by first
2336 * trying to open the file with O_NONBLOCK (see "man
2337 * fcntl" on Linux). For the second try, triggered by
2338 * an oplock break response, we do not need this
2339 * anymore.
2341 * This is true under the assumption that only Samba
2342 * requests kernel oplocks. Once someone else like
2343 * NFSv4 starts to use that API, we will have to
2344 * modify this by communicating with the NFSv4 server.
2346 flags2 |= O_NONBLOCK;
2350 * Ensure we can't write on a read-only share or file.
2353 if (flags != O_RDONLY && file_existed &&
2354 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2355 DEBUG(5,("open_file_ntcreate: write access requested for "
2356 "file %s on read only %s\n",
2357 smb_fname_str_dbg(smb_fname),
2358 !CAN_WRITE(conn) ? "share" : "file" ));
2359 errno = EACCES;
2360 return NT_STATUS_ACCESS_DENIED;
2363 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2364 fsp->share_access = share_access;
2365 fsp->fh->private_options = private_flags;
2366 fsp->access_mask = open_access_mask; /* We change this to the
2367 * requested access_mask after
2368 * the open is done. */
2369 fsp->posix_open = posix_open;
2371 /* Ensure no SAMBA_PRIVATE bits can be set. */
2372 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2374 if (timeval_is_zero(&request_time)) {
2375 request_time = fsp->open_time;
2379 * Ensure we pay attention to default ACLs on directories if required.
2382 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2383 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2384 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2387 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2388 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2389 (unsigned int)flags, (unsigned int)flags2,
2390 (unsigned int)unx_mode, (unsigned int)access_mask,
2391 (unsigned int)open_access_mask));
2393 fsp_open = open_file(fsp, conn, req, parent_dir,
2394 flags|flags2, unx_mode, access_mask,
2395 open_access_mask, &new_file_created);
2397 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_NETWORK_BUSY)) {
2398 struct deferred_open_record state;
2401 * EWOULDBLOCK/EAGAIN maps to NETWORK_BUSY.
2403 if (file_existed && S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
2404 DEBUG(10, ("FIFO busy\n"));
2405 return NT_STATUS_NETWORK_BUSY;
2407 if (req == NULL) {
2408 DEBUG(10, ("Internal open busy\n"));
2409 return NT_STATUS_NETWORK_BUSY;
2413 * From here on we assume this is an oplock break triggered
2416 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
2417 if (lck == NULL) {
2418 state.delayed_for_oplocks = false;
2419 state.async_open = false;
2420 state.id = fsp->file_id;
2421 defer_open(NULL, request_time, timeval_set(0, 0),
2422 req, &state);
2423 DEBUG(10, ("No share mode lock found after "
2424 "EWOULDBLOCK, retrying sync\n"));
2425 return NT_STATUS_SHARING_VIOLATION;
2428 if (!validate_oplock_types(lck)) {
2429 smb_panic("validate_oplock_types failed");
2432 if (delay_for_oplock(fsp, 0, lck, false, create_disposition)) {
2433 schedule_defer_open(lck, fsp->file_id, request_time, req);
2434 TALLOC_FREE(lck);
2435 DEBUG(10, ("Sent oplock break request to kernel "
2436 "oplock holder\n"));
2437 return NT_STATUS_SHARING_VIOLATION;
2441 * No oplock from Samba around. Immediately retry with
2442 * a blocking open.
2444 state.delayed_for_oplocks = false;
2445 state.async_open = false;
2446 state.id = fsp->file_id;
2447 defer_open(lck, request_time, timeval_set(0, 0), req, &state);
2448 TALLOC_FREE(lck);
2449 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2450 "Retrying sync\n"));
2451 return NT_STATUS_SHARING_VIOLATION;
2454 if (!NT_STATUS_IS_OK(fsp_open)) {
2455 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2456 schedule_async_open(request_time, req);
2458 return fsp_open;
2461 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2463 * The file did exist, but some other (local or NFS)
2464 * process either renamed/unlinked and re-created the
2465 * file with different dev/ino after we walked the path,
2466 * but before we did the open. We could retry the
2467 * open but it's a rare enough case it's easier to
2468 * just fail the open to prevent creating any problems
2469 * in the open file db having the wrong dev/ino key.
2471 fd_close(fsp);
2472 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2473 "Old (dev=0x%llu, ino =0x%llu). "
2474 "New (dev=0x%llu, ino=0x%llu). Failing open "
2475 " with NT_STATUS_ACCESS_DENIED.\n",
2476 smb_fname_str_dbg(smb_fname),
2477 (unsigned long long)saved_stat.st_ex_dev,
2478 (unsigned long long)saved_stat.st_ex_ino,
2479 (unsigned long long)smb_fname->st.st_ex_dev,
2480 (unsigned long long)smb_fname->st.st_ex_ino));
2481 return NT_STATUS_ACCESS_DENIED;
2484 old_write_time = smb_fname->st.st_ex_mtime;
2487 * Deal with the race condition where two smbd's detect the
2488 * file doesn't exist and do the create at the same time. One
2489 * of them will win and set a share mode, the other (ie. this
2490 * one) should check if the requested share mode for this
2491 * create is allowed.
2495 * Now the file exists and fsp is successfully opened,
2496 * fsp->dev and fsp->inode are valid and should replace the
2497 * dev=0,inode=0 from a non existent file. Spotted by
2498 * Nadav Danieli <nadavd@exanet.com>. JRA.
2501 id = fsp->file_id;
2503 lck = get_share_mode_lock(talloc_tos(), id,
2504 conn->connectpath,
2505 smb_fname, &old_write_time);
2507 if (lck == NULL) {
2508 DEBUG(0, ("open_file_ntcreate: Could not get share "
2509 "mode lock for %s\n",
2510 smb_fname_str_dbg(smb_fname)));
2511 fd_close(fsp);
2512 return NT_STATUS_SHARING_VIOLATION;
2515 /* Get the types we need to examine. */
2516 if (!validate_oplock_types(lck)) {
2517 smb_panic("validate_oplock_types failed");
2520 if (has_delete_on_close(lck, fsp->name_hash)) {
2521 TALLOC_FREE(lck);
2522 fd_close(fsp);
2523 return NT_STATUS_DELETE_PENDING;
2526 status = open_mode_check(conn, lck,
2527 access_mask, share_access);
2529 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION) ||
2530 (lck->data->num_share_modes > 0)) {
2532 * This comes from ancient times out of open_mode_check. I
2533 * have no clue whether this is still necessary. I can't think
2534 * of a case where this would actually matter further down in
2535 * this function. I leave it here for further investigation
2536 * :-)
2538 file_existed = true;
2541 if ((req != NULL) &&
2542 delay_for_oplock(
2543 fsp, oplock_request, lck,
2544 NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION),
2545 create_disposition)) {
2546 schedule_defer_open(lck, fsp->file_id, request_time, req);
2547 TALLOC_FREE(lck);
2548 fd_close(fsp);
2549 return NT_STATUS_SHARING_VIOLATION;
2552 if (!NT_STATUS_IS_OK(status)) {
2553 uint32 can_access_mask;
2554 bool can_access = True;
2556 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2558 /* Check if this can be done with the deny_dos and fcb
2559 * calls. */
2560 if (private_flags &
2561 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2562 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2563 if (req == NULL) {
2564 DEBUG(0, ("DOS open without an SMB "
2565 "request!\n"));
2566 TALLOC_FREE(lck);
2567 fd_close(fsp);
2568 return NT_STATUS_INTERNAL_ERROR;
2571 /* Use the client requested access mask here,
2572 * not the one we open with. */
2573 status = fcb_or_dos_open(req,
2574 conn,
2575 fsp,
2576 smb_fname,
2578 req->smbpid,
2579 req->vuid,
2580 access_mask,
2581 share_access,
2582 create_options);
2584 if (NT_STATUS_IS_OK(status)) {
2585 TALLOC_FREE(lck);
2586 if (pinfo) {
2587 *pinfo = FILE_WAS_OPENED;
2589 return NT_STATUS_OK;
2594 * This next line is a subtlety we need for
2595 * MS-Access. If a file open will fail due to share
2596 * permissions and also for security (access) reasons,
2597 * we need to return the access failed error, not the
2598 * share error. We can't open the file due to kernel
2599 * oplock deadlock (it's possible we failed above on
2600 * the open_mode_check()) so use a userspace check.
2603 if (flags & O_RDWR) {
2604 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2605 } else if (flags & O_WRONLY) {
2606 can_access_mask = FILE_WRITE_DATA;
2607 } else {
2608 can_access_mask = FILE_READ_DATA;
2611 if (((can_access_mask & FILE_WRITE_DATA) &&
2612 !CAN_WRITE(conn)) ||
2613 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2614 smb_fname,
2615 false,
2616 can_access_mask))) {
2617 can_access = False;
2621 * If we're returning a share violation, ensure we
2622 * cope with the braindead 1 second delay (SMB1 only).
2625 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2626 !conn->sconn->using_smb2 &&
2627 lp_defer_sharing_violations()) {
2628 struct timeval timeout;
2629 struct deferred_open_record state;
2630 int timeout_usecs;
2632 /* this is a hack to speed up torture tests
2633 in 'make test' */
2634 timeout_usecs = lp_parm_int(SNUM(conn),
2635 "smbd","sharedelay",
2636 SHARING_VIOLATION_USEC_WAIT);
2638 /* This is a relative time, added to the absolute
2639 request_time value to get the absolute timeout time.
2640 Note that if this is the second or greater time we enter
2641 this codepath for this particular request mid then
2642 request_time is left as the absolute time of the *first*
2643 time this request mid was processed. This is what allows
2644 the request to eventually time out. */
2646 timeout = timeval_set(0, timeout_usecs);
2648 /* Nothing actually uses state.delayed_for_oplocks
2649 but it's handy to differentiate in debug messages
2650 between a 30 second delay due to oplock break, and
2651 a 1 second delay for share mode conflicts. */
2653 state.delayed_for_oplocks = False;
2654 state.async_open = false;
2655 state.id = id;
2657 if ((req != NULL)
2658 && !request_timed_out(request_time,
2659 timeout)) {
2660 defer_open(lck, request_time, timeout,
2661 req, &state);
2665 TALLOC_FREE(lck);
2666 fd_close(fsp);
2667 if (can_access) {
2669 * We have detected a sharing violation here
2670 * so return the correct error code
2672 status = NT_STATUS_SHARING_VIOLATION;
2673 } else {
2674 status = NT_STATUS_ACCESS_DENIED;
2676 return status;
2679 grant_fsp_oplock_type(fsp, lck, oplock_request);
2682 * We have the share entry *locked*.....
2685 /* Delete streams if create_disposition requires it */
2686 if (!new_file_created && clear_ads(create_disposition) &&
2687 !is_ntfs_stream_smb_fname(smb_fname)) {
2688 status = delete_all_streams(conn, smb_fname->base_name);
2689 if (!NT_STATUS_IS_OK(status)) {
2690 TALLOC_FREE(lck);
2691 fd_close(fsp);
2692 return status;
2696 /* note that we ignore failure for the following. It is
2697 basically a hack for NFS, and NFS will never set one of
2698 these only read them. Nobody but Samba can ever set a deny
2699 mode and we have already checked our more authoritative
2700 locking database for permission to set this deny mode. If
2701 the kernel refuses the operations then the kernel is wrong.
2702 note that GPFS supports it as well - jmcd */
2704 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
2705 int ret_flock;
2706 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2707 if(ret_flock == -1 ){
2709 TALLOC_FREE(lck);
2710 fd_close(fsp);
2712 return NT_STATUS_SHARING_VIOLATION;
2717 * At this point onwards, we can guarantee that the share entry
2718 * is locked, whether we created the file or not, and that the
2719 * deny mode is compatible with all current opens.
2723 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2724 * but we don't have to store this - just ignore it on access check.
2726 if (conn->sconn->using_smb2) {
2728 * SMB2 doesn't return it (according to Microsoft tests).
2729 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2730 * File created with access = 0x7 (Read, Write, Delete)
2731 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2733 fsp->access_mask = access_mask;
2734 } else {
2735 /* But SMB1 does. */
2736 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2739 if (file_existed) {
2740 /* stat opens on existing files don't get oplocks. */
2741 if (is_stat_open(open_access_mask)) {
2742 fsp->oplock_type = NO_OPLOCK;
2746 if (new_file_created) {
2747 info = FILE_WAS_CREATED;
2748 } else {
2749 if (flags2 & O_TRUNC) {
2750 info = FILE_WAS_OVERWRITTEN;
2751 } else {
2752 info = FILE_WAS_OPENED;
2756 if (pinfo) {
2757 *pinfo = info;
2761 * Setup the oplock info in both the shared memory and
2762 * file structs.
2765 status = set_file_oplock(fsp);
2766 if (!NT_STATUS_IS_OK(status)) {
2768 * Could not get the kernel oplock
2770 fsp->oplock_type = NO_OPLOCK;
2773 if (!set_share_mode(lck, fsp, get_current_uid(conn),
2774 req ? req->mid : 0,
2775 fsp->oplock_type)) {
2776 TALLOC_FREE(lck);
2777 fd_close(fsp);
2778 return NT_STATUS_NO_MEMORY;
2781 /* Handle strange delete on close create semantics. */
2782 if (create_options & FILE_DELETE_ON_CLOSE) {
2784 status = can_set_delete_on_close(fsp, new_dos_attributes);
2786 if (!NT_STATUS_IS_OK(status)) {
2787 /* Remember to delete the mode we just added. */
2788 del_share_mode(lck, fsp);
2789 TALLOC_FREE(lck);
2790 fd_close(fsp);
2791 return status;
2793 /* Note that here we set the *inital* delete on close flag,
2794 not the regular one. The magic gets handled in close. */
2795 fsp->initial_delete_on_close = True;
2798 if (info != FILE_WAS_OPENED) {
2799 /* Files should be initially set as archive */
2800 if (lp_map_archive(SNUM(conn)) ||
2801 lp_store_dos_attributes(SNUM(conn))) {
2802 if (!posix_open) {
2803 if (file_set_dosmode(conn, smb_fname,
2804 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2805 parent_dir, true) == 0) {
2806 unx_mode = smb_fname->st.st_ex_mode;
2812 /* Determine sparse flag. */
2813 if (posix_open) {
2814 /* POSIX opens are sparse by default. */
2815 fsp->is_sparse = true;
2816 } else {
2817 fsp->is_sparse = (file_existed &&
2818 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2822 * Take care of inherited ACLs on created files - if default ACL not
2823 * selected.
2826 if (!posix_open && new_file_created && !def_acl) {
2828 int saved_errno = errno; /* We might get ENOSYS in the next
2829 * call.. */
2831 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2832 errno == ENOSYS) {
2833 errno = saved_errno; /* Ignore ENOSYS */
2836 } else if (new_unx_mode) {
2838 int ret = -1;
2840 /* Attributes need changing. File already existed. */
2843 int saved_errno = errno; /* We might get ENOSYS in the
2844 * next call.. */
2845 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2847 if (ret == -1 && errno == ENOSYS) {
2848 errno = saved_errno; /* Ignore ENOSYS */
2849 } else {
2850 DEBUG(5, ("open_file_ntcreate: reset "
2851 "attributes of file %s to 0%o\n",
2852 smb_fname_str_dbg(smb_fname),
2853 (unsigned int)new_unx_mode));
2854 ret = 0; /* Don't do the fchmod below. */
2858 if ((ret == -1) &&
2859 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2860 DEBUG(5, ("open_file_ntcreate: failed to reset "
2861 "attributes of file %s to 0%o\n",
2862 smb_fname_str_dbg(smb_fname),
2863 (unsigned int)new_unx_mode));
2868 * Deal with other opens having a modified write time.
2870 struct timespec write_time = get_share_mode_write_time(lck);
2872 if (!null_timespec(write_time)) {
2873 update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
2877 TALLOC_FREE(lck);
2879 return NT_STATUS_OK;
2882 static NTSTATUS mkdir_internal(connection_struct *conn,
2883 struct smb_filename *smb_dname,
2884 uint32 file_attributes)
2886 mode_t mode;
2887 char *parent_dir = NULL;
2888 NTSTATUS status;
2889 bool posix_open = false;
2890 bool need_re_stat = false;
2891 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2893 if (!CAN_WRITE(conn) || (access_mask & ~(conn->share_access))) {
2894 DEBUG(5,("mkdir_internal: failing share access "
2895 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
2896 return NT_STATUS_ACCESS_DENIED;
2899 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2900 NULL)) {
2901 return NT_STATUS_NO_MEMORY;
2904 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2905 posix_open = true;
2906 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2907 } else {
2908 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2911 status = check_parent_access(conn,
2912 smb_dname,
2913 access_mask);
2914 if(!NT_STATUS_IS_OK(status)) {
2915 DEBUG(5,("mkdir_internal: check_parent_access "
2916 "on directory %s for path %s returned %s\n",
2917 parent_dir,
2918 smb_dname->base_name,
2919 nt_errstr(status) ));
2920 return status;
2923 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2924 return map_nt_error_from_unix(errno);
2927 /* Ensure we're checking for a symlink here.... */
2928 /* We don't want to get caught by a symlink racer. */
2930 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2931 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2932 smb_fname_str_dbg(smb_dname), strerror(errno)));
2933 return map_nt_error_from_unix(errno);
2936 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2937 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2938 smb_fname_str_dbg(smb_dname)));
2939 return NT_STATUS_NOT_A_DIRECTORY;
2942 if (lp_store_dos_attributes(SNUM(conn))) {
2943 if (!posix_open) {
2944 file_set_dosmode(conn, smb_dname,
2945 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2946 parent_dir, true);
2950 if (lp_inherit_permissions(SNUM(conn))) {
2951 inherit_access_posix_acl(conn, parent_dir,
2952 smb_dname->base_name, mode);
2953 need_re_stat = true;
2956 if (!posix_open) {
2958 * Check if high bits should have been set,
2959 * then (if bits are missing): add them.
2960 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2961 * dir.
2963 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2964 (mode & ~smb_dname->st.st_ex_mode)) {
2965 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2966 (smb_dname->st.st_ex_mode |
2967 (mode & ~smb_dname->st.st_ex_mode)));
2968 need_re_stat = true;
2972 /* Change the owner if required. */
2973 if (lp_inherit_owner(SNUM(conn))) {
2974 change_dir_owner_to_parent(conn, parent_dir,
2975 smb_dname->base_name,
2976 &smb_dname->st);
2977 need_re_stat = true;
2980 if (need_re_stat) {
2981 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2982 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2983 smb_fname_str_dbg(smb_dname), strerror(errno)));
2984 return map_nt_error_from_unix(errno);
2988 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2989 smb_dname->base_name);
2991 return NT_STATUS_OK;
2994 /****************************************************************************
2995 Open a directory from an NT SMB call.
2996 ****************************************************************************/
2998 static NTSTATUS open_directory(connection_struct *conn,
2999 struct smb_request *req,
3000 struct smb_filename *smb_dname,
3001 uint32 access_mask,
3002 uint32 share_access,
3003 uint32 create_disposition,
3004 uint32 create_options,
3005 uint32 file_attributes,
3006 int *pinfo,
3007 files_struct **result)
3009 files_struct *fsp = NULL;
3010 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
3011 struct share_mode_lock *lck = NULL;
3012 NTSTATUS status;
3013 struct timespec mtimespec;
3014 int info = 0;
3016 if (is_ntfs_stream_smb_fname(smb_dname)) {
3017 DEBUG(2, ("open_directory: %s is a stream name!\n",
3018 smb_fname_str_dbg(smb_dname)));
3019 return NT_STATUS_NOT_A_DIRECTORY;
3022 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
3023 /* Ensure we have a directory attribute. */
3024 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
3027 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
3028 "share_access = 0x%x create_options = 0x%x, "
3029 "create_disposition = 0x%x, file_attributes = 0x%x\n",
3030 smb_fname_str_dbg(smb_dname),
3031 (unsigned int)access_mask,
3032 (unsigned int)share_access,
3033 (unsigned int)create_options,
3034 (unsigned int)create_disposition,
3035 (unsigned int)file_attributes));
3037 status = smbd_calculate_access_mask(conn, smb_dname, false,
3038 access_mask, &access_mask);
3039 if (!NT_STATUS_IS_OK(status)) {
3040 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
3041 "on file %s returned %s\n",
3042 smb_fname_str_dbg(smb_dname),
3043 nt_errstr(status)));
3044 return status;
3047 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3048 !security_token_has_privilege(get_current_nttok(conn),
3049 SEC_PRIV_SECURITY)) {
3050 DEBUG(10, ("open_directory: open on %s "
3051 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3052 smb_fname_str_dbg(smb_dname)));
3053 return NT_STATUS_PRIVILEGE_NOT_HELD;
3056 switch( create_disposition ) {
3057 case FILE_OPEN:
3059 if (!dir_existed) {
3060 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3063 info = FILE_WAS_OPENED;
3064 break;
3066 case FILE_CREATE:
3068 /* If directory exists error. If directory doesn't
3069 * exist create. */
3071 if (dir_existed) {
3072 status = NT_STATUS_OBJECT_NAME_COLLISION;
3073 DEBUG(2, ("open_directory: unable to create "
3074 "%s. Error was %s\n",
3075 smb_fname_str_dbg(smb_dname),
3076 nt_errstr(status)));
3077 return status;
3080 status = mkdir_internal(conn, smb_dname,
3081 file_attributes);
3083 if (!NT_STATUS_IS_OK(status)) {
3084 DEBUG(2, ("open_directory: unable to create "
3085 "%s. Error was %s\n",
3086 smb_fname_str_dbg(smb_dname),
3087 nt_errstr(status)));
3088 return status;
3091 info = FILE_WAS_CREATED;
3092 break;
3094 case FILE_OPEN_IF:
3096 * If directory exists open. If directory doesn't
3097 * exist create.
3100 if (dir_existed) {
3101 status = NT_STATUS_OK;
3102 info = FILE_WAS_OPENED;
3103 } else {
3104 status = mkdir_internal(conn, smb_dname,
3105 file_attributes);
3107 if (NT_STATUS_IS_OK(status)) {
3108 info = FILE_WAS_CREATED;
3109 } else {
3110 /* Cope with create race. */
3111 if (!NT_STATUS_EQUAL(status,
3112 NT_STATUS_OBJECT_NAME_COLLISION)) {
3113 DEBUG(2, ("open_directory: unable to create "
3114 "%s. Error was %s\n",
3115 smb_fname_str_dbg(smb_dname),
3116 nt_errstr(status)));
3117 return status;
3119 info = FILE_WAS_OPENED;
3123 break;
3125 case FILE_SUPERSEDE:
3126 case FILE_OVERWRITE:
3127 case FILE_OVERWRITE_IF:
3128 default:
3129 DEBUG(5,("open_directory: invalid create_disposition "
3130 "0x%x for directory %s\n",
3131 (unsigned int)create_disposition,
3132 smb_fname_str_dbg(smb_dname)));
3133 return NT_STATUS_INVALID_PARAMETER;
3136 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3137 DEBUG(5,("open_directory: %s is not a directory !\n",
3138 smb_fname_str_dbg(smb_dname)));
3139 return NT_STATUS_NOT_A_DIRECTORY;
3142 if (info == FILE_WAS_OPENED) {
3143 status = smbd_check_access_rights(conn,
3144 smb_dname,
3145 false,
3146 access_mask);
3147 if (!NT_STATUS_IS_OK(status)) {
3148 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3149 "file %s failed with %s\n",
3150 smb_fname_str_dbg(smb_dname),
3151 nt_errstr(status)));
3152 return status;
3156 status = file_new(req, conn, &fsp);
3157 if(!NT_STATUS_IS_OK(status)) {
3158 return status;
3162 * Setup the files_struct for it.
3165 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3166 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3167 fsp->file_pid = req ? req->smbpid : 0;
3168 fsp->can_lock = False;
3169 fsp->can_read = False;
3170 fsp->can_write = False;
3172 fsp->share_access = share_access;
3173 fsp->fh->private_options = 0;
3175 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3177 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3178 fsp->print_file = NULL;
3179 fsp->modified = False;
3180 fsp->oplock_type = NO_OPLOCK;
3181 fsp->sent_oplock_break = NO_BREAK_SENT;
3182 fsp->is_directory = True;
3183 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
3184 status = fsp_set_smb_fname(fsp, smb_dname);
3185 if (!NT_STATUS_IS_OK(status)) {
3186 file_free(req, fsp);
3187 return status;
3190 /* Don't store old timestamps for directory
3191 handles in the internal database. We don't
3192 update them in there if new objects
3193 are creaded in the directory. Currently
3194 we only update timestamps on file writes.
3195 See bug #9870.
3197 ZERO_STRUCT(mtimespec);
3199 if (access_mask & (FILE_LIST_DIRECTORY|
3200 FILE_ADD_FILE|
3201 FILE_ADD_SUBDIRECTORY|
3202 FILE_TRAVERSE|
3203 DELETE_ACCESS|
3204 FILE_DELETE_CHILD)) {
3205 #ifdef O_DIRECTORY
3206 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3207 #else
3208 /* POSIX allows us to open a directory with O_RDONLY. */
3209 status = fd_open(conn, fsp, O_RDONLY, 0);
3210 #endif
3211 if (!NT_STATUS_IS_OK(status)) {
3212 DEBUG(5, ("open_directory: Could not open fd for "
3213 "%s (%s)\n",
3214 smb_fname_str_dbg(smb_dname),
3215 nt_errstr(status)));
3216 file_free(req, fsp);
3217 return status;
3219 } else {
3220 fsp->fh->fd = -1;
3221 DEBUG(10, ("Not opening Directory %s\n",
3222 smb_fname_str_dbg(smb_dname)));
3225 status = vfs_stat_fsp(fsp);
3226 if (!NT_STATUS_IS_OK(status)) {
3227 fd_close(fsp);
3228 file_free(req, fsp);
3229 return status;
3232 /* Ensure there was no race condition. */
3233 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
3234 DEBUG(5,("open_directory: stat struct differs for "
3235 "directory %s.\n",
3236 smb_fname_str_dbg(smb_dname)));
3237 fd_close(fsp);
3238 file_free(req, fsp);
3239 return NT_STATUS_ACCESS_DENIED;
3242 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3243 conn->connectpath, smb_dname,
3244 &mtimespec);
3246 if (lck == NULL) {
3247 DEBUG(0, ("open_directory: Could not get share mode lock for "
3248 "%s\n", smb_fname_str_dbg(smb_dname)));
3249 fd_close(fsp);
3250 file_free(req, fsp);
3251 return NT_STATUS_SHARING_VIOLATION;
3254 if (has_delete_on_close(lck, fsp->name_hash)) {
3255 TALLOC_FREE(lck);
3256 fd_close(fsp);
3257 file_free(req, fsp);
3258 return NT_STATUS_DELETE_PENDING;
3261 status = open_mode_check(conn, lck,
3262 access_mask, share_access);
3264 if (!NT_STATUS_IS_OK(status)) {
3265 TALLOC_FREE(lck);
3266 fd_close(fsp);
3267 file_free(req, fsp);
3268 return status;
3271 if (!set_share_mode(lck, fsp, get_current_uid(conn),
3272 req ? req->mid : 0, NO_OPLOCK)) {
3273 TALLOC_FREE(lck);
3274 fd_close(fsp);
3275 file_free(req, fsp);
3276 return NT_STATUS_NO_MEMORY;
3279 /* For directories the delete on close bit at open time seems
3280 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3281 if (create_options & FILE_DELETE_ON_CLOSE) {
3282 status = can_set_delete_on_close(fsp, 0);
3283 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3284 del_share_mode(lck, fsp);
3285 TALLOC_FREE(lck);
3286 fd_close(fsp);
3287 file_free(req, fsp);
3288 return status;
3291 if (NT_STATUS_IS_OK(status)) {
3292 /* Note that here we set the *inital* delete on close flag,
3293 not the regular one. The magic gets handled in close. */
3294 fsp->initial_delete_on_close = True;
3300 * Deal with other opens having a modified write time. Is this
3301 * possible for directories?
3303 struct timespec write_time = get_share_mode_write_time(lck);
3305 if (!null_timespec(write_time)) {
3306 update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
3310 TALLOC_FREE(lck);
3312 if (pinfo) {
3313 *pinfo = info;
3316 *result = fsp;
3317 return NT_STATUS_OK;
3320 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3321 struct smb_filename *smb_dname)
3323 NTSTATUS status;
3324 files_struct *fsp;
3326 status = SMB_VFS_CREATE_FILE(
3327 conn, /* conn */
3328 req, /* req */
3329 0, /* root_dir_fid */
3330 smb_dname, /* fname */
3331 FILE_READ_ATTRIBUTES, /* access_mask */
3332 FILE_SHARE_NONE, /* share_access */
3333 FILE_CREATE, /* create_disposition*/
3334 FILE_DIRECTORY_FILE, /* create_options */
3335 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3336 0, /* oplock_request */
3337 0, /* allocation_size */
3338 0, /* private_flags */
3339 NULL, /* sd */
3340 NULL, /* ea_list */
3341 &fsp, /* result */
3342 NULL); /* pinfo */
3344 if (NT_STATUS_IS_OK(status)) {
3345 close_file(req, fsp, NORMAL_CLOSE);
3348 return status;
3351 /****************************************************************************
3352 Receive notification that one of our open files has been renamed by another
3353 smbd process.
3354 ****************************************************************************/
3356 void msg_file_was_renamed(struct messaging_context *msg,
3357 void *private_data,
3358 uint32_t msg_type,
3359 struct server_id server_id,
3360 DATA_BLOB *data)
3362 files_struct *fsp;
3363 char *frm = (char *)data->data;
3364 struct file_id id;
3365 const char *sharepath;
3366 const char *base_name;
3367 const char *stream_name;
3368 struct smb_filename *smb_fname = NULL;
3369 size_t sp_len, bn_len;
3370 NTSTATUS status;
3371 struct smbd_server_connection *sconn =
3372 talloc_get_type_abort(private_data,
3373 struct smbd_server_connection);
3375 if (data->data == NULL
3376 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3377 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3378 (int)data->length));
3379 return;
3382 /* Unpack the message. */
3383 pull_file_id_24(frm, &id);
3384 sharepath = &frm[24];
3385 sp_len = strlen(sharepath);
3386 base_name = sharepath + sp_len + 1;
3387 bn_len = strlen(base_name);
3388 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3390 /* stream_name must always be NULL if there is no stream. */
3391 if (stream_name[0] == '\0') {
3392 stream_name = NULL;
3395 smb_fname = synthetic_smb_fname(talloc_tos(), base_name,
3396 stream_name, NULL);
3397 if (smb_fname == NULL) {
3398 return;
3401 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3402 "file_id %s\n",
3403 sharepath, smb_fname_str_dbg(smb_fname),
3404 file_id_string_tos(&id)));
3406 for(fsp = file_find_di_first(sconn, id); fsp;
3407 fsp = file_find_di_next(fsp)) {
3408 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3410 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3411 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3412 smb_fname_str_dbg(smb_fname)));
3413 status = fsp_set_smb_fname(fsp, smb_fname);
3414 if (!NT_STATUS_IS_OK(status)) {
3415 goto out;
3417 } else {
3418 /* TODO. JRA. */
3419 /* Now we have the complete path we can work out if this is
3420 actually within this share and adjust newname accordingly. */
3421 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3422 "not sharepath %s) "
3423 "%s from %s -> %s\n",
3424 fsp->conn->connectpath,
3425 sharepath,
3426 fsp_fnum_dbg(fsp),
3427 fsp_str_dbg(fsp),
3428 smb_fname_str_dbg(smb_fname)));
3431 out:
3432 TALLOC_FREE(smb_fname);
3433 return;
3437 * If a main file is opened for delete, all streams need to be checked for
3438 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3439 * If that works, delete them all by setting the delete on close and close.
3442 NTSTATUS open_streams_for_delete(connection_struct *conn,
3443 const char *fname)
3445 struct stream_struct *stream_info = NULL;
3446 files_struct **streams = NULL;
3447 int i;
3448 unsigned int num_streams = 0;
3449 TALLOC_CTX *frame = talloc_stackframe();
3450 NTSTATUS status;
3452 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3453 &num_streams, &stream_info);
3455 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3456 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3457 DEBUG(10, ("no streams around\n"));
3458 TALLOC_FREE(frame);
3459 return NT_STATUS_OK;
3462 if (!NT_STATUS_IS_OK(status)) {
3463 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3464 nt_errstr(status)));
3465 goto fail;
3468 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3469 num_streams));
3471 if (num_streams == 0) {
3472 TALLOC_FREE(frame);
3473 return NT_STATUS_OK;
3476 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3477 if (streams == NULL) {
3478 DEBUG(0, ("talloc failed\n"));
3479 status = NT_STATUS_NO_MEMORY;
3480 goto fail;
3483 for (i=0; i<num_streams; i++) {
3484 struct smb_filename *smb_fname;
3486 if (strequal(stream_info[i].name, "::$DATA")) {
3487 streams[i] = NULL;
3488 continue;
3491 smb_fname = synthetic_smb_fname(
3492 talloc_tos(), fname, stream_info[i].name, NULL);
3493 if (smb_fname == NULL) {
3494 status = NT_STATUS_NO_MEMORY;
3495 goto fail;
3498 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3499 DEBUG(10, ("Unable to stat stream: %s\n",
3500 smb_fname_str_dbg(smb_fname)));
3503 status = SMB_VFS_CREATE_FILE(
3504 conn, /* conn */
3505 NULL, /* req */
3506 0, /* root_dir_fid */
3507 smb_fname, /* fname */
3508 DELETE_ACCESS, /* access_mask */
3509 (FILE_SHARE_READ | /* share_access */
3510 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3511 FILE_OPEN, /* create_disposition*/
3512 0, /* create_options */
3513 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3514 0, /* oplock_request */
3515 0, /* allocation_size */
3516 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3517 NULL, /* sd */
3518 NULL, /* ea_list */
3519 &streams[i], /* result */
3520 NULL); /* pinfo */
3522 if (!NT_STATUS_IS_OK(status)) {
3523 DEBUG(10, ("Could not open stream %s: %s\n",
3524 smb_fname_str_dbg(smb_fname),
3525 nt_errstr(status)));
3527 TALLOC_FREE(smb_fname);
3528 break;
3530 TALLOC_FREE(smb_fname);
3534 * don't touch the variable "status" beyond this point :-)
3537 for (i -= 1 ; i >= 0; i--) {
3538 if (streams[i] == NULL) {
3539 continue;
3542 DEBUG(10, ("Closing stream # %d, %s\n", i,
3543 fsp_str_dbg(streams[i])));
3544 close_file(NULL, streams[i], NORMAL_CLOSE);
3547 fail:
3548 TALLOC_FREE(frame);
3549 return status;
3552 /*********************************************************************
3553 Create a default ACL by inheriting from the parent. If no inheritance
3554 from the parent available, don't set anything. This will leave the actual
3555 permissions the new file or directory already got from the filesystem
3556 as the NT ACL when read.
3557 *********************************************************************/
3559 static NTSTATUS inherit_new_acl(files_struct *fsp)
3561 TALLOC_CTX *frame = talloc_stackframe();
3562 char *parent_name = NULL;
3563 struct security_descriptor *parent_desc = NULL;
3564 NTSTATUS status = NT_STATUS_OK;
3565 struct security_descriptor *psd = NULL;
3566 const struct dom_sid *owner_sid = NULL;
3567 const struct dom_sid *group_sid = NULL;
3568 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3569 struct security_token *token = fsp->conn->session_info->security_token;
3570 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3571 bool inheritable_components = false;
3572 bool try_builtin_administrators = false;
3573 const struct dom_sid *BA_U_sid = NULL;
3574 const struct dom_sid *BA_G_sid = NULL;
3575 bool try_system = false;
3576 const struct dom_sid *SY_U_sid = NULL;
3577 const struct dom_sid *SY_G_sid = NULL;
3578 size_t size = 0;
3580 if (!parent_dirname(frame, fsp->fsp_name->base_name, &parent_name, NULL)) {
3581 TALLOC_FREE(frame);
3582 return NT_STATUS_NO_MEMORY;
3585 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3586 parent_name,
3587 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3588 frame,
3589 &parent_desc);
3590 if (!NT_STATUS_IS_OK(status)) {
3591 TALLOC_FREE(frame);
3592 return status;
3595 inheritable_components = sd_has_inheritable_components(parent_desc,
3596 fsp->is_directory);
3598 if (!inheritable_components && !inherit_owner) {
3599 TALLOC_FREE(frame);
3600 /* Nothing to inherit and not setting owner. */
3601 return NT_STATUS_OK;
3604 /* Create an inherited descriptor from the parent. */
3606 if (DEBUGLEVEL >= 10) {
3607 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3608 fsp_str_dbg(fsp) ));
3609 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3612 /* Inherit from parent descriptor if "inherit owner" set. */
3613 if (inherit_owner) {
3614 owner_sid = parent_desc->owner_sid;
3615 group_sid = parent_desc->group_sid;
3618 if (owner_sid == NULL) {
3619 if (security_token_has_builtin_administrators(token)) {
3620 try_builtin_administrators = true;
3621 } else if (security_token_is_system(token)) {
3622 try_builtin_administrators = true;
3623 try_system = true;
3627 if (group_sid == NULL &&
3628 token->num_sids == PRIMARY_GROUP_SID_INDEX)
3630 if (security_token_is_system(token)) {
3631 try_builtin_administrators = true;
3632 try_system = true;
3636 if (try_builtin_administrators) {
3637 struct unixid ids;
3638 bool ok;
3640 ZERO_STRUCT(ids);
3641 ok = sids_to_unixids(&global_sid_Builtin_Administrators, 1, &ids);
3642 if (ok) {
3643 switch (ids.type) {
3644 case ID_TYPE_BOTH:
3645 BA_U_sid = &global_sid_Builtin_Administrators;
3646 BA_G_sid = &global_sid_Builtin_Administrators;
3647 break;
3648 case ID_TYPE_UID:
3649 BA_U_sid = &global_sid_Builtin_Administrators;
3650 break;
3651 case ID_TYPE_GID:
3652 BA_G_sid = &global_sid_Builtin_Administrators;
3653 break;
3654 default:
3655 break;
3660 if (try_system) {
3661 struct unixid ids;
3662 bool ok;
3664 ZERO_STRUCT(ids);
3665 ok = sids_to_unixids(&global_sid_System, 1, &ids);
3666 if (ok) {
3667 switch (ids.type) {
3668 case ID_TYPE_BOTH:
3669 SY_U_sid = &global_sid_System;
3670 SY_G_sid = &global_sid_System;
3671 break;
3672 case ID_TYPE_UID:
3673 SY_U_sid = &global_sid_System;
3674 break;
3675 case ID_TYPE_GID:
3676 SY_G_sid = &global_sid_System;
3677 break;
3678 default:
3679 break;
3684 if (owner_sid == NULL) {
3685 owner_sid = BA_U_sid;
3688 if (owner_sid == NULL) {
3689 owner_sid = SY_U_sid;
3692 if (group_sid == NULL) {
3693 group_sid = SY_G_sid;
3696 if (try_system && group_sid == NULL) {
3697 group_sid = BA_G_sid;
3700 if (owner_sid == NULL) {
3701 owner_sid = &token->sids[PRIMARY_USER_SID_INDEX];
3703 if (group_sid == NULL) {
3704 if (token->num_sids == PRIMARY_GROUP_SID_INDEX) {
3705 group_sid = &token->sids[PRIMARY_USER_SID_INDEX];
3706 } else {
3707 group_sid = &token->sids[PRIMARY_GROUP_SID_INDEX];
3711 status = se_create_child_secdesc(frame,
3712 &psd,
3713 &size,
3714 parent_desc,
3715 owner_sid,
3716 group_sid,
3717 fsp->is_directory);
3718 if (!NT_STATUS_IS_OK(status)) {
3719 TALLOC_FREE(frame);
3720 return status;
3723 /* If inheritable_components == false,
3724 se_create_child_secdesc()
3725 creates a security desriptor with a NULL dacl
3726 entry, but with SEC_DESC_DACL_PRESENT. We need
3727 to remove that flag. */
3729 if (!inheritable_components) {
3730 security_info_sent &= ~SECINFO_DACL;
3731 psd->type &= ~SEC_DESC_DACL_PRESENT;
3734 if (DEBUGLEVEL >= 10) {
3735 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3736 fsp_str_dbg(fsp) ));
3737 NDR_PRINT_DEBUG(security_descriptor, psd);
3740 if (inherit_owner) {
3741 /* We need to be root to force this. */
3742 become_root();
3744 status = SMB_VFS_FSET_NT_ACL(fsp,
3745 security_info_sent,
3746 psd);
3747 if (inherit_owner) {
3748 unbecome_root();
3750 TALLOC_FREE(frame);
3751 return status;
3755 * Wrapper around open_file_ntcreate and open_directory
3758 static NTSTATUS create_file_unixpath(connection_struct *conn,
3759 struct smb_request *req,
3760 struct smb_filename *smb_fname,
3761 uint32_t access_mask,
3762 uint32_t share_access,
3763 uint32_t create_disposition,
3764 uint32_t create_options,
3765 uint32_t file_attributes,
3766 uint32_t oplock_request,
3767 uint64_t allocation_size,
3768 uint32_t private_flags,
3769 struct security_descriptor *sd,
3770 struct ea_list *ea_list,
3772 files_struct **result,
3773 int *pinfo)
3775 int info = FILE_WAS_OPENED;
3776 files_struct *base_fsp = NULL;
3777 files_struct *fsp = NULL;
3778 NTSTATUS status;
3780 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3781 "file_attributes = 0x%x, share_access = 0x%x, "
3782 "create_disposition = 0x%x create_options = 0x%x "
3783 "oplock_request = 0x%x private_flags = 0x%x "
3784 "ea_list = 0x%p, sd = 0x%p, "
3785 "fname = %s\n",
3786 (unsigned int)access_mask,
3787 (unsigned int)file_attributes,
3788 (unsigned int)share_access,
3789 (unsigned int)create_disposition,
3790 (unsigned int)create_options,
3791 (unsigned int)oplock_request,
3792 (unsigned int)private_flags,
3793 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3795 if (create_options & FILE_OPEN_BY_FILE_ID) {
3796 status = NT_STATUS_NOT_SUPPORTED;
3797 goto fail;
3800 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3801 status = NT_STATUS_INVALID_PARAMETER;
3802 goto fail;
3805 if (req == NULL) {
3806 oplock_request |= INTERNAL_OPEN_ONLY;
3809 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3810 && (access_mask & DELETE_ACCESS)
3811 && !is_ntfs_stream_smb_fname(smb_fname)) {
3813 * We can't open a file with DELETE access if any of the
3814 * streams is open without FILE_SHARE_DELETE
3816 status = open_streams_for_delete(conn, smb_fname->base_name);
3818 if (!NT_STATUS_IS_OK(status)) {
3819 goto fail;
3823 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3824 !security_token_has_privilege(get_current_nttok(conn),
3825 SEC_PRIV_SECURITY)) {
3826 DEBUG(10, ("create_file_unixpath: open on %s "
3827 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3828 smb_fname_str_dbg(smb_fname)));
3829 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3830 goto fail;
3833 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3834 && is_ntfs_stream_smb_fname(smb_fname)
3835 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3836 uint32 base_create_disposition;
3837 struct smb_filename *smb_fname_base = NULL;
3839 if (create_options & FILE_DIRECTORY_FILE) {
3840 status = NT_STATUS_NOT_A_DIRECTORY;
3841 goto fail;
3844 switch (create_disposition) {
3845 case FILE_OPEN:
3846 base_create_disposition = FILE_OPEN;
3847 break;
3848 default:
3849 base_create_disposition = FILE_OPEN_IF;
3850 break;
3853 /* Create an smb_filename with stream_name == NULL. */
3854 smb_fname_base = synthetic_smb_fname(talloc_tos(),
3855 smb_fname->base_name,
3856 NULL, NULL);
3857 if (smb_fname_base == NULL) {
3858 status = NT_STATUS_NO_MEMORY;
3859 goto fail;
3862 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3863 DEBUG(10, ("Unable to stat stream: %s\n",
3864 smb_fname_str_dbg(smb_fname_base)));
3865 } else {
3867 * https://bugzilla.samba.org/show_bug.cgi?id=10229
3868 * We need to check if the requested access mask
3869 * could be used to open the underlying file (if
3870 * it existed), as we're passing in zero for the
3871 * access mask to the base filename.
3873 status = check_base_file_access(conn,
3874 smb_fname_base,
3875 access_mask);
3877 if (!NT_STATUS_IS_OK(status)) {
3878 DEBUG(10, ("Permission check "
3879 "for base %s failed: "
3880 "%s\n", smb_fname->base_name,
3881 nt_errstr(status)));
3882 goto fail;
3886 /* Open the base file. */
3887 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3888 FILE_SHARE_READ
3889 | FILE_SHARE_WRITE
3890 | FILE_SHARE_DELETE,
3891 base_create_disposition,
3892 0, 0, 0, 0, 0, NULL, NULL,
3893 &base_fsp, NULL);
3894 TALLOC_FREE(smb_fname_base);
3896 if (!NT_STATUS_IS_OK(status)) {
3897 DEBUG(10, ("create_file_unixpath for base %s failed: "
3898 "%s\n", smb_fname->base_name,
3899 nt_errstr(status)));
3900 goto fail;
3902 /* we don't need to low level fd */
3903 fd_close(base_fsp);
3907 * If it's a request for a directory open, deal with it separately.
3910 if (create_options & FILE_DIRECTORY_FILE) {
3912 if (create_options & FILE_NON_DIRECTORY_FILE) {
3913 status = NT_STATUS_INVALID_PARAMETER;
3914 goto fail;
3917 /* Can't open a temp directory. IFS kit test. */
3918 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3919 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3920 status = NT_STATUS_INVALID_PARAMETER;
3921 goto fail;
3925 * We will get a create directory here if the Win32
3926 * app specified a security descriptor in the
3927 * CreateDirectory() call.
3930 oplock_request = 0;
3931 status = open_directory(
3932 conn, req, smb_fname, access_mask, share_access,
3933 create_disposition, create_options, file_attributes,
3934 &info, &fsp);
3935 } else {
3938 * Ordinary file case.
3941 status = file_new(req, conn, &fsp);
3942 if(!NT_STATUS_IS_OK(status)) {
3943 goto fail;
3946 status = fsp_set_smb_fname(fsp, smb_fname);
3947 if (!NT_STATUS_IS_OK(status)) {
3948 goto fail;
3951 if (base_fsp) {
3953 * We're opening the stream element of a
3954 * base_fsp we already opened. Set up the
3955 * base_fsp pointer.
3957 fsp->base_fsp = base_fsp;
3960 if (allocation_size) {
3961 fsp->initial_allocation_size = smb_roundup(fsp->conn,
3962 allocation_size);
3965 status = open_file_ntcreate(conn,
3966 req,
3967 access_mask,
3968 share_access,
3969 create_disposition,
3970 create_options,
3971 file_attributes,
3972 oplock_request,
3973 private_flags,
3974 &info,
3975 fsp);
3977 if(!NT_STATUS_IS_OK(status)) {
3978 file_free(req, fsp);
3979 fsp = NULL;
3982 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3984 /* A stream open never opens a directory */
3986 if (base_fsp) {
3987 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3988 goto fail;
3992 * Fail the open if it was explicitly a non-directory
3993 * file.
3996 if (create_options & FILE_NON_DIRECTORY_FILE) {
3997 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3998 goto fail;
4001 oplock_request = 0;
4002 status = open_directory(
4003 conn, req, smb_fname, access_mask,
4004 share_access, create_disposition,
4005 create_options, file_attributes,
4006 &info, &fsp);
4010 if (!NT_STATUS_IS_OK(status)) {
4011 goto fail;
4014 fsp->base_fsp = base_fsp;
4016 if ((ea_list != NULL) &&
4017 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
4018 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
4019 if (!NT_STATUS_IS_OK(status)) {
4020 goto fail;
4024 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
4025 status = NT_STATUS_ACCESS_DENIED;
4026 goto fail;
4029 /* Save the requested allocation size. */
4030 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
4031 if (allocation_size
4032 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
4033 fsp->initial_allocation_size = smb_roundup(
4034 fsp->conn, allocation_size);
4035 if (fsp->is_directory) {
4036 /* Can't set allocation size on a directory. */
4037 status = NT_STATUS_ACCESS_DENIED;
4038 goto fail;
4040 if (vfs_allocate_file_space(
4041 fsp, fsp->initial_allocation_size) == -1) {
4042 status = NT_STATUS_DISK_FULL;
4043 goto fail;
4045 } else {
4046 fsp->initial_allocation_size = smb_roundup(
4047 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
4049 } else {
4050 fsp->initial_allocation_size = 0;
4053 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
4054 fsp->base_fsp == NULL) {
4055 if (sd != NULL) {
4057 * According to the MS documentation, the only time the security
4058 * descriptor is applied to the opened file is iff we *created* the
4059 * file; an existing file stays the same.
4061 * Also, it seems (from observation) that you can open the file with
4062 * any access mask but you can still write the sd. We need to override
4063 * the granted access before we call set_sd
4064 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
4067 uint32_t sec_info_sent;
4068 uint32_t saved_access_mask = fsp->access_mask;
4070 sec_info_sent = get_sec_info(sd);
4072 fsp->access_mask = FILE_GENERIC_ALL;
4074 if (sec_info_sent & (SECINFO_OWNER|
4075 SECINFO_GROUP|
4076 SECINFO_DACL|
4077 SECINFO_SACL)) {
4078 status = set_sd(fsp, sd, sec_info_sent);
4081 fsp->access_mask = saved_access_mask;
4083 if (!NT_STATUS_IS_OK(status)) {
4084 goto fail;
4086 } else if (lp_inherit_acls(SNUM(conn))) {
4087 /* Inherit from parent. Errors here are not fatal. */
4088 status = inherit_new_acl(fsp);
4089 if (!NT_STATUS_IS_OK(status)) {
4090 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
4091 fsp_str_dbg(fsp),
4092 nt_errstr(status) ));
4097 if ((conn->fs_capabilities & FILE_FILE_COMPRESSION)
4098 && (create_options & FILE_NO_COMPRESSION)
4099 && (info == FILE_WAS_CREATED)) {
4100 status = SMB_VFS_SET_COMPRESSION(conn, fsp, fsp,
4101 COMPRESSION_FORMAT_NONE);
4102 if (!NT_STATUS_IS_OK(status)) {
4103 DEBUG(1, ("failed to disable compression: %s\n",
4104 nt_errstr(status)));
4108 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
4110 *result = fsp;
4111 if (pinfo != NULL) {
4112 *pinfo = info;
4115 smb_fname->st = fsp->fsp_name->st;
4117 return NT_STATUS_OK;
4119 fail:
4120 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
4122 if (fsp != NULL) {
4123 if (base_fsp && fsp->base_fsp == base_fsp) {
4125 * The close_file below will close
4126 * fsp->base_fsp.
4128 base_fsp = NULL;
4130 close_file(req, fsp, ERROR_CLOSE);
4131 fsp = NULL;
4133 if (base_fsp != NULL) {
4134 close_file(req, base_fsp, ERROR_CLOSE);
4135 base_fsp = NULL;
4137 return status;
4141 * Calculate the full path name given a relative fid.
4143 NTSTATUS get_relative_fid_filename(connection_struct *conn,
4144 struct smb_request *req,
4145 uint16_t root_dir_fid,
4146 const struct smb_filename *smb_fname,
4147 struct smb_filename **smb_fname_out)
4149 files_struct *dir_fsp;
4150 char *parent_fname = NULL;
4151 char *new_base_name = NULL;
4152 NTSTATUS status;
4154 if (root_dir_fid == 0 || !smb_fname) {
4155 status = NT_STATUS_INTERNAL_ERROR;
4156 goto out;
4159 dir_fsp = file_fsp(req, root_dir_fid);
4161 if (dir_fsp == NULL) {
4162 status = NT_STATUS_INVALID_HANDLE;
4163 goto out;
4166 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
4167 status = NT_STATUS_INVALID_HANDLE;
4168 goto out;
4171 if (!dir_fsp->is_directory) {
4174 * Check to see if this is a mac fork of some kind.
4177 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
4178 is_ntfs_stream_smb_fname(smb_fname)) {
4179 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
4180 goto out;
4184 we need to handle the case when we get a
4185 relative open relative to a file and the
4186 pathname is blank - this is a reopen!
4187 (hint from demyn plantenberg)
4190 status = NT_STATUS_INVALID_HANDLE;
4191 goto out;
4194 if (ISDOT(dir_fsp->fsp_name->base_name)) {
4196 * We're at the toplevel dir, the final file name
4197 * must not contain ./, as this is filtered out
4198 * normally by srvstr_get_path and unix_convert
4199 * explicitly rejects paths containing ./.
4201 parent_fname = talloc_strdup(talloc_tos(), "");
4202 if (parent_fname == NULL) {
4203 status = NT_STATUS_NO_MEMORY;
4204 goto out;
4206 } else {
4207 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
4210 * Copy in the base directory name.
4213 parent_fname = talloc_array(talloc_tos(), char,
4214 dir_name_len+2);
4215 if (parent_fname == NULL) {
4216 status = NT_STATUS_NO_MEMORY;
4217 goto out;
4219 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
4220 dir_name_len+1);
4223 * Ensure it ends in a '/'.
4224 * We used TALLOC_SIZE +2 to add space for the '/'.
4227 if(dir_name_len
4228 && (parent_fname[dir_name_len-1] != '\\')
4229 && (parent_fname[dir_name_len-1] != '/')) {
4230 parent_fname[dir_name_len] = '/';
4231 parent_fname[dir_name_len+1] = '\0';
4235 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
4236 smb_fname->base_name);
4237 if (new_base_name == NULL) {
4238 status = NT_STATUS_NO_MEMORY;
4239 goto out;
4242 status = filename_convert(req,
4243 conn,
4244 req->flags2 & FLAGS2_DFS_PATHNAMES,
4245 new_base_name,
4247 NULL,
4248 smb_fname_out);
4249 if (!NT_STATUS_IS_OK(status)) {
4250 goto out;
4253 out:
4254 TALLOC_FREE(parent_fname);
4255 TALLOC_FREE(new_base_name);
4256 return status;
4259 NTSTATUS create_file_default(connection_struct *conn,
4260 struct smb_request *req,
4261 uint16_t root_dir_fid,
4262 struct smb_filename *smb_fname,
4263 uint32_t access_mask,
4264 uint32_t share_access,
4265 uint32_t create_disposition,
4266 uint32_t create_options,
4267 uint32_t file_attributes,
4268 uint32_t oplock_request,
4269 uint64_t allocation_size,
4270 uint32_t private_flags,
4271 struct security_descriptor *sd,
4272 struct ea_list *ea_list,
4273 files_struct **result,
4274 int *pinfo)
4276 int info = FILE_WAS_OPENED;
4277 files_struct *fsp = NULL;
4278 NTSTATUS status;
4279 bool stream_name = false;
4281 DEBUG(10,("create_file: access_mask = 0x%x "
4282 "file_attributes = 0x%x, share_access = 0x%x, "
4283 "create_disposition = 0x%x create_options = 0x%x "
4284 "oplock_request = 0x%x "
4285 "private_flags = 0x%x "
4286 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4287 "fname = %s\n",
4288 (unsigned int)access_mask,
4289 (unsigned int)file_attributes,
4290 (unsigned int)share_access,
4291 (unsigned int)create_disposition,
4292 (unsigned int)create_options,
4293 (unsigned int)oplock_request,
4294 (unsigned int)private_flags,
4295 (unsigned int)root_dir_fid,
4296 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4299 * Calculate the filename from the root_dir_if if necessary.
4302 if (root_dir_fid != 0) {
4303 struct smb_filename *smb_fname_out = NULL;
4304 status = get_relative_fid_filename(conn, req, root_dir_fid,
4305 smb_fname, &smb_fname_out);
4306 if (!NT_STATUS_IS_OK(status)) {
4307 goto fail;
4309 smb_fname = smb_fname_out;
4313 * Check to see if this is a mac fork of some kind.
4316 stream_name = is_ntfs_stream_smb_fname(smb_fname);
4317 if (stream_name) {
4318 enum FAKE_FILE_TYPE fake_file_type;
4320 fake_file_type = is_fake_file(smb_fname);
4322 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
4325 * Here we go! support for changing the disk quotas
4326 * --metze
4328 * We need to fake up to open this MAGIC QUOTA file
4329 * and return a valid FID.
4331 * w2k close this file directly after openening xp
4332 * also tries a QUERY_FILE_INFO on the file and then
4333 * close it
4335 status = open_fake_file(req, conn, req->vuid,
4336 fake_file_type, smb_fname,
4337 access_mask, &fsp);
4338 if (!NT_STATUS_IS_OK(status)) {
4339 goto fail;
4342 ZERO_STRUCT(smb_fname->st);
4343 goto done;
4346 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
4347 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4348 goto fail;
4352 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
4353 int ret;
4354 smb_fname->stream_name = NULL;
4355 /* We have to handle this error here. */
4356 if (create_options & FILE_DIRECTORY_FILE) {
4357 status = NT_STATUS_NOT_A_DIRECTORY;
4358 goto fail;
4360 if (lp_posix_pathnames()) {
4361 ret = SMB_VFS_LSTAT(conn, smb_fname);
4362 } else {
4363 ret = SMB_VFS_STAT(conn, smb_fname);
4366 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
4367 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4368 goto fail;
4372 status = create_file_unixpath(
4373 conn, req, smb_fname, access_mask, share_access,
4374 create_disposition, create_options, file_attributes,
4375 oplock_request, allocation_size, private_flags,
4376 sd, ea_list,
4377 &fsp, &info);
4379 if (!NT_STATUS_IS_OK(status)) {
4380 goto fail;
4383 done:
4384 DEBUG(10, ("create_file: info=%d\n", info));
4386 *result = fsp;
4387 if (pinfo != NULL) {
4388 *pinfo = info;
4390 return NT_STATUS_OK;
4392 fail:
4393 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
4395 if (fsp != NULL) {
4396 close_file(req, fsp, ERROR_CLOSE);
4397 fsp = NULL;
4399 return status;