s3: Make is_stat_open() a bit more readable
[Samba/gbeck.git] / source3 / smbd / open.c
blobeff78d45339d0fa3e60f15760f20381de167c011
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 "auth.h"
32 #include "serverid.h"
33 #include "messages.h"
35 extern const struct generic_mapping file_generic_mapping;
37 struct deferred_open_record {
38 bool delayed_for_oplocks;
39 bool async_open;
40 struct file_id id;
43 /****************************************************************************
44 If the requester wanted DELETE_ACCESS and was rejected because
45 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
46 overrides this.
47 ****************************************************************************/
49 static bool parent_override_delete(connection_struct *conn,
50 const struct smb_filename *smb_fname,
51 uint32_t access_mask,
52 uint32_t rejected_mask)
54 if ((access_mask & DELETE_ACCESS) &&
55 (rejected_mask & DELETE_ACCESS) &&
56 can_delete_file_in_directory(conn, smb_fname)) {
57 return true;
59 return false;
62 /****************************************************************************
63 Check if we have open rights.
64 ****************************************************************************/
66 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
67 const struct smb_filename *smb_fname,
68 bool use_privs,
69 uint32_t access_mask)
71 /* Check if we have rights to open. */
72 NTSTATUS status;
73 struct security_descriptor *sd = NULL;
74 uint32_t rejected_share_access;
75 uint32_t rejected_mask = access_mask;
77 rejected_share_access = access_mask & ~(conn->share_access);
79 if (rejected_share_access) {
80 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
81 "on %s (0x%x)\n",
82 (unsigned int)access_mask,
83 smb_fname_str_dbg(smb_fname),
84 (unsigned int)rejected_share_access ));
85 return NT_STATUS_ACCESS_DENIED;
88 if (!use_privs && get_current_uid(conn) == (uid_t)0) {
89 /* I'm sorry sir, I didn't know you were root... */
90 DEBUG(10,("smbd_check_access_rights: root override "
91 "on %s. Granting 0x%x\n",
92 smb_fname_str_dbg(smb_fname),
93 (unsigned int)access_mask ));
94 return NT_STATUS_OK;
97 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
98 DEBUG(10,("smbd_check_access_rights: not checking ACL "
99 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
100 smb_fname_str_dbg(smb_fname),
101 (unsigned int)access_mask ));
102 return NT_STATUS_OK;
105 if (access_mask == DELETE_ACCESS &&
106 VALID_STAT(smb_fname->st) &&
107 S_ISLNK(smb_fname->st.st_ex_mode)) {
108 /* We can always delete a symlink. */
109 DEBUG(10,("smbd_check_access_rights: not checking ACL "
110 "on DELETE_ACCESS on symlink %s.\n",
111 smb_fname_str_dbg(smb_fname) ));
112 return NT_STATUS_OK;
115 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
116 (SECINFO_OWNER |
117 SECINFO_GROUP |
118 SECINFO_DACL),&sd);
120 if (!NT_STATUS_IS_OK(status)) {
121 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
122 "on %s: %s\n",
123 smb_fname_str_dbg(smb_fname),
124 nt_errstr(status)));
126 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
127 goto access_denied;
130 return status;
134 * Never test FILE_READ_ATTRIBUTES. se_file_access_check() also takes care of
135 * owner WRITE_DAC and READ_CONTROL.
137 status = se_file_access_check(sd,
138 get_current_nttok(conn),
139 use_privs,
140 (access_mask & ~FILE_READ_ATTRIBUTES),
141 &rejected_mask);
143 DEBUG(10,("smbd_check_access_rights: file %s requesting "
144 "0x%x returning 0x%x (%s)\n",
145 smb_fname_str_dbg(smb_fname),
146 (unsigned int)access_mask,
147 (unsigned int)rejected_mask,
148 nt_errstr(status) ));
150 if (!NT_STATUS_IS_OK(status)) {
151 if (DEBUGLEVEL >= 10) {
152 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
153 smb_fname_str_dbg(smb_fname) ));
154 NDR_PRINT_DEBUG(security_descriptor, sd);
158 TALLOC_FREE(sd);
160 if (NT_STATUS_IS_OK(status) ||
161 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
162 return status;
165 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
167 access_denied:
169 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
170 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
171 !lp_store_dos_attributes(SNUM(conn)) &&
172 (lp_map_readonly(SNUM(conn)) ||
173 lp_map_archive(SNUM(conn)) ||
174 lp_map_hidden(SNUM(conn)) ||
175 lp_map_system(SNUM(conn)))) {
176 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
178 DEBUG(10,("smbd_check_access_rights: "
179 "overrode "
180 "FILE_WRITE_ATTRIBUTES "
181 "on file %s\n",
182 smb_fname_str_dbg(smb_fname)));
185 if (parent_override_delete(conn,
186 smb_fname,
187 access_mask,
188 rejected_mask)) {
189 /* Were we trying to do an open
190 * for delete and didn't get DELETE
191 * access (only) ? Check if the
192 * directory allows DELETE_CHILD.
193 * See here:
194 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
195 * for details. */
197 rejected_mask &= ~DELETE_ACCESS;
199 DEBUG(10,("smbd_check_access_rights: "
200 "overrode "
201 "DELETE_ACCESS on "
202 "file %s\n",
203 smb_fname_str_dbg(smb_fname)));
206 if (rejected_mask != 0) {
207 return NT_STATUS_ACCESS_DENIED;
209 return NT_STATUS_OK;
212 static NTSTATUS check_parent_access(struct connection_struct *conn,
213 struct smb_filename *smb_fname,
214 uint32_t access_mask)
216 NTSTATUS status;
217 char *parent_dir = NULL;
218 struct security_descriptor *parent_sd = NULL;
219 uint32_t access_granted = 0;
221 if (!parent_dirname(talloc_tos(),
222 smb_fname->base_name,
223 &parent_dir,
224 NULL)) {
225 return NT_STATUS_NO_MEMORY;
228 if (get_current_uid(conn) == (uid_t)0) {
229 /* I'm sorry sir, I didn't know you were root... */
230 DEBUG(10,("check_parent_access: root override "
231 "on %s. Granting 0x%x\n",
232 smb_fname_str_dbg(smb_fname),
233 (unsigned int)access_mask ));
234 return NT_STATUS_OK;
237 status = SMB_VFS_GET_NT_ACL(conn,
238 parent_dir,
239 SECINFO_DACL,
240 &parent_sd);
242 if (!NT_STATUS_IS_OK(status)) {
243 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
244 "%s with error %s\n",
245 parent_dir,
246 nt_errstr(status)));
247 return status;
251 * Never test FILE_READ_ATTRIBUTES. se_file_access_check() also takes care of
252 * owner WRITE_DAC and READ_CONTROL.
254 status = se_file_access_check(parent_sd,
255 get_current_nttok(conn),
256 false,
257 (access_mask & ~FILE_READ_ATTRIBUTES),
258 &access_granted);
259 if(!NT_STATUS_IS_OK(status)) {
260 DEBUG(5,("check_parent_access: access check "
261 "on directory %s for "
262 "path %s for mask 0x%x returned (0x%x) %s\n",
263 parent_dir,
264 smb_fname->base_name,
265 access_mask,
266 access_granted,
267 nt_errstr(status) ));
268 return status;
271 return NT_STATUS_OK;
274 /****************************************************************************
275 fd support routines - attempt to do a dos_open.
276 ****************************************************************************/
278 NTSTATUS fd_open(struct connection_struct *conn,
279 files_struct *fsp,
280 int flags,
281 mode_t mode)
283 struct smb_filename *smb_fname = fsp->fsp_name;
284 NTSTATUS status = NT_STATUS_OK;
286 #ifdef O_NOFOLLOW
288 * Never follow symlinks on a POSIX client. The
289 * client should be doing this.
292 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
293 flags |= O_NOFOLLOW;
295 #endif
297 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
298 if (fsp->fh->fd == -1) {
299 int posix_errno = errno;
300 #ifdef O_NOFOLLOW
301 #if defined(ENOTSUP) && defined(OSF1)
302 /* handle special Tru64 errno */
303 if (errno == ENOTSUP) {
304 posix_errno = ELOOP;
306 #endif /* ENOTSUP */
307 #ifdef EFTYPE
308 /* fix broken NetBSD errno */
309 if (errno == EFTYPE) {
310 posix_errno = ELOOP;
312 #endif /* EFTYPE */
313 /* fix broken FreeBSD errno */
314 if (errno == EMLINK) {
315 posix_errno = ELOOP;
317 #endif /* O_NOFOLLOW */
318 status = map_nt_error_from_unix(posix_errno);
319 if (errno == EMFILE) {
320 static time_t last_warned = 0L;
322 if (time((time_t *) NULL) > last_warned) {
323 DEBUG(0,("Too many open files, unable "
324 "to open more! smbd's max "
325 "open files = %d\n",
326 lp_max_open_files()));
327 last_warned = time((time_t *) NULL);
333 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
334 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
335 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
337 return status;
340 /****************************************************************************
341 Close the file associated with a fsp.
342 ****************************************************************************/
344 NTSTATUS fd_close(files_struct *fsp)
346 int ret;
348 if (fsp->dptr) {
349 dptr_CloseDir(fsp);
351 if (fsp->fh->fd == -1) {
352 return NT_STATUS_OK; /* What we used to call a stat open. */
354 if (fsp->fh->ref_count > 1) {
355 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
358 ret = SMB_VFS_CLOSE(fsp);
359 fsp->fh->fd = -1;
360 if (ret == -1) {
361 return map_nt_error_from_unix(errno);
363 return NT_STATUS_OK;
366 /****************************************************************************
367 Change the ownership of a file to that of the parent directory.
368 Do this by fd if possible.
369 ****************************************************************************/
371 void change_file_owner_to_parent(connection_struct *conn,
372 const char *inherit_from_dir,
373 files_struct *fsp)
375 struct smb_filename *smb_fname_parent = NULL;
376 NTSTATUS status;
377 int ret;
379 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
380 NULL, NULL, &smb_fname_parent);
381 if (!NT_STATUS_IS_OK(status)) {
382 return;
385 ret = SMB_VFS_STAT(conn, smb_fname_parent);
386 if (ret == -1) {
387 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
388 "directory %s. Error was %s\n",
389 smb_fname_str_dbg(smb_fname_parent),
390 strerror(errno)));
391 TALLOC_FREE(smb_fname_parent);
392 return;
395 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
396 /* Already this uid - no need to change. */
397 DEBUG(10,("change_file_owner_to_parent: file %s "
398 "is already owned by uid %d\n",
399 fsp_str_dbg(fsp),
400 (int)fsp->fsp_name->st.st_ex_uid ));
401 TALLOC_FREE(smb_fname_parent);
402 return;
405 become_root();
406 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
407 unbecome_root();
408 if (ret == -1) {
409 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
410 "file %s to parent directory uid %u. Error "
411 "was %s\n", fsp_str_dbg(fsp),
412 (unsigned int)smb_fname_parent->st.st_ex_uid,
413 strerror(errno) ));
414 } else {
415 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
416 "parent directory uid %u.\n", fsp_str_dbg(fsp),
417 (unsigned int)smb_fname_parent->st.st_ex_uid));
418 /* Ensure the uid entry is updated. */
419 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
422 TALLOC_FREE(smb_fname_parent);
425 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
426 const char *inherit_from_dir,
427 const char *fname,
428 SMB_STRUCT_STAT *psbuf)
430 struct smb_filename *smb_fname_parent = NULL;
431 struct smb_filename *smb_fname_cwd = NULL;
432 char *saved_dir = NULL;
433 TALLOC_CTX *ctx = talloc_tos();
434 NTSTATUS status = NT_STATUS_OK;
435 int ret;
437 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
438 &smb_fname_parent);
439 if (!NT_STATUS_IS_OK(status)) {
440 return status;
443 ret = SMB_VFS_STAT(conn, smb_fname_parent);
444 if (ret == -1) {
445 status = map_nt_error_from_unix(errno);
446 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
447 "directory %s. Error was %s\n",
448 smb_fname_str_dbg(smb_fname_parent),
449 strerror(errno)));
450 goto out;
453 /* We've already done an lstat into psbuf, and we know it's a
454 directory. If we can cd into the directory and the dev/ino
455 are the same then we can safely chown without races as
456 we're locking the directory in place by being in it. This
457 should work on any UNIX (thanks tridge :-). JRA.
460 saved_dir = vfs_GetWd(ctx,conn);
461 if (!saved_dir) {
462 status = map_nt_error_from_unix(errno);
463 DEBUG(0,("change_dir_owner_to_parent: failed to get "
464 "current working directory. Error was %s\n",
465 strerror(errno)));
466 goto out;
469 /* Chdir into the new path. */
470 if (vfs_ChDir(conn, fname) == -1) {
471 status = map_nt_error_from_unix(errno);
472 DEBUG(0,("change_dir_owner_to_parent: failed to change "
473 "current working directory to %s. Error "
474 "was %s\n", fname, strerror(errno) ));
475 goto chdir;
478 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
479 &smb_fname_cwd);
480 if (!NT_STATUS_IS_OK(status)) {
481 return status;
484 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
485 if (ret == -1) {
486 status = map_nt_error_from_unix(errno);
487 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
488 "directory '.' (%s) Error was %s\n",
489 fname, strerror(errno)));
490 goto chdir;
493 /* Ensure we're pointing at the same place. */
494 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
495 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
496 DEBUG(0,("change_dir_owner_to_parent: "
497 "device/inode on directory %s changed. "
498 "Refusing to chown !\n", fname ));
499 status = NT_STATUS_ACCESS_DENIED;
500 goto chdir;
503 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
504 /* Already this uid - no need to change. */
505 DEBUG(10,("change_dir_owner_to_parent: directory %s "
506 "is already owned by uid %d\n",
507 fname,
508 (int)smb_fname_cwd->st.st_ex_uid ));
509 status = NT_STATUS_OK;
510 goto chdir;
513 become_root();
514 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
515 (gid_t)-1);
516 unbecome_root();
517 if (ret == -1) {
518 status = map_nt_error_from_unix(errno);
519 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
520 "directory %s to parent directory uid %u. "
521 "Error was %s\n", fname,
522 (unsigned int)smb_fname_parent->st.st_ex_uid,
523 strerror(errno) ));
524 } else {
525 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
526 "directory %s to parent directory uid %u.\n",
527 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
528 /* Ensure the uid entry is updated. */
529 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
532 chdir:
533 vfs_ChDir(conn,saved_dir);
534 out:
535 TALLOC_FREE(smb_fname_parent);
536 TALLOC_FREE(smb_fname_cwd);
537 return status;
540 /****************************************************************************
541 Open a file - returning a guaranteed ATOMIC indication of if the
542 file was created or not.
543 ****************************************************************************/
545 static NTSTATUS fd_open_atomic(struct connection_struct *conn,
546 files_struct *fsp,
547 int flags,
548 mode_t mode,
549 bool *file_created)
551 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
552 bool file_existed = VALID_STAT(fsp->fsp_name->st);
554 *file_created = false;
556 if (!(flags & O_CREAT)) {
558 * We're not creating the file, just pass through.
560 return fd_open(conn, fsp, flags, mode);
563 if (flags & O_EXCL) {
565 * Fail if already exists, just pass through.
567 status = fd_open(conn, fsp, flags, mode);
570 * Here we've opened with O_CREAT|O_EXCL. If that went
571 * NT_STATUS_OK, we *know* we created this file.
573 *file_created = NT_STATUS_IS_OK(status);
575 return status;
579 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
580 * To know absolutely if we created the file or not,
581 * we can never call O_CREAT without O_EXCL. So if
582 * we think the file existed, try without O_CREAT|O_EXCL.
583 * If we think the file didn't exist, try with
584 * O_CREAT|O_EXCL. Keep bouncing between these two
585 * requests until either the file is created, or
586 * opened. Either way, we keep going until we get
587 * a returnable result (error, or open/create).
590 while(1) {
591 int curr_flags = flags;
593 if (file_existed) {
594 /* Just try open, do not create. */
595 curr_flags &= ~(O_CREAT);
596 status = fd_open(conn, fsp, curr_flags, mode);
597 if (NT_STATUS_EQUAL(status,
598 NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
600 * Someone deleted it in the meantime.
601 * Retry with O_EXCL.
603 file_existed = false;
604 DEBUG(10,("fd_open_atomic: file %s existed. "
605 "Retry.\n",
606 smb_fname_str_dbg(fsp->fsp_name)));
607 continue;
609 } else {
610 /* Try create exclusively, fail if it exists. */
611 curr_flags |= O_EXCL;
612 status = fd_open(conn, fsp, curr_flags, mode);
613 if (NT_STATUS_EQUAL(status,
614 NT_STATUS_OBJECT_NAME_COLLISION)) {
616 * Someone created it in the meantime.
617 * Retry without O_CREAT.
619 file_existed = true;
620 DEBUG(10,("fd_open_atomic: file %s "
621 "did not exist. Retry.\n",
622 smb_fname_str_dbg(fsp->fsp_name)));
623 continue;
625 if (NT_STATUS_IS_OK(status)) {
627 * Here we've opened with O_CREAT|O_EXCL
628 * and got success. We *know* we created
629 * this file.
631 *file_created = true;
634 /* Create is done, or failed. */
635 break;
637 return status;
640 /****************************************************************************
641 Open a file.
642 ****************************************************************************/
644 static NTSTATUS open_file(files_struct *fsp,
645 connection_struct *conn,
646 struct smb_request *req,
647 const char *parent_dir,
648 int flags,
649 mode_t unx_mode,
650 uint32 access_mask, /* client requested access mask. */
651 uint32 open_access_mask, /* what we're actually using in the open. */
652 bool *p_file_created)
654 struct smb_filename *smb_fname = fsp->fsp_name;
655 NTSTATUS status = NT_STATUS_OK;
656 int accmode = (flags & O_ACCMODE);
657 int local_flags = flags;
658 bool file_existed = VALID_STAT(fsp->fsp_name->st);
660 fsp->fh->fd = -1;
661 errno = EPERM;
663 /* Check permissions */
666 * This code was changed after seeing a client open request
667 * containing the open mode of (DENY_WRITE/read-only) with
668 * the 'create if not exist' bit set. The previous code
669 * would fail to open the file read only on a read-only share
670 * as it was checking the flags parameter directly against O_RDONLY,
671 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
672 * JRA.
675 if (!CAN_WRITE(conn)) {
676 /* It's a read-only share - fail if we wanted to write. */
677 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
678 DEBUG(3,("Permission denied opening %s\n",
679 smb_fname_str_dbg(smb_fname)));
680 return NT_STATUS_ACCESS_DENIED;
681 } else if(flags & O_CREAT) {
682 /* We don't want to write - but we must make sure that
683 O_CREAT doesn't create the file if we have write
684 access into the directory.
686 flags &= ~(O_CREAT|O_EXCL);
687 local_flags &= ~(O_CREAT|O_EXCL);
692 * This little piece of insanity is inspired by the
693 * fact that an NT client can open a file for O_RDONLY,
694 * but set the create disposition to FILE_EXISTS_TRUNCATE.
695 * If the client *can* write to the file, then it expects to
696 * truncate the file, even though it is opening for readonly.
697 * Quicken uses this stupid trick in backup file creation...
698 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
699 * for helping track this one down. It didn't bite us in 2.0.x
700 * as we always opened files read-write in that release. JRA.
703 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
704 DEBUG(10,("open_file: truncate requested on read-only open "
705 "for file %s\n", smb_fname_str_dbg(smb_fname)));
706 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
709 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
710 (!file_existed && (local_flags & O_CREAT)) ||
711 ((local_flags & O_TRUNC) == O_TRUNC) ) {
712 const char *wild;
713 int ret;
715 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
717 * We would block on opening a FIFO with no one else on the
718 * other end. Do what we used to do and add O_NONBLOCK to the
719 * open flags. JRA.
722 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
723 local_flags &= ~O_TRUNC; /* Can't truncate a FIFO. */
724 local_flags |= O_NONBLOCK;
726 #endif
728 /* Don't create files with Microsoft wildcard characters. */
729 if (fsp->base_fsp) {
731 * wildcard characters are allowed in stream names
732 * only test the basefilename
734 wild = fsp->base_fsp->fsp_name->base_name;
735 } else {
736 wild = smb_fname->base_name;
738 if ((local_flags & O_CREAT) && !file_existed &&
739 ms_has_wild(wild)) {
740 return NT_STATUS_OBJECT_NAME_INVALID;
743 /* Can we access this file ? */
744 if (!fsp->base_fsp) {
745 /* Only do this check on non-stream open. */
746 if (file_existed) {
747 status = smbd_check_access_rights(conn,
748 smb_fname,
749 false,
750 access_mask);
751 } else if (local_flags & O_CREAT){
752 status = check_parent_access(conn,
753 smb_fname,
754 SEC_DIR_ADD_FILE);
755 } else {
756 /* File didn't exist and no O_CREAT. */
757 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
759 if (!NT_STATUS_IS_OK(status)) {
760 DEBUG(10,("open_file: "
761 "%s on file "
762 "%s returned %s\n",
763 file_existed ?
764 "smbd_check_access_rights" :
765 "check_parent_access",
766 smb_fname_str_dbg(smb_fname),
767 nt_errstr(status) ));
768 return status;
772 /* Actually do the open */
773 status = fd_open_atomic(conn, fsp, local_flags,
774 unx_mode, p_file_created);
775 if (!NT_STATUS_IS_OK(status)) {
776 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
777 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
778 nt_errstr(status),local_flags,flags));
779 return status;
782 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
783 if (ret == -1) {
784 /* If we have an fd, this stat should succeed. */
785 DEBUG(0,("Error doing fstat on open file %s "
786 "(%s)\n",
787 smb_fname_str_dbg(smb_fname),
788 strerror(errno) ));
789 status = map_nt_error_from_unix(errno);
790 fd_close(fsp);
791 return status;
794 if (*p_file_created) {
795 /* We created this file. */
797 bool need_re_stat = false;
798 /* Do all inheritance work after we've
799 done a successful fstat call and filled
800 in the stat struct in fsp->fsp_name. */
802 /* Inherit the ACL if required */
803 if (lp_inherit_perms(SNUM(conn))) {
804 inherit_access_posix_acl(conn, parent_dir,
805 smb_fname->base_name,
806 unx_mode);
807 need_re_stat = true;
810 /* Change the owner if required. */
811 if (lp_inherit_owner(SNUM(conn))) {
812 change_file_owner_to_parent(conn, parent_dir,
813 fsp);
814 need_re_stat = true;
817 if (need_re_stat) {
818 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
819 /* If we have an fd, this stat should succeed. */
820 if (ret == -1) {
821 DEBUG(0,("Error doing fstat on open file %s "
822 "(%s)\n",
823 smb_fname_str_dbg(smb_fname),
824 strerror(errno) ));
828 notify_fname(conn, NOTIFY_ACTION_ADDED,
829 FILE_NOTIFY_CHANGE_FILE_NAME,
830 smb_fname->base_name);
832 } else {
833 fsp->fh->fd = -1; /* What we used to call a stat open. */
834 if (!file_existed) {
835 /* File must exist for a stat open. */
836 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
839 status = smbd_check_access_rights(conn,
840 smb_fname,
841 false,
842 access_mask);
844 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
845 fsp->posix_open &&
846 S_ISLNK(smb_fname->st.st_ex_mode)) {
847 /* This is a POSIX stat open for delete
848 * or rename on a symlink that points
849 * nowhere. Allow. */
850 DEBUG(10,("open_file: allowing POSIX "
851 "open on bad symlink %s\n",
852 smb_fname_str_dbg(smb_fname)));
853 status = NT_STATUS_OK;
856 if (!NT_STATUS_IS_OK(status)) {
857 DEBUG(10,("open_file: "
858 "smbd_check_access_rights on file "
859 "%s returned %s\n",
860 smb_fname_str_dbg(smb_fname),
861 nt_errstr(status) ));
862 return status;
867 * POSIX allows read-only opens of directories. We don't
868 * want to do this (we use a different code path for this)
869 * so catch a directory open and return an EISDIR. JRA.
872 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
873 fd_close(fsp);
874 errno = EISDIR;
875 return NT_STATUS_FILE_IS_A_DIRECTORY;
878 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
879 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
880 fsp->file_pid = req ? req->smbpid : 0;
881 fsp->can_lock = True;
882 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
883 fsp->can_write =
884 CAN_WRITE(conn) &&
885 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
886 fsp->print_file = NULL;
887 fsp->modified = False;
888 fsp->sent_oplock_break = NO_BREAK_SENT;
889 fsp->is_directory = False;
890 if (conn->aio_write_behind_list &&
891 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
892 conn->case_sensitive)) {
893 fsp->aio_write_behind = True;
896 fsp->wcp = NULL; /* Write cache pointer. */
898 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
899 conn->session_info->unix_info->unix_name,
900 smb_fname_str_dbg(smb_fname),
901 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
902 conn->num_files_open));
904 errno = 0;
905 return NT_STATUS_OK;
908 /****************************************************************************
909 Check if we can open a file with a share mode.
910 Returns True if conflict, False if not.
911 ****************************************************************************/
913 static bool share_conflict(struct share_mode_entry *entry,
914 uint32 access_mask,
915 uint32 share_access)
917 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
918 "entry->share_access = 0x%x, "
919 "entry->private_options = 0x%x\n",
920 (unsigned int)entry->access_mask,
921 (unsigned int)entry->share_access,
922 (unsigned int)entry->private_options));
924 if (server_id_is_disconnected(&entry->pid)) {
926 * note: cleanup should have been done by
927 * delay_for_batch_oplocks()
929 return false;
932 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
933 (unsigned int)access_mask, (unsigned int)share_access));
935 if ((entry->access_mask & (FILE_WRITE_DATA|
936 FILE_APPEND_DATA|
937 FILE_READ_DATA|
938 FILE_EXECUTE|
939 DELETE_ACCESS)) == 0) {
940 DEBUG(10,("share_conflict: No conflict due to "
941 "entry->access_mask = 0x%x\n",
942 (unsigned int)entry->access_mask ));
943 return False;
946 if ((access_mask & (FILE_WRITE_DATA|
947 FILE_APPEND_DATA|
948 FILE_READ_DATA|
949 FILE_EXECUTE|
950 DELETE_ACCESS)) == 0) {
951 DEBUG(10,("share_conflict: No conflict due to "
952 "access_mask = 0x%x\n",
953 (unsigned int)access_mask ));
954 return False;
957 #if 1 /* JRA TEST - Superdebug. */
958 #define CHECK_MASK(num, am, right, sa, share) \
959 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
960 (unsigned int)(num), (unsigned int)(am), \
961 (unsigned int)(right), (unsigned int)(am)&(right) )); \
962 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
963 (unsigned int)(num), (unsigned int)(sa), \
964 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
965 if (((am) & (right)) && !((sa) & (share))) { \
966 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
967 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
968 (unsigned int)(share) )); \
969 return True; \
971 #else
972 #define CHECK_MASK(num, am, right, sa, share) \
973 if (((am) & (right)) && !((sa) & (share))) { \
974 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
975 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
976 (unsigned int)(share) )); \
977 return True; \
979 #endif
981 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
982 share_access, FILE_SHARE_WRITE);
983 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
984 entry->share_access, FILE_SHARE_WRITE);
986 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
987 share_access, FILE_SHARE_READ);
988 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
989 entry->share_access, FILE_SHARE_READ);
991 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
992 share_access, FILE_SHARE_DELETE);
993 CHECK_MASK(6, access_mask, DELETE_ACCESS,
994 entry->share_access, FILE_SHARE_DELETE);
996 DEBUG(10,("share_conflict: No conflict.\n"));
997 return False;
1000 #if defined(DEVELOPER)
1001 static void validate_my_share_entries(struct smbd_server_connection *sconn,
1002 int num,
1003 struct share_mode_entry *share_entry)
1005 struct server_id self = messaging_server_id(sconn->msg_ctx);
1006 files_struct *fsp;
1008 if (!serverid_equal(&self, &share_entry->pid)) {
1009 return;
1012 if (is_deferred_open_entry(share_entry) &&
1013 !open_was_deferred(sconn, share_entry->op_mid)) {
1014 char *str = talloc_asprintf(talloc_tos(),
1015 "Got a deferred entry without a request: "
1016 "PANIC: %s\n",
1017 share_mode_str(talloc_tos(), num, share_entry));
1018 smb_panic(str);
1021 if (!is_valid_share_mode_entry(share_entry)) {
1022 return;
1025 fsp = file_find_dif(sconn, share_entry->id,
1026 share_entry->share_file_id);
1027 if (!fsp) {
1028 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1029 share_mode_str(talloc_tos(), num, share_entry) ));
1030 smb_panic("validate_my_share_entries: Cannot match a "
1031 "share entry with an open file\n");
1034 if (is_deferred_open_entry(share_entry)) {
1035 goto panic;
1038 if ((share_entry->op_type == NO_OPLOCK) &&
1039 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
1040 /* Someone has already written to it, but I haven't yet
1041 * noticed */
1042 return;
1045 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
1046 goto panic;
1049 return;
1051 panic:
1053 char *str;
1054 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1055 share_mode_str(talloc_tos(), num, share_entry) ));
1056 str = talloc_asprintf(talloc_tos(),
1057 "validate_my_share_entries: "
1058 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1059 fsp->fsp_name->base_name,
1060 (unsigned int)fsp->oplock_type,
1061 (unsigned int)share_entry->op_type );
1062 smb_panic(str);
1065 #endif
1067 bool is_stat_open(uint32 access_mask)
1069 const uint32_t stat_open_bits =
1070 (SYNCHRONIZE_ACCESS|
1071 FILE_READ_ATTRIBUTES|
1072 FILE_WRITE_ATTRIBUTES);
1074 return (access_mask &&
1075 ((access_mask & ~stat_open_bits) == 0) &&
1076 ((access_mask & stat_open_bits) != 0));
1079 /****************************************************************************
1080 Deal with share modes
1081 Invarient: Share mode must be locked on entry and exit.
1082 Returns -1 on error, or number of share modes on success (may be zero).
1083 ****************************************************************************/
1085 static NTSTATUS open_mode_check(connection_struct *conn,
1086 struct share_mode_lock *lck,
1087 uint32_t name_hash,
1088 uint32 access_mask,
1089 uint32 share_access,
1090 uint32 create_options,
1091 bool *file_existed)
1093 int i;
1095 if(lck->data->num_share_modes == 0) {
1096 return NT_STATUS_OK;
1099 /* A delete on close prohibits everything */
1101 if (is_delete_on_close_set(lck, name_hash)) {
1103 * Check the delete on close token
1104 * is valid. It could have been left
1105 * after a server crash.
1107 for(i = 0; i < lck->data->num_share_modes; i++) {
1108 if (!share_mode_stale_pid(lck->data, i)) {
1110 *file_existed = true;
1112 return NT_STATUS_DELETE_PENDING;
1115 return NT_STATUS_OK;
1118 if (is_stat_open(access_mask)) {
1119 /* Stat open that doesn't trigger oplock breaks or share mode
1120 * checks... ! JRA. */
1121 return NT_STATUS_OK;
1125 * Check if the share modes will give us access.
1128 #if defined(DEVELOPER)
1129 for(i = 0; i < lck->data->num_share_modes; i++) {
1130 validate_my_share_entries(conn->sconn, i,
1131 &lck->data->share_modes[i]);
1133 #endif
1135 /* Now we check the share modes, after any oplock breaks. */
1136 for(i = 0; i < lck->data->num_share_modes; i++) {
1138 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1139 continue;
1142 /* someone else has a share lock on it, check to see if we can
1143 * too */
1144 if (share_conflict(&lck->data->share_modes[i],
1145 access_mask, share_access)) {
1147 if (share_mode_stale_pid(lck->data, i)) {
1148 continue;
1151 *file_existed = true;
1153 return NT_STATUS_SHARING_VIOLATION;
1157 if (lck->data->num_share_modes != 0) {
1158 *file_existed = true;
1161 return NT_STATUS_OK;
1165 * Send a break message to the oplock holder and delay the open for
1166 * our client.
1169 static NTSTATUS send_break_message(files_struct *fsp,
1170 struct share_mode_entry *exclusive,
1171 uint64_t mid,
1172 int oplock_request)
1174 NTSTATUS status;
1175 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1177 DEBUG(10, ("Sending break request to PID %s\n",
1178 procid_str_static(&exclusive->pid)));
1179 exclusive->op_mid = mid;
1181 /* Create the message. */
1182 share_mode_entry_to_message(msg, exclusive);
1184 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1185 don't want this set in the share mode struct pointed to by lck. */
1187 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1188 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1189 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1192 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1193 MSG_SMB_BREAK_REQUEST,
1194 (uint8 *)msg,
1195 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1196 if (!NT_STATUS_IS_OK(status)) {
1197 DEBUG(3, ("Could not send oplock break message: %s\n",
1198 nt_errstr(status)));
1201 return status;
1205 * Return share_mode_entry pointers for :
1206 * 1). Batch oplock entry.
1207 * 2). Batch or exclusive oplock entry (may be identical to #1).
1208 * bool have_level2_oplock
1209 * bool have_no_oplock.
1210 * Do internal consistency checks on the share mode for a file.
1213 static void find_oplock_types(files_struct *fsp,
1214 int oplock_request,
1215 const struct share_mode_lock *lck,
1216 struct share_mode_entry **pp_batch,
1217 struct share_mode_entry **pp_ex_or_batch,
1218 bool *got_level2,
1219 bool *got_no_oplock)
1221 int i;
1223 *pp_batch = NULL;
1224 *pp_ex_or_batch = NULL;
1225 *got_level2 = false;
1226 *got_no_oplock = false;
1228 /* Ignore stat or internal opens, as is done in
1229 delay_for_batch_oplocks() and
1230 delay_for_exclusive_oplocks().
1232 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1233 return;
1236 for (i=0; i<lck->data->num_share_modes; i++) {
1237 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1238 continue;
1241 if (lck->data->share_modes[i].op_type == NO_OPLOCK &&
1242 is_stat_open(lck->data->share_modes[i].access_mask)) {
1243 /* We ignore stat opens in the table - they
1244 always have NO_OPLOCK and never get or
1245 cause breaks. JRA. */
1246 continue;
1249 if (BATCH_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1250 /* batch - can only be one. */
1251 if (share_mode_stale_pid(lck->data, i)) {
1252 DEBUG(10, ("Found stale batch oplock\n"));
1253 continue;
1255 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1256 smb_panic("Bad batch oplock entry.");
1258 *pp_batch = &lck->data->share_modes[i];
1261 if (EXCLUSIVE_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1262 if (share_mode_stale_pid(lck->data, i)) {
1263 DEBUG(10, ("Found stale duplicate oplock\n"));
1264 continue;
1266 /* Exclusive or batch - can only be one. */
1267 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1268 smb_panic("Bad exclusive or batch oplock entry.");
1270 *pp_ex_or_batch = &lck->data->share_modes[i];
1273 if (LEVEL_II_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1274 if (*pp_batch || *pp_ex_or_batch) {
1275 if (share_mode_stale_pid(lck->data, i)) {
1276 DEBUG(10, ("Found stale LevelII "
1277 "oplock\n"));
1278 continue;
1280 smb_panic("Bad levelII oplock entry.");
1282 *got_level2 = true;
1285 if (lck->data->share_modes[i].op_type == NO_OPLOCK) {
1286 if (*pp_batch || *pp_ex_or_batch) {
1287 if (share_mode_stale_pid(lck->data, i)) {
1288 DEBUG(10, ("Found stale NO_OPLOCK "
1289 "entry\n"));
1290 continue;
1292 smb_panic("Bad no oplock entry.");
1294 *got_no_oplock = true;
1299 static bool delay_for_batch_oplocks(files_struct *fsp,
1300 uint64_t mid,
1301 int oplock_request,
1302 struct share_mode_entry *batch_entry)
1304 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1305 return false;
1307 if (batch_entry == NULL) {
1308 return false;
1311 if (server_id_is_disconnected(&batch_entry->pid)) {
1313 * TODO: clean up.
1314 * This could be achieved by sending a break message
1315 * to ourselves. Special considerations for files
1316 * with delete_on_close flag set!
1318 * For now we keep it simple and do not
1319 * allow delete on close for durable handles.
1321 return false;
1324 /* Found a batch oplock */
1325 send_break_message(fsp, batch_entry, mid, oplock_request);
1326 return true;
1329 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1330 uint64_t mid,
1331 int oplock_request,
1332 struct share_mode_entry *ex_entry)
1334 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1335 return false;
1337 if (ex_entry == NULL) {
1338 return false;
1341 if (server_id_is_disconnected(&ex_entry->pid)) {
1343 * since only durable handles can get disconnected,
1344 * and we can only get durable handles with batch oplocks,
1345 * this should actually never be reached...
1347 return false;
1350 send_break_message(fsp, ex_entry, mid, oplock_request);
1351 return true;
1354 static bool file_has_brlocks(files_struct *fsp)
1356 struct byte_range_lock *br_lck;
1358 br_lck = brl_get_locks_readonly(fsp);
1359 if (!br_lck)
1360 return false;
1362 return br_lck->num_locks > 0 ? true : false;
1365 static void grant_fsp_oplock_type(files_struct *fsp,
1366 int oplock_request,
1367 bool got_level2_oplock,
1368 bool got_a_none_oplock)
1370 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1371 lp_level2_oplocks(SNUM(fsp->conn));
1373 /* Start by granting what the client asked for,
1374 but ensure no SAMBA_PRIVATE bits can be set. */
1375 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1377 if (oplock_request & INTERNAL_OPEN_ONLY) {
1378 /* No oplocks on internal open. */
1379 fsp->oplock_type = NO_OPLOCK;
1380 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1381 fsp->oplock_type, fsp_str_dbg(fsp)));
1382 return;
1385 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1386 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1387 fsp_str_dbg(fsp)));
1388 fsp->oplock_type = NO_OPLOCK;
1391 if (is_stat_open(fsp->access_mask)) {
1392 /* Leave the value already set. */
1393 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1394 fsp->oplock_type, fsp_str_dbg(fsp)));
1395 return;
1399 * Match what was requested (fsp->oplock_type) with
1400 * what was found in the existing share modes.
1403 if (got_a_none_oplock) {
1404 fsp->oplock_type = NO_OPLOCK;
1405 } else if (got_level2_oplock) {
1406 if (fsp->oplock_type == NO_OPLOCK ||
1407 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1408 /* Store a level2 oplock, but don't tell the client */
1409 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1410 } else {
1411 fsp->oplock_type = LEVEL_II_OPLOCK;
1413 } else {
1414 /* All share_mode_entries are placeholders or deferred.
1415 * Silently upgrade to fake levelII if the client didn't
1416 * ask for an oplock. */
1417 if (fsp->oplock_type == NO_OPLOCK) {
1418 /* Store a level2 oplock, but don't tell the client */
1419 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1424 * Don't grant level2 to clients that don't want them
1425 * or if we've turned them off.
1427 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1428 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1431 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1432 fsp->oplock_type, fsp_str_dbg(fsp)));
1435 static bool request_timed_out(struct timeval request_time,
1436 struct timeval timeout)
1438 struct timeval now, end_time;
1439 GetTimeOfDay(&now);
1440 end_time = timeval_sum(&request_time, &timeout);
1441 return (timeval_compare(&end_time, &now) < 0);
1444 /****************************************************************************
1445 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1446 ****************************************************************************/
1448 static void defer_open(struct share_mode_lock *lck,
1449 struct timeval request_time,
1450 struct timeval timeout,
1451 struct smb_request *req,
1452 struct deferred_open_record *state)
1454 struct server_id self = messaging_server_id(req->sconn->msg_ctx);
1456 /* Paranoia check */
1458 if (lck) {
1459 int i;
1461 for (i=0; i<lck->data->num_share_modes; i++) {
1462 struct share_mode_entry *e = &lck->data->share_modes[i];
1464 if (is_deferred_open_entry(e) &&
1465 serverid_equal(&self, &e->pid) &&
1466 (e->op_mid == req->mid)) {
1467 DEBUG(0, ("Trying to defer an already deferred "
1468 "request: mid=%llu, exiting\n",
1469 (unsigned long long)req->mid));
1470 TALLOC_FREE(lck);
1471 exit_server("attempt to defer a deferred request");
1476 /* End paranoia check */
1478 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1479 "open entry for mid %llu\n",
1480 (unsigned int)request_time.tv_sec,
1481 (unsigned int)request_time.tv_usec,
1482 (unsigned long long)req->mid));
1484 if (!push_deferred_open_message_smb(req, request_time, timeout,
1485 state->id, (char *)state, sizeof(*state))) {
1486 TALLOC_FREE(lck);
1487 exit_server("push_deferred_open_message_smb failed");
1489 if (lck) {
1490 add_deferred_open(lck, req->mid, request_time, self, state->id);
1495 /****************************************************************************
1496 On overwrite open ensure that the attributes match.
1497 ****************************************************************************/
1499 static bool open_match_attributes(connection_struct *conn,
1500 uint32 old_dos_attr,
1501 uint32 new_dos_attr,
1502 mode_t existing_unx_mode,
1503 mode_t new_unx_mode,
1504 mode_t *returned_unx_mode)
1506 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1508 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1509 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1511 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1512 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1513 *returned_unx_mode = new_unx_mode;
1514 } else {
1515 *returned_unx_mode = (mode_t)0;
1518 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1519 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1520 "returned_unx_mode = 0%o\n",
1521 (unsigned int)old_dos_attr,
1522 (unsigned int)existing_unx_mode,
1523 (unsigned int)new_dos_attr,
1524 (unsigned int)*returned_unx_mode ));
1526 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1527 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1528 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1529 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1530 return False;
1533 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1534 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1535 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1536 return False;
1539 return True;
1542 /****************************************************************************
1543 Special FCB or DOS processing in the case of a sharing violation.
1544 Try and find a duplicated file handle.
1545 ****************************************************************************/
1547 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1548 connection_struct *conn,
1549 files_struct *fsp_to_dup_into,
1550 const struct smb_filename *smb_fname,
1551 struct file_id id,
1552 uint16 file_pid,
1553 uint64_t vuid,
1554 uint32 access_mask,
1555 uint32 share_access,
1556 uint32 create_options)
1558 files_struct *fsp;
1560 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1561 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1563 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1564 fsp = file_find_di_next(fsp)) {
1566 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1567 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1568 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1569 fsp->fh->fd, (unsigned long long)fsp->vuid,
1570 (unsigned int)fsp->file_pid,
1571 (unsigned int)fsp->fh->private_options,
1572 (unsigned int)fsp->access_mask ));
1574 if (fsp->fh->fd != -1 &&
1575 fsp->vuid == vuid &&
1576 fsp->file_pid == file_pid &&
1577 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1578 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1579 (fsp->access_mask & FILE_WRITE_DATA) &&
1580 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1581 strequal(fsp->fsp_name->stream_name,
1582 smb_fname->stream_name)) {
1583 DEBUG(10,("fcb_or_dos_open: file match\n"));
1584 break;
1588 if (!fsp) {
1589 return NT_STATUS_NOT_FOUND;
1592 /* quite an insane set of semantics ... */
1593 if (is_executable(smb_fname->base_name) &&
1594 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1595 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1596 return NT_STATUS_INVALID_PARAMETER;
1599 /* We need to duplicate this fsp. */
1600 return dup_file_fsp(req, fsp, access_mask, share_access,
1601 create_options, fsp_to_dup_into);
1604 static void schedule_defer_open(struct share_mode_lock *lck,
1605 struct timeval request_time,
1606 struct smb_request *req)
1608 struct deferred_open_record state;
1610 /* This is a relative time, added to the absolute
1611 request_time value to get the absolute timeout time.
1612 Note that if this is the second or greater time we enter
1613 this codepath for this particular request mid then
1614 request_time is left as the absolute time of the *first*
1615 time this request mid was processed. This is what allows
1616 the request to eventually time out. */
1618 struct timeval timeout;
1620 /* Normally the smbd we asked should respond within
1621 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1622 * the client did, give twice the timeout as a safety
1623 * measure here in case the other smbd is stuck
1624 * somewhere else. */
1626 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1628 /* Nothing actually uses state.delayed_for_oplocks
1629 but it's handy to differentiate in debug messages
1630 between a 30 second delay due to oplock break, and
1631 a 1 second delay for share mode conflicts. */
1633 state.delayed_for_oplocks = True;
1634 state.async_open = false;
1635 state.id = lck->data->id;
1637 if (!request_timed_out(request_time, timeout)) {
1638 defer_open(lck, request_time, timeout, req, &state);
1642 /****************************************************************************
1643 Reschedule an open call that went asynchronous.
1644 ****************************************************************************/
1646 static void schedule_async_open(struct timeval request_time,
1647 struct smb_request *req)
1649 struct deferred_open_record state;
1650 struct timeval timeout;
1652 timeout = timeval_set(20, 0);
1654 ZERO_STRUCT(state);
1655 state.delayed_for_oplocks = false;
1656 state.async_open = true;
1658 if (!request_timed_out(request_time, timeout)) {
1659 defer_open(NULL, request_time, timeout, req, &state);
1663 /****************************************************************************
1664 Work out what access_mask to use from what the client sent us.
1665 ****************************************************************************/
1667 static NTSTATUS smbd_calculate_maximum_allowed_access(
1668 connection_struct *conn,
1669 const struct smb_filename *smb_fname,
1670 bool use_privs,
1671 uint32_t *p_access_mask)
1673 struct security_descriptor *sd;
1674 uint32_t access_granted;
1675 NTSTATUS status;
1677 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
1678 *p_access_mask |= FILE_GENERIC_ALL;
1679 return NT_STATUS_OK;
1682 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1683 (SECINFO_OWNER |
1684 SECINFO_GROUP |
1685 SECINFO_DACL),&sd);
1687 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1689 * File did not exist
1691 *p_access_mask = FILE_GENERIC_ALL;
1692 return NT_STATUS_OK;
1694 if (!NT_STATUS_IS_OK(status)) {
1695 DEBUG(10,("Could not get acl on file %s: %s\n",
1696 smb_fname_str_dbg(smb_fname),
1697 nt_errstr(status)));
1698 return NT_STATUS_ACCESS_DENIED;
1702 * Never test FILE_READ_ATTRIBUTES. se_file_access_check()
1703 * also takes care of owner WRITE_DAC and READ_CONTROL.
1705 status = se_file_access_check(sd,
1706 get_current_nttok(conn),
1707 use_privs,
1708 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1709 &access_granted);
1711 TALLOC_FREE(sd);
1713 if (!NT_STATUS_IS_OK(status)) {
1714 DEBUG(10, ("Access denied on file %s: "
1715 "when calculating maximum access\n",
1716 smb_fname_str_dbg(smb_fname)));
1717 return NT_STATUS_ACCESS_DENIED;
1719 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1721 if (!(access_granted & DELETE_ACCESS)) {
1722 if (can_delete_file_in_directory(conn, smb_fname)) {
1723 *p_access_mask |= DELETE_ACCESS;
1727 return NT_STATUS_OK;
1730 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1731 const struct smb_filename *smb_fname,
1732 bool use_privs,
1733 uint32_t access_mask,
1734 uint32_t *access_mask_out)
1736 NTSTATUS status;
1737 uint32_t orig_access_mask = access_mask;
1738 uint32_t rejected_share_access;
1741 * Convert GENERIC bits to specific bits.
1744 se_map_generic(&access_mask, &file_generic_mapping);
1746 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1747 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1749 status = smbd_calculate_maximum_allowed_access(
1750 conn, smb_fname, use_privs, &access_mask);
1752 if (!NT_STATUS_IS_OK(status)) {
1753 return status;
1756 access_mask &= conn->share_access;
1759 rejected_share_access = access_mask & ~(conn->share_access);
1761 if (rejected_share_access) {
1762 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1763 "file %s: rejected by share access mask[0x%08X] "
1764 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1765 smb_fname_str_dbg(smb_fname),
1766 conn->share_access,
1767 orig_access_mask, access_mask,
1768 rejected_share_access));
1769 return NT_STATUS_ACCESS_DENIED;
1772 *access_mask_out = access_mask;
1773 return NT_STATUS_OK;
1776 /****************************************************************************
1777 Remove the deferred open entry under lock.
1778 ****************************************************************************/
1780 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1781 struct server_id pid)
1783 struct share_mode_lock *lck = get_existing_share_mode_lock(
1784 talloc_tos(), id);
1785 if (lck == NULL) {
1786 DEBUG(0, ("could not get share mode lock\n"));
1787 return;
1789 del_deferred_open_entry(lck, mid, pid);
1790 TALLOC_FREE(lck);
1793 /****************************************************************************
1794 Return true if this is a state pointer to an asynchronous create.
1795 ****************************************************************************/
1797 bool is_deferred_open_async(const void *ptr)
1799 const struct deferred_open_record *state = (const struct deferred_open_record *)ptr;
1801 return state->async_open;
1804 static bool clear_ads(uint32_t create_disposition)
1806 bool ret = false;
1808 switch (create_disposition) {
1809 case FILE_SUPERSEDE:
1810 case FILE_OVERWRITE_IF:
1811 case FILE_OVERWRITE:
1812 ret = true;
1813 break;
1814 default:
1815 break;
1817 return ret;
1820 static int disposition_to_open_flags(uint32_t create_disposition)
1822 int ret = 0;
1825 * Currently we're using FILE_SUPERSEDE as the same as
1826 * FILE_OVERWRITE_IF but they really are
1827 * different. FILE_SUPERSEDE deletes an existing file
1828 * (requiring delete access) then recreates it.
1831 switch (create_disposition) {
1832 case FILE_SUPERSEDE:
1833 case FILE_OVERWRITE_IF:
1835 * If file exists replace/overwrite. If file doesn't
1836 * exist create.
1838 ret = O_CREAT|O_TRUNC;
1839 break;
1841 case FILE_OPEN:
1843 * If file exists open. If file doesn't exist error.
1845 ret = 0;
1846 break;
1848 case FILE_OVERWRITE:
1850 * If file exists overwrite. If file doesn't exist
1851 * error.
1853 ret = O_TRUNC;
1854 break;
1856 case FILE_CREATE:
1858 * If file exists error. If file doesn't exist create.
1860 ret = O_CREAT|O_EXCL;
1861 break;
1863 case FILE_OPEN_IF:
1865 * If file exists open. If file doesn't exist create.
1867 ret = O_CREAT;
1868 break;
1870 return ret;
1873 /****************************************************************************
1874 Open a file with a share mode. Passed in an already created files_struct *.
1875 ****************************************************************************/
1877 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1878 struct smb_request *req,
1879 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1880 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1881 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1882 uint32 create_options, /* options such as delete on close. */
1883 uint32 new_dos_attributes, /* attributes used for new file. */
1884 int oplock_request, /* internal Samba oplock codes. */
1885 /* Information (FILE_EXISTS etc.) */
1886 uint32_t private_flags, /* Samba specific flags. */
1887 int *pinfo,
1888 files_struct *fsp)
1890 struct smb_filename *smb_fname = fsp->fsp_name;
1891 int flags=0;
1892 int flags2=0;
1893 bool file_existed = VALID_STAT(smb_fname->st);
1894 bool def_acl = False;
1895 bool posix_open = False;
1896 bool new_file_created = False;
1897 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1898 mode_t new_unx_mode = (mode_t)0;
1899 mode_t unx_mode = (mode_t)0;
1900 int info;
1901 uint32 existing_dos_attributes = 0;
1902 struct timeval request_time = timeval_zero();
1903 struct share_mode_lock *lck = NULL;
1904 uint32 open_access_mask = access_mask;
1905 NTSTATUS status;
1906 char *parent_dir;
1907 SMB_STRUCT_STAT saved_stat = smb_fname->st;
1909 if (conn->printer) {
1911 * Printers are handled completely differently.
1912 * Most of the passed parameters are ignored.
1915 if (pinfo) {
1916 *pinfo = FILE_WAS_CREATED;
1919 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1920 smb_fname_str_dbg(smb_fname)));
1922 if (!req) {
1923 DEBUG(0,("open_file_ntcreate: printer open without "
1924 "an SMB request!\n"));
1925 return NT_STATUS_INTERNAL_ERROR;
1928 return print_spool_open(fsp, smb_fname->base_name,
1929 req->vuid);
1932 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1933 NULL)) {
1934 return NT_STATUS_NO_MEMORY;
1937 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1938 posix_open = True;
1939 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1940 new_dos_attributes = 0;
1941 } else {
1942 /* Windows allows a new file to be created and
1943 silently removes a FILE_ATTRIBUTE_DIRECTORY
1944 sent by the client. Do the same. */
1946 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1948 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1949 * created new. */
1950 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1951 smb_fname, parent_dir);
1954 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1955 "access_mask=0x%x share_access=0x%x "
1956 "create_disposition = 0x%x create_options=0x%x "
1957 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1958 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1959 access_mask, share_access, create_disposition,
1960 create_options, (unsigned int)unx_mode, oplock_request,
1961 (unsigned int)private_flags));
1963 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1964 DEBUG(0, ("No smb request but not an internal only open!\n"));
1965 return NT_STATUS_INTERNAL_ERROR;
1969 * Only non-internal opens can be deferred at all
1972 if (req) {
1973 void *ptr;
1974 if (get_deferred_open_message_state(req,
1975 &request_time,
1976 &ptr)) {
1977 /* Remember the absolute time of the original
1978 request with this mid. We'll use it later to
1979 see if this has timed out. */
1981 /* If it was an async create retry, the file
1982 didn't exist. */
1984 if (is_deferred_open_async(ptr)) {
1985 SET_STAT_INVALID(smb_fname->st);
1986 file_existed = false;
1987 } else {
1988 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1989 /* Remove the deferred open entry under lock. */
1990 remove_deferred_open_entry(
1991 state->id, req->mid,
1992 messaging_server_id(req->sconn->msg_ctx));
1995 /* Ensure we don't reprocess this message. */
1996 remove_deferred_open_message_smb(req->sconn, req->mid);
2000 if (!posix_open) {
2001 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
2002 if (file_existed) {
2003 existing_dos_attributes = dos_mode(conn, smb_fname);
2007 /* ignore any oplock requests if oplocks are disabled */
2008 if (!lp_oplocks(SNUM(conn)) ||
2009 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
2010 /* Mask off everything except the private Samba bits. */
2011 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2014 /* this is for OS/2 long file names - say we don't support them */
2015 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
2016 /* OS/2 Workplace shell fix may be main code stream in a later
2017 * release. */
2018 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2019 "supported.\n"));
2020 if (use_nt_status()) {
2021 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2023 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2026 switch( create_disposition ) {
2027 case FILE_OPEN:
2028 /* If file exists open. If file doesn't exist error. */
2029 if (!file_existed) {
2030 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2031 "requested for file %s and file "
2032 "doesn't exist.\n",
2033 smb_fname_str_dbg(smb_fname)));
2034 errno = ENOENT;
2035 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2037 break;
2039 case FILE_OVERWRITE:
2040 /* If file exists overwrite. If file doesn't exist
2041 * error. */
2042 if (!file_existed) {
2043 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2044 "requested for file %s and file "
2045 "doesn't exist.\n",
2046 smb_fname_str_dbg(smb_fname) ));
2047 errno = ENOENT;
2048 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2050 break;
2052 case FILE_CREATE:
2053 /* If file exists error. If file doesn't exist
2054 * create. */
2055 if (file_existed) {
2056 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2057 "requested for file %s and file "
2058 "already exists.\n",
2059 smb_fname_str_dbg(smb_fname)));
2060 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2061 errno = EISDIR;
2062 } else {
2063 errno = EEXIST;
2065 return map_nt_error_from_unix(errno);
2067 break;
2069 case FILE_SUPERSEDE:
2070 case FILE_OVERWRITE_IF:
2071 case FILE_OPEN_IF:
2072 break;
2073 default:
2074 return NT_STATUS_INVALID_PARAMETER;
2077 flags2 = disposition_to_open_flags(create_disposition);
2079 /* We only care about matching attributes on file exists and
2080 * overwrite. */
2082 if (!posix_open && file_existed &&
2083 ((create_disposition == FILE_OVERWRITE) ||
2084 (create_disposition == FILE_OVERWRITE_IF))) {
2085 if (!open_match_attributes(conn, existing_dos_attributes,
2086 new_dos_attributes,
2087 smb_fname->st.st_ex_mode,
2088 unx_mode, &new_unx_mode)) {
2089 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2090 "for file %s (%x %x) (0%o, 0%o)\n",
2091 smb_fname_str_dbg(smb_fname),
2092 existing_dos_attributes,
2093 new_dos_attributes,
2094 (unsigned int)smb_fname->st.st_ex_mode,
2095 (unsigned int)unx_mode ));
2096 errno = EACCES;
2097 return NT_STATUS_ACCESS_DENIED;
2101 status = smbd_calculate_access_mask(conn, smb_fname,
2102 false,
2103 access_mask,
2104 &access_mask);
2105 if (!NT_STATUS_IS_OK(status)) {
2106 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2107 "on file %s returned %s\n",
2108 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2109 return status;
2112 open_access_mask = access_mask;
2114 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
2115 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2118 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2119 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2120 access_mask));
2123 * Note that we ignore the append flag as append does not
2124 * mean the same thing under DOS and Unix.
2127 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
2128 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
2129 /* DENY_DOS opens are always underlying read-write on the
2130 file handle, no matter what the requested access mask
2131 says. */
2132 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
2133 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
2134 FILE_READ_EA|FILE_EXECUTE)) {
2135 flags = O_RDWR;
2136 } else {
2137 flags = O_WRONLY;
2139 } else {
2140 flags = O_RDONLY;
2144 * Currently we only look at FILE_WRITE_THROUGH for create options.
2147 #if defined(O_SYNC)
2148 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2149 flags2 |= O_SYNC;
2151 #endif /* O_SYNC */
2153 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2154 flags2 |= O_APPEND;
2157 if (!posix_open && !CAN_WRITE(conn)) {
2159 * We should really return a permission denied error if either
2160 * O_CREAT or O_TRUNC are set, but for compatibility with
2161 * older versions of Samba we just AND them out.
2163 flags2 &= ~(O_CREAT|O_TRUNC);
2167 * Ensure we can't write on a read-only share or file.
2170 if (flags != O_RDONLY && file_existed &&
2171 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2172 DEBUG(5,("open_file_ntcreate: write access requested for "
2173 "file %s on read only %s\n",
2174 smb_fname_str_dbg(smb_fname),
2175 !CAN_WRITE(conn) ? "share" : "file" ));
2176 errno = EACCES;
2177 return NT_STATUS_ACCESS_DENIED;
2180 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2181 fsp->share_access = share_access;
2182 fsp->fh->private_options = private_flags;
2183 fsp->access_mask = open_access_mask; /* We change this to the
2184 * requested access_mask after
2185 * the open is done. */
2186 fsp->posix_open = posix_open;
2188 /* Ensure no SAMBA_PRIVATE bits can be set. */
2189 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2191 if (timeval_is_zero(&request_time)) {
2192 request_time = fsp->open_time;
2195 if (file_existed) {
2196 struct share_mode_entry *batch_entry = NULL;
2197 struct share_mode_entry *exclusive_entry = NULL;
2198 bool got_level2_oplock = false;
2199 bool got_a_none_oplock = false;
2200 struct file_id id;
2202 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2203 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2205 lck = get_share_mode_lock(talloc_tos(), id,
2206 conn->connectpath,
2207 smb_fname, &old_write_time);
2208 if (lck == NULL) {
2209 DEBUG(0, ("Could not get share mode lock\n"));
2210 return NT_STATUS_SHARING_VIOLATION;
2213 /* Get the types we need to examine. */
2214 find_oplock_types(fsp,
2215 oplock_request,
2216 lck,
2217 &batch_entry,
2218 &exclusive_entry,
2219 &got_level2_oplock,
2220 &got_a_none_oplock);
2222 /* First pass - send break only on batch oplocks. */
2223 if ((req != NULL) &&
2224 delay_for_batch_oplocks(fsp,
2225 req->mid,
2226 oplock_request,
2227 batch_entry)) {
2228 schedule_defer_open(lck, request_time, req);
2229 TALLOC_FREE(lck);
2230 return NT_STATUS_SHARING_VIOLATION;
2233 /* Use the client requested access mask here, not the one we
2234 * open with. */
2235 status = open_mode_check(conn, lck, fsp->name_hash,
2236 access_mask, share_access,
2237 create_options, &file_existed);
2239 if (NT_STATUS_IS_OK(status)) {
2240 /* We might be going to allow this open. Check oplock
2241 * status again. */
2242 /* Second pass - send break for both batch or
2243 * exclusive oplocks. */
2244 if ((req != NULL) &&
2245 delay_for_exclusive_oplocks(
2246 fsp,
2247 req->mid,
2248 oplock_request,
2249 exclusive_entry)) {
2250 schedule_defer_open(lck, request_time, req);
2251 TALLOC_FREE(lck);
2252 return NT_STATUS_SHARING_VIOLATION;
2256 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2257 /* DELETE_PENDING is not deferred for a second */
2258 TALLOC_FREE(lck);
2259 return status;
2262 grant_fsp_oplock_type(fsp,
2263 oplock_request,
2264 got_level2_oplock,
2265 got_a_none_oplock);
2267 if (!NT_STATUS_IS_OK(status)) {
2268 uint32 can_access_mask;
2269 bool can_access = True;
2271 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2273 /* Check if this can be done with the deny_dos and fcb
2274 * calls. */
2275 if (private_flags &
2276 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2277 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2278 if (req == NULL) {
2279 DEBUG(0, ("DOS open without an SMB "
2280 "request!\n"));
2281 TALLOC_FREE(lck);
2282 return NT_STATUS_INTERNAL_ERROR;
2285 /* Use the client requested access mask here,
2286 * not the one we open with. */
2287 status = fcb_or_dos_open(req,
2288 conn,
2289 fsp,
2290 smb_fname,
2292 req->smbpid,
2293 req->vuid,
2294 access_mask,
2295 share_access,
2296 create_options);
2298 if (NT_STATUS_IS_OK(status)) {
2299 TALLOC_FREE(lck);
2300 if (pinfo) {
2301 *pinfo = FILE_WAS_OPENED;
2303 return NT_STATUS_OK;
2308 * This next line is a subtlety we need for
2309 * MS-Access. If a file open will fail due to share
2310 * permissions and also for security (access) reasons,
2311 * we need to return the access failed error, not the
2312 * share error. We can't open the file due to kernel
2313 * oplock deadlock (it's possible we failed above on
2314 * the open_mode_check()) so use a userspace check.
2317 if (flags & O_RDWR) {
2318 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2319 } else if (flags & O_WRONLY) {
2320 can_access_mask = FILE_WRITE_DATA;
2321 } else {
2322 can_access_mask = FILE_READ_DATA;
2325 if (((can_access_mask & FILE_WRITE_DATA) &&
2326 !CAN_WRITE(conn)) ||
2327 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2328 smb_fname,
2329 false,
2330 can_access_mask))) {
2331 can_access = False;
2335 * If we're returning a share violation, ensure we
2336 * cope with the braindead 1 second delay.
2339 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2340 lp_defer_sharing_violations()) {
2341 struct timeval timeout;
2342 struct deferred_open_record state;
2343 int timeout_usecs;
2345 /* this is a hack to speed up torture tests
2346 in 'make test' */
2347 timeout_usecs = lp_parm_int(SNUM(conn),
2348 "smbd","sharedelay",
2349 SHARING_VIOLATION_USEC_WAIT);
2351 /* This is a relative time, added to the absolute
2352 request_time value to get the absolute timeout time.
2353 Note that if this is the second or greater time we enter
2354 this codepath for this particular request mid then
2355 request_time is left as the absolute time of the *first*
2356 time this request mid was processed. This is what allows
2357 the request to eventually time out. */
2359 timeout = timeval_set(0, timeout_usecs);
2361 /* Nothing actually uses state.delayed_for_oplocks
2362 but it's handy to differentiate in debug messages
2363 between a 30 second delay due to oplock break, and
2364 a 1 second delay for share mode conflicts. */
2366 state.delayed_for_oplocks = False;
2367 state.async_open = false;
2368 state.id = id;
2370 if ((req != NULL)
2371 && !request_timed_out(request_time,
2372 timeout)) {
2373 defer_open(lck, request_time, timeout,
2374 req, &state);
2378 TALLOC_FREE(lck);
2379 if (can_access) {
2381 * We have detected a sharing violation here
2382 * so return the correct error code
2384 status = NT_STATUS_SHARING_VIOLATION;
2385 } else {
2386 status = NT_STATUS_ACCESS_DENIED;
2388 return status;
2392 * We exit this block with the share entry *locked*.....
2396 SMB_ASSERT(!file_existed || (lck != NULL));
2399 * Ensure we pay attention to default ACLs on directories if required.
2402 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2403 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2404 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2407 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2408 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2409 (unsigned int)flags, (unsigned int)flags2,
2410 (unsigned int)unx_mode, (unsigned int)access_mask,
2411 (unsigned int)open_access_mask));
2413 fsp_open = open_file(fsp, conn, req, parent_dir,
2414 flags|flags2, unx_mode, access_mask,
2415 open_access_mask, &new_file_created);
2417 if (!NT_STATUS_IS_OK(fsp_open)) {
2418 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2419 schedule_async_open(request_time, req);
2421 TALLOC_FREE(lck);
2422 return fsp_open;
2425 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2427 * The file did exist, but some other (local or NFS)
2428 * process either renamed/unlinked and re-created the
2429 * file with different dev/ino after we walked the path,
2430 * but before we did the open. We could retry the
2431 * open but it's a rare enough case it's easier to
2432 * just fail the open to prevent creating any problems
2433 * in the open file db having the wrong dev/ino key.
2435 TALLOC_FREE(lck);
2436 fd_close(fsp);
2437 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2438 "Old (dev=0x%llu, ino =0x%llu). "
2439 "New (dev=0x%llu, ino=0x%llu). Failing open "
2440 " with NT_STATUS_ACCESS_DENIED.\n",
2441 smb_fname_str_dbg(smb_fname),
2442 (unsigned long long)saved_stat.st_ex_dev,
2443 (unsigned long long)saved_stat.st_ex_ino,
2444 (unsigned long long)smb_fname->st.st_ex_dev,
2445 (unsigned long long)smb_fname->st.st_ex_ino));
2446 return NT_STATUS_ACCESS_DENIED;
2449 if (!file_existed) {
2450 struct share_mode_entry *batch_entry = NULL;
2451 struct share_mode_entry *exclusive_entry = NULL;
2452 bool got_level2_oplock = false;
2453 bool got_a_none_oplock = false;
2454 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2455 struct file_id id;
2457 * Deal with the race condition where two smbd's detect the
2458 * file doesn't exist and do the create at the same time. One
2459 * of them will win and set a share mode, the other (ie. this
2460 * one) should check if the requested share mode for this
2461 * create is allowed.
2465 * Now the file exists and fsp is successfully opened,
2466 * fsp->dev and fsp->inode are valid and should replace the
2467 * dev=0,inode=0 from a non existent file. Spotted by
2468 * Nadav Danieli <nadavd@exanet.com>. JRA.
2471 id = fsp->file_id;
2473 lck = get_share_mode_lock(talloc_tos(), id,
2474 conn->connectpath,
2475 smb_fname, &old_write_time);
2477 if (lck == NULL) {
2478 DEBUG(0, ("open_file_ntcreate: Could not get share "
2479 "mode lock for %s\n",
2480 smb_fname_str_dbg(smb_fname)));
2481 fd_close(fsp);
2482 return NT_STATUS_SHARING_VIOLATION;
2485 /* Get the types we need to examine. */
2486 find_oplock_types(fsp,
2487 oplock_request,
2488 lck,
2489 &batch_entry,
2490 &exclusive_entry,
2491 &got_level2_oplock,
2492 &got_a_none_oplock);
2494 /* First pass - send break only on batch oplocks. */
2495 if ((req != NULL) &&
2496 delay_for_batch_oplocks(fsp,
2497 req->mid,
2498 oplock_request,
2499 batch_entry)) {
2500 schedule_defer_open(lck, request_time, req);
2501 TALLOC_FREE(lck);
2502 fd_close(fsp);
2503 return NT_STATUS_SHARING_VIOLATION;
2506 status = open_mode_check(conn, lck, fsp->name_hash,
2507 access_mask, share_access,
2508 create_options, &file_existed);
2510 if (NT_STATUS_IS_OK(status)) {
2511 /* We might be going to allow this open. Check oplock
2512 * status again. */
2513 /* Second pass - send break for both batch or
2514 * exclusive oplocks. */
2515 if ((req != NULL) &&
2516 delay_for_exclusive_oplocks(
2517 fsp,
2518 req->mid,
2519 oplock_request,
2520 exclusive_entry)) {
2521 schedule_defer_open(lck, request_time, req);
2522 TALLOC_FREE(lck);
2523 fd_close(fsp);
2524 return NT_STATUS_SHARING_VIOLATION;
2528 if (!NT_STATUS_IS_OK(status)) {
2529 struct deferred_open_record state;
2531 state.delayed_for_oplocks = False;
2532 state.async_open = false;
2533 state.id = id;
2535 /* Do it all over again immediately. In the second
2536 * round we will find that the file existed and handle
2537 * the DELETE_PENDING and FCB cases correctly. No need
2538 * to duplicate the code here. Essentially this is a
2539 * "goto top of this function", but don't tell
2540 * anybody... */
2542 if (req != NULL) {
2543 defer_open(lck, request_time, timeval_zero(),
2544 req, &state);
2546 TALLOC_FREE(lck);
2547 fd_close(fsp);
2548 return status;
2551 grant_fsp_oplock_type(fsp,
2552 oplock_request,
2553 got_level2_oplock,
2554 got_a_none_oplock);
2557 * We exit this block with the share entry *locked*.....
2562 SMB_ASSERT(lck != NULL);
2564 /* Delete streams if create_disposition requires it */
2565 if (!new_file_created && clear_ads(create_disposition) &&
2566 !is_ntfs_stream_smb_fname(smb_fname)) {
2567 status = delete_all_streams(conn, smb_fname->base_name);
2568 if (!NT_STATUS_IS_OK(status)) {
2569 TALLOC_FREE(lck);
2570 fd_close(fsp);
2571 return status;
2575 /* note that we ignore failure for the following. It is
2576 basically a hack for NFS, and NFS will never set one of
2577 these only read them. Nobody but Samba can ever set a deny
2578 mode and we have already checked our more authoritative
2579 locking database for permission to set this deny mode. If
2580 the kernel refuses the operations then the kernel is wrong.
2581 note that GPFS supports it as well - jmcd */
2583 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
2584 int ret_flock;
2585 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2586 if(ret_flock == -1 ){
2588 TALLOC_FREE(lck);
2589 fd_close(fsp);
2591 return NT_STATUS_SHARING_VIOLATION;
2596 * At this point onwards, we can guarentee that the share entry
2597 * is locked, whether we created the file or not, and that the
2598 * deny mode is compatible with all current opens.
2602 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2603 * but we don't have to store this - just ignore it on access check.
2605 if (conn->sconn->using_smb2) {
2607 * SMB2 doesn't return it (according to Microsoft tests).
2608 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2609 * File created with access = 0x7 (Read, Write, Delete)
2610 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2612 fsp->access_mask = access_mask;
2613 } else {
2614 /* But SMB1 does. */
2615 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2618 if (file_existed) {
2619 /* stat opens on existing files don't get oplocks. */
2620 if (is_stat_open(open_access_mask)) {
2621 fsp->oplock_type = NO_OPLOCK;
2625 if (new_file_created) {
2626 info = FILE_WAS_CREATED;
2627 } else {
2628 if (flags2 & O_TRUNC) {
2629 info = FILE_WAS_OVERWRITTEN;
2630 } else {
2631 info = FILE_WAS_OPENED;
2635 if (pinfo) {
2636 *pinfo = info;
2640 * Setup the oplock info in both the shared memory and
2641 * file structs.
2644 status = set_file_oplock(fsp, fsp->oplock_type);
2645 if (!NT_STATUS_IS_OK(status)) {
2647 * Could not get the kernel oplock or there are byte-range
2648 * locks on the file.
2650 fsp->oplock_type = NO_OPLOCK;
2653 set_share_mode(lck, fsp, get_current_uid(conn),
2654 req ? req->mid : 0,
2655 fsp->oplock_type);
2657 /* Handle strange delete on close create semantics. */
2658 if (create_options & FILE_DELETE_ON_CLOSE) {
2660 status = can_set_delete_on_close(fsp, new_dos_attributes);
2662 if (!NT_STATUS_IS_OK(status)) {
2663 /* Remember to delete the mode we just added. */
2664 del_share_mode(lck, fsp);
2665 TALLOC_FREE(lck);
2666 fd_close(fsp);
2667 return status;
2669 /* Note that here we set the *inital* delete on close flag,
2670 not the regular one. The magic gets handled in close. */
2671 fsp->initial_delete_on_close = True;
2674 if (info != FILE_WAS_OPENED) {
2675 /* Files should be initially set as archive */
2676 if (lp_map_archive(SNUM(conn)) ||
2677 lp_store_dos_attributes(SNUM(conn))) {
2678 if (!posix_open) {
2679 if (file_set_dosmode(conn, smb_fname,
2680 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2681 parent_dir, true) == 0) {
2682 unx_mode = smb_fname->st.st_ex_mode;
2688 /* Determine sparse flag. */
2689 if (posix_open) {
2690 /* POSIX opens are sparse by default. */
2691 fsp->is_sparse = true;
2692 } else {
2693 fsp->is_sparse = (file_existed &&
2694 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2698 * Take care of inherited ACLs on created files - if default ACL not
2699 * selected.
2702 if (!posix_open && new_file_created && !def_acl) {
2704 int saved_errno = errno; /* We might get ENOSYS in the next
2705 * call.. */
2707 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2708 errno == ENOSYS) {
2709 errno = saved_errno; /* Ignore ENOSYS */
2712 } else if (new_unx_mode) {
2714 int ret = -1;
2716 /* Attributes need changing. File already existed. */
2719 int saved_errno = errno; /* We might get ENOSYS in the
2720 * next call.. */
2721 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2723 if (ret == -1 && errno == ENOSYS) {
2724 errno = saved_errno; /* Ignore ENOSYS */
2725 } else {
2726 DEBUG(5, ("open_file_ntcreate: reset "
2727 "attributes of file %s to 0%o\n",
2728 smb_fname_str_dbg(smb_fname),
2729 (unsigned int)new_unx_mode));
2730 ret = 0; /* Don't do the fchmod below. */
2734 if ((ret == -1) &&
2735 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2736 DEBUG(5, ("open_file_ntcreate: failed to reset "
2737 "attributes of file %s to 0%o\n",
2738 smb_fname_str_dbg(smb_fname),
2739 (unsigned int)new_unx_mode));
2742 /* If this is a successful open, we must remove any deferred open
2743 * records. */
2744 if (req != NULL) {
2745 del_deferred_open_entry(lck, req->mid,
2746 messaging_server_id(req->sconn->msg_ctx));
2748 TALLOC_FREE(lck);
2750 return NT_STATUS_OK;
2754 /****************************************************************************
2755 Open a file for for write to ensure that we can fchmod it.
2756 ****************************************************************************/
2758 NTSTATUS open_file_fchmod(connection_struct *conn,
2759 struct smb_filename *smb_fname,
2760 files_struct **result)
2762 if (!VALID_STAT(smb_fname->st)) {
2763 return NT_STATUS_INVALID_PARAMETER;
2766 return SMB_VFS_CREATE_FILE(
2767 conn, /* conn */
2768 NULL, /* req */
2769 0, /* root_dir_fid */
2770 smb_fname, /* fname */
2771 FILE_WRITE_DATA, /* access_mask */
2772 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2773 FILE_SHARE_DELETE),
2774 FILE_OPEN, /* create_disposition*/
2775 0, /* create_options */
2776 0, /* file_attributes */
2777 INTERNAL_OPEN_ONLY, /* oplock_request */
2778 0, /* allocation_size */
2779 0, /* private_flags */
2780 NULL, /* sd */
2781 NULL, /* ea_list */
2782 result, /* result */
2783 NULL); /* pinfo */
2786 static NTSTATUS mkdir_internal(connection_struct *conn,
2787 struct smb_filename *smb_dname,
2788 uint32 file_attributes)
2790 mode_t mode;
2791 char *parent_dir = NULL;
2792 NTSTATUS status;
2793 bool posix_open = false;
2794 bool need_re_stat = false;
2795 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2797 if(access_mask & ~(conn->share_access)) {
2798 DEBUG(5,("mkdir_internal: failing share access "
2799 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
2800 return NT_STATUS_ACCESS_DENIED;
2803 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2804 NULL)) {
2805 return NT_STATUS_NO_MEMORY;
2808 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2809 posix_open = true;
2810 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2811 } else {
2812 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2815 status = check_parent_access(conn,
2816 smb_dname,
2817 access_mask);
2818 if(!NT_STATUS_IS_OK(status)) {
2819 DEBUG(5,("mkdir_internal: check_parent_access "
2820 "on directory %s for path %s returned %s\n",
2821 parent_dir,
2822 smb_dname->base_name,
2823 nt_errstr(status) ));
2824 return status;
2827 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2828 return map_nt_error_from_unix(errno);
2831 /* Ensure we're checking for a symlink here.... */
2832 /* We don't want to get caught by a symlink racer. */
2834 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2835 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2836 smb_fname_str_dbg(smb_dname), strerror(errno)));
2837 return map_nt_error_from_unix(errno);
2840 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2841 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2842 smb_fname_str_dbg(smb_dname)));
2843 return NT_STATUS_NOT_A_DIRECTORY;
2846 if (lp_store_dos_attributes(SNUM(conn))) {
2847 if (!posix_open) {
2848 file_set_dosmode(conn, smb_dname,
2849 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2850 parent_dir, true);
2854 if (lp_inherit_perms(SNUM(conn))) {
2855 inherit_access_posix_acl(conn, parent_dir,
2856 smb_dname->base_name, mode);
2857 need_re_stat = true;
2860 if (!posix_open) {
2862 * Check if high bits should have been set,
2863 * then (if bits are missing): add them.
2864 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2865 * dir.
2867 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2868 (mode & ~smb_dname->st.st_ex_mode)) {
2869 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2870 (smb_dname->st.st_ex_mode |
2871 (mode & ~smb_dname->st.st_ex_mode)));
2872 need_re_stat = true;
2876 /* Change the owner if required. */
2877 if (lp_inherit_owner(SNUM(conn))) {
2878 change_dir_owner_to_parent(conn, parent_dir,
2879 smb_dname->base_name,
2880 &smb_dname->st);
2881 need_re_stat = true;
2884 if (need_re_stat) {
2885 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2886 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2887 smb_fname_str_dbg(smb_dname), strerror(errno)));
2888 return map_nt_error_from_unix(errno);
2892 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2893 smb_dname->base_name);
2895 return NT_STATUS_OK;
2898 /****************************************************************************
2899 Open a directory from an NT SMB call.
2900 ****************************************************************************/
2902 static NTSTATUS open_directory(connection_struct *conn,
2903 struct smb_request *req,
2904 struct smb_filename *smb_dname,
2905 uint32 access_mask,
2906 uint32 share_access,
2907 uint32 create_disposition,
2908 uint32 create_options,
2909 uint32 file_attributes,
2910 int *pinfo,
2911 files_struct **result)
2913 files_struct *fsp = NULL;
2914 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2915 struct share_mode_lock *lck = NULL;
2916 NTSTATUS status;
2917 struct timespec mtimespec;
2918 int info = 0;
2920 if (is_ntfs_stream_smb_fname(smb_dname)) {
2921 DEBUG(2, ("open_directory: %s is a stream name!\n",
2922 smb_fname_str_dbg(smb_dname)));
2923 return NT_STATUS_NOT_A_DIRECTORY;
2926 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2927 /* Ensure we have a directory attribute. */
2928 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2931 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2932 "share_access = 0x%x create_options = 0x%x, "
2933 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2934 smb_fname_str_dbg(smb_dname),
2935 (unsigned int)access_mask,
2936 (unsigned int)share_access,
2937 (unsigned int)create_options,
2938 (unsigned int)create_disposition,
2939 (unsigned int)file_attributes));
2941 status = smbd_calculate_access_mask(conn, smb_dname, false,
2942 access_mask, &access_mask);
2943 if (!NT_STATUS_IS_OK(status)) {
2944 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2945 "on file %s returned %s\n",
2946 smb_fname_str_dbg(smb_dname),
2947 nt_errstr(status)));
2948 return status;
2951 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2952 !security_token_has_privilege(get_current_nttok(conn),
2953 SEC_PRIV_SECURITY)) {
2954 DEBUG(10, ("open_directory: open on %s "
2955 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2956 smb_fname_str_dbg(smb_dname)));
2957 return NT_STATUS_PRIVILEGE_NOT_HELD;
2960 switch( create_disposition ) {
2961 case FILE_OPEN:
2963 if (!dir_existed) {
2964 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2967 info = FILE_WAS_OPENED;
2968 break;
2970 case FILE_CREATE:
2972 /* If directory exists error. If directory doesn't
2973 * exist create. */
2975 if (dir_existed) {
2976 status = NT_STATUS_OBJECT_NAME_COLLISION;
2977 DEBUG(2, ("open_directory: unable to create "
2978 "%s. Error was %s\n",
2979 smb_fname_str_dbg(smb_dname),
2980 nt_errstr(status)));
2981 return status;
2984 status = mkdir_internal(conn, smb_dname,
2985 file_attributes);
2987 if (!NT_STATUS_IS_OK(status)) {
2988 DEBUG(2, ("open_directory: unable to create "
2989 "%s. Error was %s\n",
2990 smb_fname_str_dbg(smb_dname),
2991 nt_errstr(status)));
2992 return status;
2995 info = FILE_WAS_CREATED;
2996 break;
2998 case FILE_OPEN_IF:
3000 * If directory exists open. If directory doesn't
3001 * exist create.
3004 if (dir_existed) {
3005 status = NT_STATUS_OK;
3006 info = FILE_WAS_OPENED;
3007 } else {
3008 status = mkdir_internal(conn, smb_dname,
3009 file_attributes);
3011 if (NT_STATUS_IS_OK(status)) {
3012 info = FILE_WAS_CREATED;
3013 } else {
3014 /* Cope with create race. */
3015 if (!NT_STATUS_EQUAL(status,
3016 NT_STATUS_OBJECT_NAME_COLLISION)) {
3017 DEBUG(2, ("open_directory: unable to create "
3018 "%s. Error was %s\n",
3019 smb_fname_str_dbg(smb_dname),
3020 nt_errstr(status)));
3021 return status;
3023 info = FILE_WAS_OPENED;
3027 break;
3029 case FILE_SUPERSEDE:
3030 case FILE_OVERWRITE:
3031 case FILE_OVERWRITE_IF:
3032 default:
3033 DEBUG(5,("open_directory: invalid create_disposition "
3034 "0x%x for directory %s\n",
3035 (unsigned int)create_disposition,
3036 smb_fname_str_dbg(smb_dname)));
3037 return NT_STATUS_INVALID_PARAMETER;
3040 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3041 DEBUG(5,("open_directory: %s is not a directory !\n",
3042 smb_fname_str_dbg(smb_dname)));
3043 return NT_STATUS_NOT_A_DIRECTORY;
3046 if (info == FILE_WAS_OPENED) {
3047 status = smbd_check_access_rights(conn,
3048 smb_dname,
3049 false,
3050 access_mask);
3051 if (!NT_STATUS_IS_OK(status)) {
3052 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3053 "file %s failed with %s\n",
3054 smb_fname_str_dbg(smb_dname),
3055 nt_errstr(status)));
3056 return status;
3060 status = file_new(req, conn, &fsp);
3061 if(!NT_STATUS_IS_OK(status)) {
3062 return status;
3066 * Setup the files_struct for it.
3069 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3070 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3071 fsp->file_pid = req ? req->smbpid : 0;
3072 fsp->can_lock = False;
3073 fsp->can_read = False;
3074 fsp->can_write = False;
3076 fsp->share_access = share_access;
3077 fsp->fh->private_options = 0;
3079 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3081 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3082 fsp->print_file = NULL;
3083 fsp->modified = False;
3084 fsp->oplock_type = NO_OPLOCK;
3085 fsp->sent_oplock_break = NO_BREAK_SENT;
3086 fsp->is_directory = True;
3087 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
3088 status = fsp_set_smb_fname(fsp, smb_dname);
3089 if (!NT_STATUS_IS_OK(status)) {
3090 file_free(req, fsp);
3091 return status;
3094 mtimespec = smb_dname->st.st_ex_mtime;
3096 #ifdef O_DIRECTORY
3097 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3098 #else
3099 /* POSIX allows us to open a directory with O_RDONLY. */
3100 status = fd_open(conn, fsp, O_RDONLY, 0);
3101 #endif
3102 if (!NT_STATUS_IS_OK(status)) {
3103 DEBUG(5, ("open_directory: Could not open fd for "
3104 "%s (%s)\n",
3105 smb_fname_str_dbg(smb_dname),
3106 nt_errstr(status)));
3107 file_free(req, fsp);
3108 return status;
3111 status = vfs_stat_fsp(fsp);
3112 if (!NT_STATUS_IS_OK(status)) {
3113 fd_close(fsp);
3114 file_free(req, fsp);
3115 return status;
3118 /* Ensure there was no race condition. */
3119 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
3120 DEBUG(5,("open_directory: stat struct differs for "
3121 "directory %s.\n",
3122 smb_fname_str_dbg(smb_dname)));
3123 fd_close(fsp);
3124 file_free(req, fsp);
3125 return NT_STATUS_ACCESS_DENIED;
3128 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3129 conn->connectpath, smb_dname,
3130 &mtimespec);
3132 if (lck == NULL) {
3133 DEBUG(0, ("open_directory: Could not get share mode lock for "
3134 "%s\n", smb_fname_str_dbg(smb_dname)));
3135 fd_close(fsp);
3136 file_free(req, fsp);
3137 return NT_STATUS_SHARING_VIOLATION;
3140 status = open_mode_check(conn, lck, fsp->name_hash,
3141 access_mask, share_access,
3142 create_options, &dir_existed);
3144 if (!NT_STATUS_IS_OK(status)) {
3145 TALLOC_FREE(lck);
3146 fd_close(fsp);
3147 file_free(req, fsp);
3148 return status;
3151 set_share_mode(lck, fsp, get_current_uid(conn),
3152 req ? req->mid : 0, NO_OPLOCK);
3154 /* For directories the delete on close bit at open time seems
3155 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3156 if (create_options & FILE_DELETE_ON_CLOSE) {
3157 status = can_set_delete_on_close(fsp, 0);
3158 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3159 TALLOC_FREE(lck);
3160 fd_close(fsp);
3161 file_free(req, fsp);
3162 return status;
3165 if (NT_STATUS_IS_OK(status)) {
3166 /* Note that here we set the *inital* delete on close flag,
3167 not the regular one. The magic gets handled in close. */
3168 fsp->initial_delete_on_close = True;
3172 TALLOC_FREE(lck);
3174 if (pinfo) {
3175 *pinfo = info;
3178 *result = fsp;
3179 return NT_STATUS_OK;
3182 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3183 struct smb_filename *smb_dname)
3185 NTSTATUS status;
3186 files_struct *fsp;
3188 status = SMB_VFS_CREATE_FILE(
3189 conn, /* conn */
3190 req, /* req */
3191 0, /* root_dir_fid */
3192 smb_dname, /* fname */
3193 FILE_READ_ATTRIBUTES, /* access_mask */
3194 FILE_SHARE_NONE, /* share_access */
3195 FILE_CREATE, /* create_disposition*/
3196 FILE_DIRECTORY_FILE, /* create_options */
3197 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3198 0, /* oplock_request */
3199 0, /* allocation_size */
3200 0, /* private_flags */
3201 NULL, /* sd */
3202 NULL, /* ea_list */
3203 &fsp, /* result */
3204 NULL); /* pinfo */
3206 if (NT_STATUS_IS_OK(status)) {
3207 close_file(req, fsp, NORMAL_CLOSE);
3210 return status;
3213 /****************************************************************************
3214 Receive notification that one of our open files has been renamed by another
3215 smbd process.
3216 ****************************************************************************/
3218 void msg_file_was_renamed(struct messaging_context *msg,
3219 void *private_data,
3220 uint32_t msg_type,
3221 struct server_id server_id,
3222 DATA_BLOB *data)
3224 files_struct *fsp;
3225 char *frm = (char *)data->data;
3226 struct file_id id;
3227 const char *sharepath;
3228 const char *base_name;
3229 const char *stream_name;
3230 struct smb_filename *smb_fname = NULL;
3231 size_t sp_len, bn_len;
3232 NTSTATUS status;
3233 struct smbd_server_connection *sconn =
3234 talloc_get_type_abort(private_data,
3235 struct smbd_server_connection);
3237 if (data->data == NULL
3238 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3239 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3240 (int)data->length));
3241 return;
3244 /* Unpack the message. */
3245 pull_file_id_24(frm, &id);
3246 sharepath = &frm[24];
3247 sp_len = strlen(sharepath);
3248 base_name = sharepath + sp_len + 1;
3249 bn_len = strlen(base_name);
3250 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3252 /* stream_name must always be NULL if there is no stream. */
3253 if (stream_name[0] == '\0') {
3254 stream_name = NULL;
3257 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3258 stream_name, NULL, &smb_fname);
3259 if (!NT_STATUS_IS_OK(status)) {
3260 return;
3263 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3264 "file_id %s\n",
3265 sharepath, smb_fname_str_dbg(smb_fname),
3266 file_id_string_tos(&id)));
3268 for(fsp = file_find_di_first(sconn, id); fsp;
3269 fsp = file_find_di_next(fsp)) {
3270 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3272 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3273 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3274 smb_fname_str_dbg(smb_fname)));
3275 status = fsp_set_smb_fname(fsp, smb_fname);
3276 if (!NT_STATUS_IS_OK(status)) {
3277 goto out;
3279 } else {
3280 /* TODO. JRA. */
3281 /* Now we have the complete path we can work out if this is
3282 actually within this share and adjust newname accordingly. */
3283 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3284 "not sharepath %s) "
3285 "%s from %s -> %s\n",
3286 fsp->conn->connectpath,
3287 sharepath,
3288 fsp_fnum_dbg(fsp),
3289 fsp_str_dbg(fsp),
3290 smb_fname_str_dbg(smb_fname)));
3293 out:
3294 TALLOC_FREE(smb_fname);
3295 return;
3299 * If a main file is opened for delete, all streams need to be checked for
3300 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3301 * If that works, delete them all by setting the delete on close and close.
3304 NTSTATUS open_streams_for_delete(connection_struct *conn,
3305 const char *fname)
3307 struct stream_struct *stream_info = NULL;
3308 files_struct **streams = NULL;
3309 int i;
3310 unsigned int num_streams = 0;
3311 TALLOC_CTX *frame = talloc_stackframe();
3312 NTSTATUS status;
3314 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3315 &num_streams, &stream_info);
3317 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3318 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3319 DEBUG(10, ("no streams around\n"));
3320 TALLOC_FREE(frame);
3321 return NT_STATUS_OK;
3324 if (!NT_STATUS_IS_OK(status)) {
3325 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3326 nt_errstr(status)));
3327 goto fail;
3330 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3331 num_streams));
3333 if (num_streams == 0) {
3334 TALLOC_FREE(frame);
3335 return NT_STATUS_OK;
3338 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3339 if (streams == NULL) {
3340 DEBUG(0, ("talloc failed\n"));
3341 status = NT_STATUS_NO_MEMORY;
3342 goto fail;
3345 for (i=0; i<num_streams; i++) {
3346 struct smb_filename *smb_fname = NULL;
3348 if (strequal(stream_info[i].name, "::$DATA")) {
3349 streams[i] = NULL;
3350 continue;
3353 status = create_synthetic_smb_fname(talloc_tos(), fname,
3354 stream_info[i].name,
3355 NULL, &smb_fname);
3356 if (!NT_STATUS_IS_OK(status)) {
3357 goto fail;
3360 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3361 DEBUG(10, ("Unable to stat stream: %s\n",
3362 smb_fname_str_dbg(smb_fname)));
3365 status = SMB_VFS_CREATE_FILE(
3366 conn, /* conn */
3367 NULL, /* req */
3368 0, /* root_dir_fid */
3369 smb_fname, /* fname */
3370 DELETE_ACCESS, /* access_mask */
3371 (FILE_SHARE_READ | /* share_access */
3372 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3373 FILE_OPEN, /* create_disposition*/
3374 0, /* create_options */
3375 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3376 0, /* oplock_request */
3377 0, /* allocation_size */
3378 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3379 NULL, /* sd */
3380 NULL, /* ea_list */
3381 &streams[i], /* result */
3382 NULL); /* pinfo */
3384 if (!NT_STATUS_IS_OK(status)) {
3385 DEBUG(10, ("Could not open stream %s: %s\n",
3386 smb_fname_str_dbg(smb_fname),
3387 nt_errstr(status)));
3389 TALLOC_FREE(smb_fname);
3390 break;
3392 TALLOC_FREE(smb_fname);
3396 * don't touch the variable "status" beyond this point :-)
3399 for (i -= 1 ; i >= 0; i--) {
3400 if (streams[i] == NULL) {
3401 continue;
3404 DEBUG(10, ("Closing stream # %d, %s\n", i,
3405 fsp_str_dbg(streams[i])));
3406 close_file(NULL, streams[i], NORMAL_CLOSE);
3409 fail:
3410 TALLOC_FREE(frame);
3411 return status;
3414 /*********************************************************************
3415 Create a default ACL by inheriting from the parent. If no inheritance
3416 from the parent available, don't set anything. This will leave the actual
3417 permissions the new file or directory already got from the filesystem
3418 as the NT ACL when read.
3419 *********************************************************************/
3421 static NTSTATUS inherit_new_acl(files_struct *fsp)
3423 TALLOC_CTX *ctx = talloc_tos();
3424 char *parent_name = NULL;
3425 struct security_descriptor *parent_desc = NULL;
3426 NTSTATUS status = NT_STATUS_OK;
3427 struct security_descriptor *psd = NULL;
3428 struct dom_sid *owner_sid = NULL;
3429 struct dom_sid *group_sid = NULL;
3430 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3431 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3432 bool inheritable_components = false;
3433 size_t size = 0;
3435 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3436 return NT_STATUS_NO_MEMORY;
3439 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3440 parent_name,
3441 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3442 &parent_desc);
3443 if (!NT_STATUS_IS_OK(status)) {
3444 return status;
3447 inheritable_components = sd_has_inheritable_components(parent_desc,
3448 fsp->is_directory);
3450 if (!inheritable_components && !inherit_owner) {
3451 /* Nothing to inherit and not setting owner. */
3452 return NT_STATUS_OK;
3455 /* Create an inherited descriptor from the parent. */
3457 if (DEBUGLEVEL >= 10) {
3458 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3459 fsp_str_dbg(fsp) ));
3460 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3463 /* Inherit from parent descriptor if "inherit owner" set. */
3464 if (inherit_owner) {
3465 owner_sid = parent_desc->owner_sid;
3466 group_sid = parent_desc->group_sid;
3469 if (owner_sid == NULL) {
3470 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3472 if (group_sid == NULL) {
3473 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3476 status = se_create_child_secdesc(ctx,
3477 &psd,
3478 &size,
3479 parent_desc,
3480 owner_sid,
3481 group_sid,
3482 fsp->is_directory);
3483 if (!NT_STATUS_IS_OK(status)) {
3484 return status;
3487 /* If inheritable_components == false,
3488 se_create_child_secdesc()
3489 creates a security desriptor with a NULL dacl
3490 entry, but with SEC_DESC_DACL_PRESENT. We need
3491 to remove that flag. */
3493 if (!inheritable_components) {
3494 security_info_sent &= ~SECINFO_DACL;
3495 psd->type &= ~SEC_DESC_DACL_PRESENT;
3498 if (DEBUGLEVEL >= 10) {
3499 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3500 fsp_str_dbg(fsp) ));
3501 NDR_PRINT_DEBUG(security_descriptor, psd);
3504 if (inherit_owner) {
3505 /* We need to be root to force this. */
3506 become_root();
3508 status = SMB_VFS_FSET_NT_ACL(fsp,
3509 security_info_sent,
3510 psd);
3511 if (inherit_owner) {
3512 unbecome_root();
3514 return status;
3518 * Wrapper around open_file_ntcreate and open_directory
3521 static NTSTATUS create_file_unixpath(connection_struct *conn,
3522 struct smb_request *req,
3523 struct smb_filename *smb_fname,
3524 uint32_t access_mask,
3525 uint32_t share_access,
3526 uint32_t create_disposition,
3527 uint32_t create_options,
3528 uint32_t file_attributes,
3529 uint32_t oplock_request,
3530 uint64_t allocation_size,
3531 uint32_t private_flags,
3532 struct security_descriptor *sd,
3533 struct ea_list *ea_list,
3535 files_struct **result,
3536 int *pinfo)
3538 int info = FILE_WAS_OPENED;
3539 files_struct *base_fsp = NULL;
3540 files_struct *fsp = NULL;
3541 NTSTATUS status;
3543 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3544 "file_attributes = 0x%x, share_access = 0x%x, "
3545 "create_disposition = 0x%x create_options = 0x%x "
3546 "oplock_request = 0x%x private_flags = 0x%x "
3547 "ea_list = 0x%p, sd = 0x%p, "
3548 "fname = %s\n",
3549 (unsigned int)access_mask,
3550 (unsigned int)file_attributes,
3551 (unsigned int)share_access,
3552 (unsigned int)create_disposition,
3553 (unsigned int)create_options,
3554 (unsigned int)oplock_request,
3555 (unsigned int)private_flags,
3556 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3558 if (create_options & FILE_OPEN_BY_FILE_ID) {
3559 status = NT_STATUS_NOT_SUPPORTED;
3560 goto fail;
3563 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3564 status = NT_STATUS_INVALID_PARAMETER;
3565 goto fail;
3568 if (req == NULL) {
3569 oplock_request |= INTERNAL_OPEN_ONLY;
3572 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3573 && (access_mask & DELETE_ACCESS)
3574 && !is_ntfs_stream_smb_fname(smb_fname)) {
3576 * We can't open a file with DELETE access if any of the
3577 * streams is open without FILE_SHARE_DELETE
3579 status = open_streams_for_delete(conn, smb_fname->base_name);
3581 if (!NT_STATUS_IS_OK(status)) {
3582 goto fail;
3586 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3587 !security_token_has_privilege(get_current_nttok(conn),
3588 SEC_PRIV_SECURITY)) {
3589 DEBUG(10, ("create_file_unixpath: open on %s "
3590 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3591 smb_fname_str_dbg(smb_fname)));
3592 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3593 goto fail;
3596 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3597 && is_ntfs_stream_smb_fname(smb_fname)
3598 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3599 uint32 base_create_disposition;
3600 struct smb_filename *smb_fname_base = NULL;
3602 if (create_options & FILE_DIRECTORY_FILE) {
3603 status = NT_STATUS_NOT_A_DIRECTORY;
3604 goto fail;
3607 switch (create_disposition) {
3608 case FILE_OPEN:
3609 base_create_disposition = FILE_OPEN;
3610 break;
3611 default:
3612 base_create_disposition = FILE_OPEN_IF;
3613 break;
3616 /* Create an smb_filename with stream_name == NULL. */
3617 status = create_synthetic_smb_fname(talloc_tos(),
3618 smb_fname->base_name,
3619 NULL, NULL,
3620 &smb_fname_base);
3621 if (!NT_STATUS_IS_OK(status)) {
3622 goto fail;
3625 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3626 DEBUG(10, ("Unable to stat stream: %s\n",
3627 smb_fname_str_dbg(smb_fname_base)));
3630 /* Open the base file. */
3631 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3632 FILE_SHARE_READ
3633 | FILE_SHARE_WRITE
3634 | FILE_SHARE_DELETE,
3635 base_create_disposition,
3636 0, 0, 0, 0, 0, NULL, NULL,
3637 &base_fsp, NULL);
3638 TALLOC_FREE(smb_fname_base);
3640 if (!NT_STATUS_IS_OK(status)) {
3641 DEBUG(10, ("create_file_unixpath for base %s failed: "
3642 "%s\n", smb_fname->base_name,
3643 nt_errstr(status)));
3644 goto fail;
3646 /* we don't need to low level fd */
3647 fd_close(base_fsp);
3651 * If it's a request for a directory open, deal with it separately.
3654 if (create_options & FILE_DIRECTORY_FILE) {
3656 if (create_options & FILE_NON_DIRECTORY_FILE) {
3657 status = NT_STATUS_INVALID_PARAMETER;
3658 goto fail;
3661 /* Can't open a temp directory. IFS kit test. */
3662 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3663 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3664 status = NT_STATUS_INVALID_PARAMETER;
3665 goto fail;
3669 * We will get a create directory here if the Win32
3670 * app specified a security descriptor in the
3671 * CreateDirectory() call.
3674 oplock_request = 0;
3675 status = open_directory(
3676 conn, req, smb_fname, access_mask, share_access,
3677 create_disposition, create_options, file_attributes,
3678 &info, &fsp);
3679 } else {
3682 * Ordinary file case.
3685 status = file_new(req, conn, &fsp);
3686 if(!NT_STATUS_IS_OK(status)) {
3687 goto fail;
3690 status = fsp_set_smb_fname(fsp, smb_fname);
3691 if (!NT_STATUS_IS_OK(status)) {
3692 goto fail;
3695 if (base_fsp) {
3697 * We're opening the stream element of a
3698 * base_fsp we already opened. Set up the
3699 * base_fsp pointer.
3701 fsp->base_fsp = base_fsp;
3704 if (allocation_size) {
3705 fsp->initial_allocation_size = smb_roundup(fsp->conn,
3706 allocation_size);
3709 status = open_file_ntcreate(conn,
3710 req,
3711 access_mask,
3712 share_access,
3713 create_disposition,
3714 create_options,
3715 file_attributes,
3716 oplock_request,
3717 private_flags,
3718 &info,
3719 fsp);
3721 if(!NT_STATUS_IS_OK(status)) {
3722 file_free(req, fsp);
3723 fsp = NULL;
3726 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3728 /* A stream open never opens a directory */
3730 if (base_fsp) {
3731 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3732 goto fail;
3736 * Fail the open if it was explicitly a non-directory
3737 * file.
3740 if (create_options & FILE_NON_DIRECTORY_FILE) {
3741 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3742 goto fail;
3745 oplock_request = 0;
3746 status = open_directory(
3747 conn, req, smb_fname, access_mask,
3748 share_access, create_disposition,
3749 create_options, file_attributes,
3750 &info, &fsp);
3754 if (!NT_STATUS_IS_OK(status)) {
3755 goto fail;
3758 fsp->base_fsp = base_fsp;
3760 if ((ea_list != NULL) &&
3761 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3762 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3763 if (!NT_STATUS_IS_OK(status)) {
3764 goto fail;
3768 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3769 status = NT_STATUS_ACCESS_DENIED;
3770 goto fail;
3773 /* Save the requested allocation size. */
3774 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3775 if (allocation_size
3776 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3777 fsp->initial_allocation_size = smb_roundup(
3778 fsp->conn, allocation_size);
3779 if (fsp->is_directory) {
3780 /* Can't set allocation size on a directory. */
3781 status = NT_STATUS_ACCESS_DENIED;
3782 goto fail;
3784 if (vfs_allocate_file_space(
3785 fsp, fsp->initial_allocation_size) == -1) {
3786 status = NT_STATUS_DISK_FULL;
3787 goto fail;
3789 } else {
3790 fsp->initial_allocation_size = smb_roundup(
3791 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3793 } else {
3794 fsp->initial_allocation_size = 0;
3797 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3798 fsp->base_fsp == NULL) {
3799 if (sd != NULL) {
3801 * According to the MS documentation, the only time the security
3802 * descriptor is applied to the opened file is iff we *created* the
3803 * file; an existing file stays the same.
3805 * Also, it seems (from observation) that you can open the file with
3806 * any access mask but you can still write the sd. We need to override
3807 * the granted access before we call set_sd
3808 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3811 uint32_t sec_info_sent;
3812 uint32_t saved_access_mask = fsp->access_mask;
3814 sec_info_sent = get_sec_info(sd);
3816 fsp->access_mask = FILE_GENERIC_ALL;
3818 if (sec_info_sent & (SECINFO_OWNER|
3819 SECINFO_GROUP|
3820 SECINFO_DACL|
3821 SECINFO_SACL)) {
3822 status = set_sd(fsp, sd, sec_info_sent);
3825 fsp->access_mask = saved_access_mask;
3827 if (!NT_STATUS_IS_OK(status)) {
3828 goto fail;
3830 } else if (lp_inherit_acls(SNUM(conn))) {
3831 /* Inherit from parent. Errors here are not fatal. */
3832 status = inherit_new_acl(fsp);
3833 if (!NT_STATUS_IS_OK(status)) {
3834 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3835 fsp_str_dbg(fsp),
3836 nt_errstr(status) ));
3841 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3843 *result = fsp;
3844 if (pinfo != NULL) {
3845 *pinfo = info;
3848 smb_fname->st = fsp->fsp_name->st;
3850 return NT_STATUS_OK;
3852 fail:
3853 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3855 if (fsp != NULL) {
3856 if (base_fsp && fsp->base_fsp == base_fsp) {
3858 * The close_file below will close
3859 * fsp->base_fsp.
3861 base_fsp = NULL;
3863 close_file(req, fsp, ERROR_CLOSE);
3864 fsp = NULL;
3866 if (base_fsp != NULL) {
3867 close_file(req, base_fsp, ERROR_CLOSE);
3868 base_fsp = NULL;
3870 return status;
3874 * Calculate the full path name given a relative fid.
3876 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3877 struct smb_request *req,
3878 uint16_t root_dir_fid,
3879 const struct smb_filename *smb_fname,
3880 struct smb_filename **smb_fname_out)
3882 files_struct *dir_fsp;
3883 char *parent_fname = NULL;
3884 char *new_base_name = NULL;
3885 NTSTATUS status;
3887 if (root_dir_fid == 0 || !smb_fname) {
3888 status = NT_STATUS_INTERNAL_ERROR;
3889 goto out;
3892 dir_fsp = file_fsp(req, root_dir_fid);
3894 if (dir_fsp == NULL) {
3895 status = NT_STATUS_INVALID_HANDLE;
3896 goto out;
3899 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3900 status = NT_STATUS_INVALID_HANDLE;
3901 goto out;
3904 if (!dir_fsp->is_directory) {
3907 * Check to see if this is a mac fork of some kind.
3910 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3911 is_ntfs_stream_smb_fname(smb_fname)) {
3912 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3913 goto out;
3917 we need to handle the case when we get a
3918 relative open relative to a file and the
3919 pathname is blank - this is a reopen!
3920 (hint from demyn plantenberg)
3923 status = NT_STATUS_INVALID_HANDLE;
3924 goto out;
3927 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3929 * We're at the toplevel dir, the final file name
3930 * must not contain ./, as this is filtered out
3931 * normally by srvstr_get_path and unix_convert
3932 * explicitly rejects paths containing ./.
3934 parent_fname = talloc_strdup(talloc_tos(), "");
3935 if (parent_fname == NULL) {
3936 status = NT_STATUS_NO_MEMORY;
3937 goto out;
3939 } else {
3940 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3943 * Copy in the base directory name.
3946 parent_fname = talloc_array(talloc_tos(), char,
3947 dir_name_len+2);
3948 if (parent_fname == NULL) {
3949 status = NT_STATUS_NO_MEMORY;
3950 goto out;
3952 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3953 dir_name_len+1);
3956 * Ensure it ends in a '/'.
3957 * We used TALLOC_SIZE +2 to add space for the '/'.
3960 if(dir_name_len
3961 && (parent_fname[dir_name_len-1] != '\\')
3962 && (parent_fname[dir_name_len-1] != '/')) {
3963 parent_fname[dir_name_len] = '/';
3964 parent_fname[dir_name_len+1] = '\0';
3968 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3969 smb_fname->base_name);
3970 if (new_base_name == NULL) {
3971 status = NT_STATUS_NO_MEMORY;
3972 goto out;
3975 status = filename_convert(req,
3976 conn,
3977 req->flags2 & FLAGS2_DFS_PATHNAMES,
3978 new_base_name,
3980 NULL,
3981 smb_fname_out);
3982 if (!NT_STATUS_IS_OK(status)) {
3983 goto out;
3986 out:
3987 TALLOC_FREE(parent_fname);
3988 TALLOC_FREE(new_base_name);
3989 return status;
3992 NTSTATUS create_file_default(connection_struct *conn,
3993 struct smb_request *req,
3994 uint16_t root_dir_fid,
3995 struct smb_filename *smb_fname,
3996 uint32_t access_mask,
3997 uint32_t share_access,
3998 uint32_t create_disposition,
3999 uint32_t create_options,
4000 uint32_t file_attributes,
4001 uint32_t oplock_request,
4002 uint64_t allocation_size,
4003 uint32_t private_flags,
4004 struct security_descriptor *sd,
4005 struct ea_list *ea_list,
4006 files_struct **result,
4007 int *pinfo)
4009 int info = FILE_WAS_OPENED;
4010 files_struct *fsp = NULL;
4011 NTSTATUS status;
4012 bool stream_name = false;
4014 DEBUG(10,("create_file: access_mask = 0x%x "
4015 "file_attributes = 0x%x, share_access = 0x%x, "
4016 "create_disposition = 0x%x create_options = 0x%x "
4017 "oplock_request = 0x%x "
4018 "private_flags = 0x%x "
4019 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4020 "fname = %s\n",
4021 (unsigned int)access_mask,
4022 (unsigned int)file_attributes,
4023 (unsigned int)share_access,
4024 (unsigned int)create_disposition,
4025 (unsigned int)create_options,
4026 (unsigned int)oplock_request,
4027 (unsigned int)private_flags,
4028 (unsigned int)root_dir_fid,
4029 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4032 * Calculate the filename from the root_dir_if if necessary.
4035 if (root_dir_fid != 0) {
4036 struct smb_filename *smb_fname_out = NULL;
4037 status = get_relative_fid_filename(conn, req, root_dir_fid,
4038 smb_fname, &smb_fname_out);
4039 if (!NT_STATUS_IS_OK(status)) {
4040 goto fail;
4042 smb_fname = smb_fname_out;
4046 * Check to see if this is a mac fork of some kind.
4049 stream_name = is_ntfs_stream_smb_fname(smb_fname);
4050 if (stream_name) {
4051 enum FAKE_FILE_TYPE fake_file_type;
4053 fake_file_type = is_fake_file(smb_fname);
4055 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
4058 * Here we go! support for changing the disk quotas
4059 * --metze
4061 * We need to fake up to open this MAGIC QUOTA file
4062 * and return a valid FID.
4064 * w2k close this file directly after openening xp
4065 * also tries a QUERY_FILE_INFO on the file and then
4066 * close it
4068 status = open_fake_file(req, conn, req->vuid,
4069 fake_file_type, smb_fname,
4070 access_mask, &fsp);
4071 if (!NT_STATUS_IS_OK(status)) {
4072 goto fail;
4075 ZERO_STRUCT(smb_fname->st);
4076 goto done;
4079 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
4080 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4081 goto fail;
4085 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
4086 int ret;
4087 smb_fname->stream_name = NULL;
4088 /* We have to handle this error here. */
4089 if (create_options & FILE_DIRECTORY_FILE) {
4090 status = NT_STATUS_NOT_A_DIRECTORY;
4091 goto fail;
4093 if (lp_posix_pathnames()) {
4094 ret = SMB_VFS_LSTAT(conn, smb_fname);
4095 } else {
4096 ret = SMB_VFS_STAT(conn, smb_fname);
4099 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
4100 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4101 goto fail;
4105 status = create_file_unixpath(
4106 conn, req, smb_fname, access_mask, share_access,
4107 create_disposition, create_options, file_attributes,
4108 oplock_request, allocation_size, private_flags,
4109 sd, ea_list,
4110 &fsp, &info);
4112 if (!NT_STATUS_IS_OK(status)) {
4113 goto fail;
4116 done:
4117 DEBUG(10, ("create_file: info=%d\n", info));
4119 *result = fsp;
4120 if (pinfo != NULL) {
4121 *pinfo = info;
4123 return NT_STATUS_OK;
4125 fail:
4126 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
4128 if (fsp != NULL) {
4129 close_file(req, fsp, ERROR_CLOSE);
4130 fsp = NULL;
4132 return status;