s3: Factor out calculate_open_access_flags
[Samba/gebeck_regimport.git] / source3 / smbd / open.c
blob2785d6f3d1892e71d39a954f32f43f59b72ae7dd
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 & stat_open_bits) != 0) &&
1075 ((access_mask & ~stat_open_bits) == 0));
1078 /****************************************************************************
1079 Deal with share modes
1080 Invarient: Share mode must be locked on entry and exit.
1081 Returns -1 on error, or number of share modes on success (may be zero).
1082 ****************************************************************************/
1084 static NTSTATUS open_mode_check(connection_struct *conn,
1085 struct share_mode_lock *lck,
1086 uint32_t name_hash,
1087 uint32 access_mask,
1088 uint32 share_access,
1089 uint32 create_options,
1090 bool *file_existed)
1092 int i;
1094 if(lck->data->num_share_modes == 0) {
1095 return NT_STATUS_OK;
1098 /* A delete on close prohibits everything */
1100 if (is_delete_on_close_set(lck, name_hash)) {
1102 * Check the delete on close token
1103 * is valid. It could have been left
1104 * after a server crash.
1106 for(i = 0; i < lck->data->num_share_modes; i++) {
1107 if (!share_mode_stale_pid(lck->data, i)) {
1109 *file_existed = true;
1111 return NT_STATUS_DELETE_PENDING;
1114 return NT_STATUS_OK;
1117 if (is_stat_open(access_mask)) {
1118 /* Stat open that doesn't trigger oplock breaks or share mode
1119 * checks... ! JRA. */
1120 return NT_STATUS_OK;
1124 * Check if the share modes will give us access.
1127 #if defined(DEVELOPER)
1128 for(i = 0; i < lck->data->num_share_modes; i++) {
1129 validate_my_share_entries(conn->sconn, i,
1130 &lck->data->share_modes[i]);
1132 #endif
1134 /* Now we check the share modes, after any oplock breaks. */
1135 for(i = 0; i < lck->data->num_share_modes; i++) {
1137 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1138 continue;
1141 /* someone else has a share lock on it, check to see if we can
1142 * too */
1143 if (share_conflict(&lck->data->share_modes[i],
1144 access_mask, share_access)) {
1146 if (share_mode_stale_pid(lck->data, i)) {
1147 continue;
1150 *file_existed = true;
1152 return NT_STATUS_SHARING_VIOLATION;
1156 if (lck->data->num_share_modes != 0) {
1157 *file_existed = true;
1160 return NT_STATUS_OK;
1164 * Send a break message to the oplock holder and delay the open for
1165 * our client.
1168 static NTSTATUS send_break_message(files_struct *fsp,
1169 struct share_mode_entry *exclusive,
1170 uint64_t mid,
1171 int oplock_request)
1173 NTSTATUS status;
1174 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1176 DEBUG(10, ("Sending break request to PID %s\n",
1177 procid_str_static(&exclusive->pid)));
1178 exclusive->op_mid = mid;
1180 /* Create the message. */
1181 share_mode_entry_to_message(msg, exclusive);
1183 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1184 don't want this set in the share mode struct pointed to by lck. */
1186 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1187 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1188 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1191 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1192 MSG_SMB_BREAK_REQUEST,
1193 (uint8 *)msg,
1194 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1195 if (!NT_STATUS_IS_OK(status)) {
1196 DEBUG(3, ("Could not send oplock break message: %s\n",
1197 nt_errstr(status)));
1200 return status;
1204 * Return share_mode_entry pointers for :
1205 * 1). Batch oplock entry.
1206 * 2). Batch or exclusive oplock entry (may be identical to #1).
1207 * bool have_level2_oplock
1208 * bool have_no_oplock.
1209 * Do internal consistency checks on the share mode for a file.
1212 static void find_oplock_types(files_struct *fsp,
1213 int oplock_request,
1214 const struct share_mode_lock *lck,
1215 struct share_mode_entry **pp_batch,
1216 struct share_mode_entry **pp_ex_or_batch,
1217 bool *got_level2,
1218 bool *got_no_oplock)
1220 int i;
1222 *pp_batch = NULL;
1223 *pp_ex_or_batch = NULL;
1224 *got_level2 = false;
1225 *got_no_oplock = false;
1227 /* Ignore stat or internal opens, as is done in
1228 delay_for_batch_oplocks() and
1229 delay_for_exclusive_oplocks().
1231 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1232 return;
1235 for (i=0; i<lck->data->num_share_modes; i++) {
1236 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1237 continue;
1240 if (lck->data->share_modes[i].op_type == NO_OPLOCK &&
1241 is_stat_open(lck->data->share_modes[i].access_mask)) {
1242 /* We ignore stat opens in the table - they
1243 always have NO_OPLOCK and never get or
1244 cause breaks. JRA. */
1245 continue;
1248 if (BATCH_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1249 /* batch - can only be one. */
1250 if (share_mode_stale_pid(lck->data, i)) {
1251 DEBUG(10, ("Found stale batch oplock\n"));
1252 continue;
1254 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1255 smb_panic("Bad batch oplock entry.");
1257 *pp_batch = &lck->data->share_modes[i];
1260 if (EXCLUSIVE_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1261 if (share_mode_stale_pid(lck->data, i)) {
1262 DEBUG(10, ("Found stale duplicate oplock\n"));
1263 continue;
1265 /* Exclusive or batch - can only be one. */
1266 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1267 smb_panic("Bad exclusive or batch oplock entry.");
1269 *pp_ex_or_batch = &lck->data->share_modes[i];
1272 if (LEVEL_II_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1273 if (*pp_batch || *pp_ex_or_batch) {
1274 if (share_mode_stale_pid(lck->data, i)) {
1275 DEBUG(10, ("Found stale LevelII "
1276 "oplock\n"));
1277 continue;
1279 smb_panic("Bad levelII oplock entry.");
1281 *got_level2 = true;
1284 if (lck->data->share_modes[i].op_type == NO_OPLOCK) {
1285 if (*pp_batch || *pp_ex_or_batch) {
1286 if (share_mode_stale_pid(lck->data, i)) {
1287 DEBUG(10, ("Found stale NO_OPLOCK "
1288 "entry\n"));
1289 continue;
1291 smb_panic("Bad no oplock entry.");
1293 *got_no_oplock = true;
1298 static bool delay_for_batch_oplocks(files_struct *fsp,
1299 uint64_t mid,
1300 int oplock_request,
1301 struct share_mode_entry *batch_entry)
1303 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1304 return false;
1306 if (batch_entry == NULL) {
1307 return false;
1310 if (server_id_is_disconnected(&batch_entry->pid)) {
1312 * TODO: clean up.
1313 * This could be achieved by sending a break message
1314 * to ourselves. Special considerations for files
1315 * with delete_on_close flag set!
1317 * For now we keep it simple and do not
1318 * allow delete on close for durable handles.
1320 return false;
1323 /* Found a batch oplock */
1324 send_break_message(fsp, batch_entry, mid, oplock_request);
1325 return true;
1328 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1329 uint64_t mid,
1330 int oplock_request,
1331 struct share_mode_entry *ex_entry)
1333 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1334 return false;
1336 if (ex_entry == NULL) {
1337 return false;
1340 if (server_id_is_disconnected(&ex_entry->pid)) {
1342 * since only durable handles can get disconnected,
1343 * and we can only get durable handles with batch oplocks,
1344 * this should actually never be reached...
1346 return false;
1349 send_break_message(fsp, ex_entry, mid, oplock_request);
1350 return true;
1353 static bool file_has_brlocks(files_struct *fsp)
1355 struct byte_range_lock *br_lck;
1357 br_lck = brl_get_locks_readonly(fsp);
1358 if (!br_lck)
1359 return false;
1361 return br_lck->num_locks > 0 ? true : false;
1364 static void grant_fsp_oplock_type(files_struct *fsp,
1365 int oplock_request,
1366 bool got_level2_oplock,
1367 bool got_a_none_oplock)
1369 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1370 lp_level2_oplocks(SNUM(fsp->conn));
1372 /* Start by granting what the client asked for,
1373 but ensure no SAMBA_PRIVATE bits can be set. */
1374 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1376 if (oplock_request & INTERNAL_OPEN_ONLY) {
1377 /* No oplocks on internal open. */
1378 fsp->oplock_type = NO_OPLOCK;
1379 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1380 fsp->oplock_type, fsp_str_dbg(fsp)));
1381 return;
1384 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1385 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1386 fsp_str_dbg(fsp)));
1387 fsp->oplock_type = NO_OPLOCK;
1390 if (is_stat_open(fsp->access_mask)) {
1391 /* Leave the value already set. */
1392 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1393 fsp->oplock_type, fsp_str_dbg(fsp)));
1394 return;
1398 * Match what was requested (fsp->oplock_type) with
1399 * what was found in the existing share modes.
1402 if (got_a_none_oplock) {
1403 fsp->oplock_type = NO_OPLOCK;
1404 } else if (got_level2_oplock) {
1405 if (fsp->oplock_type == NO_OPLOCK ||
1406 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1407 /* Store a level2 oplock, but don't tell the client */
1408 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1409 } else {
1410 fsp->oplock_type = LEVEL_II_OPLOCK;
1412 } else {
1413 /* All share_mode_entries are placeholders or deferred.
1414 * Silently upgrade to fake levelII if the client didn't
1415 * ask for an oplock. */
1416 if (fsp->oplock_type == NO_OPLOCK) {
1417 /* Store a level2 oplock, but don't tell the client */
1418 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1423 * Don't grant level2 to clients that don't want them
1424 * or if we've turned them off.
1426 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1427 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1430 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1431 fsp->oplock_type, fsp_str_dbg(fsp)));
1434 static bool request_timed_out(struct timeval request_time,
1435 struct timeval timeout)
1437 struct timeval now, end_time;
1438 GetTimeOfDay(&now);
1439 end_time = timeval_sum(&request_time, &timeout);
1440 return (timeval_compare(&end_time, &now) < 0);
1443 /****************************************************************************
1444 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1445 ****************************************************************************/
1447 static void defer_open(struct share_mode_lock *lck,
1448 struct timeval request_time,
1449 struct timeval timeout,
1450 struct smb_request *req,
1451 struct deferred_open_record *state)
1453 struct server_id self = messaging_server_id(req->sconn->msg_ctx);
1455 /* Paranoia check */
1457 if (lck) {
1458 int i;
1460 for (i=0; i<lck->data->num_share_modes; i++) {
1461 struct share_mode_entry *e = &lck->data->share_modes[i];
1463 if (is_deferred_open_entry(e) &&
1464 serverid_equal(&self, &e->pid) &&
1465 (e->op_mid == req->mid)) {
1466 DEBUG(0, ("Trying to defer an already deferred "
1467 "request: mid=%llu, exiting\n",
1468 (unsigned long long)req->mid));
1469 TALLOC_FREE(lck);
1470 exit_server("attempt to defer a deferred request");
1475 /* End paranoia check */
1477 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1478 "open entry for mid %llu\n",
1479 (unsigned int)request_time.tv_sec,
1480 (unsigned int)request_time.tv_usec,
1481 (unsigned long long)req->mid));
1483 if (!push_deferred_open_message_smb(req, request_time, timeout,
1484 state->id, (char *)state, sizeof(*state))) {
1485 TALLOC_FREE(lck);
1486 exit_server("push_deferred_open_message_smb failed");
1488 if (lck) {
1489 add_deferred_open(lck, req->mid, request_time, self, state->id);
1494 /****************************************************************************
1495 On overwrite open ensure that the attributes match.
1496 ****************************************************************************/
1498 static bool open_match_attributes(connection_struct *conn,
1499 uint32 old_dos_attr,
1500 uint32 new_dos_attr,
1501 mode_t existing_unx_mode,
1502 mode_t new_unx_mode,
1503 mode_t *returned_unx_mode)
1505 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1507 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1508 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1510 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1511 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1512 *returned_unx_mode = new_unx_mode;
1513 } else {
1514 *returned_unx_mode = (mode_t)0;
1517 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1518 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1519 "returned_unx_mode = 0%o\n",
1520 (unsigned int)old_dos_attr,
1521 (unsigned int)existing_unx_mode,
1522 (unsigned int)new_dos_attr,
1523 (unsigned int)*returned_unx_mode ));
1525 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1526 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1527 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1528 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1529 return False;
1532 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1533 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1534 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1535 return False;
1538 return True;
1541 /****************************************************************************
1542 Special FCB or DOS processing in the case of a sharing violation.
1543 Try and find a duplicated file handle.
1544 ****************************************************************************/
1546 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1547 connection_struct *conn,
1548 files_struct *fsp_to_dup_into,
1549 const struct smb_filename *smb_fname,
1550 struct file_id id,
1551 uint16 file_pid,
1552 uint64_t vuid,
1553 uint32 access_mask,
1554 uint32 share_access,
1555 uint32 create_options)
1557 files_struct *fsp;
1559 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1560 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1562 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1563 fsp = file_find_di_next(fsp)) {
1565 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1566 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1567 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1568 fsp->fh->fd, (unsigned long long)fsp->vuid,
1569 (unsigned int)fsp->file_pid,
1570 (unsigned int)fsp->fh->private_options,
1571 (unsigned int)fsp->access_mask ));
1573 if (fsp->fh->fd != -1 &&
1574 fsp->vuid == vuid &&
1575 fsp->file_pid == file_pid &&
1576 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1577 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1578 (fsp->access_mask & FILE_WRITE_DATA) &&
1579 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1580 strequal(fsp->fsp_name->stream_name,
1581 smb_fname->stream_name)) {
1582 DEBUG(10,("fcb_or_dos_open: file match\n"));
1583 break;
1587 if (!fsp) {
1588 return NT_STATUS_NOT_FOUND;
1591 /* quite an insane set of semantics ... */
1592 if (is_executable(smb_fname->base_name) &&
1593 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1594 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1595 return NT_STATUS_INVALID_PARAMETER;
1598 /* We need to duplicate this fsp. */
1599 return dup_file_fsp(req, fsp, access_mask, share_access,
1600 create_options, fsp_to_dup_into);
1603 static void schedule_defer_open(struct share_mode_lock *lck,
1604 struct timeval request_time,
1605 struct smb_request *req)
1607 struct deferred_open_record state;
1609 /* This is a relative time, added to the absolute
1610 request_time value to get the absolute timeout time.
1611 Note that if this is the second or greater time we enter
1612 this codepath for this particular request mid then
1613 request_time is left as the absolute time of the *first*
1614 time this request mid was processed. This is what allows
1615 the request to eventually time out. */
1617 struct timeval timeout;
1619 /* Normally the smbd we asked should respond within
1620 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1621 * the client did, give twice the timeout as a safety
1622 * measure here in case the other smbd is stuck
1623 * somewhere else. */
1625 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1627 /* Nothing actually uses state.delayed_for_oplocks
1628 but it's handy to differentiate in debug messages
1629 between a 30 second delay due to oplock break, and
1630 a 1 second delay for share mode conflicts. */
1632 state.delayed_for_oplocks = True;
1633 state.async_open = false;
1634 state.id = lck->data->id;
1636 if (!request_timed_out(request_time, timeout)) {
1637 defer_open(lck, request_time, timeout, req, &state);
1641 /****************************************************************************
1642 Reschedule an open call that went asynchronous.
1643 ****************************************************************************/
1645 static void schedule_async_open(struct timeval request_time,
1646 struct smb_request *req)
1648 struct deferred_open_record state;
1649 struct timeval timeout;
1651 timeout = timeval_set(20, 0);
1653 ZERO_STRUCT(state);
1654 state.delayed_for_oplocks = false;
1655 state.async_open = true;
1657 if (!request_timed_out(request_time, timeout)) {
1658 defer_open(NULL, request_time, timeout, req, &state);
1662 /****************************************************************************
1663 Work out what access_mask to use from what the client sent us.
1664 ****************************************************************************/
1666 static NTSTATUS smbd_calculate_maximum_allowed_access(
1667 connection_struct *conn,
1668 const struct smb_filename *smb_fname,
1669 bool use_privs,
1670 uint32_t *p_access_mask)
1672 struct security_descriptor *sd;
1673 uint32_t access_granted;
1674 NTSTATUS status;
1676 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
1677 *p_access_mask |= FILE_GENERIC_ALL;
1678 return NT_STATUS_OK;
1681 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1682 (SECINFO_OWNER |
1683 SECINFO_GROUP |
1684 SECINFO_DACL),&sd);
1686 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1688 * File did not exist
1690 *p_access_mask = FILE_GENERIC_ALL;
1691 return NT_STATUS_OK;
1693 if (!NT_STATUS_IS_OK(status)) {
1694 DEBUG(10,("Could not get acl on file %s: %s\n",
1695 smb_fname_str_dbg(smb_fname),
1696 nt_errstr(status)));
1697 return NT_STATUS_ACCESS_DENIED;
1701 * Never test FILE_READ_ATTRIBUTES. se_file_access_check()
1702 * also takes care of owner WRITE_DAC and READ_CONTROL.
1704 status = se_file_access_check(sd,
1705 get_current_nttok(conn),
1706 use_privs,
1707 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1708 &access_granted);
1710 TALLOC_FREE(sd);
1712 if (!NT_STATUS_IS_OK(status)) {
1713 DEBUG(10, ("Access denied on file %s: "
1714 "when calculating maximum access\n",
1715 smb_fname_str_dbg(smb_fname)));
1716 return NT_STATUS_ACCESS_DENIED;
1718 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1720 if (!(access_granted & DELETE_ACCESS)) {
1721 if (can_delete_file_in_directory(conn, smb_fname)) {
1722 *p_access_mask |= DELETE_ACCESS;
1726 return NT_STATUS_OK;
1729 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1730 const struct smb_filename *smb_fname,
1731 bool use_privs,
1732 uint32_t access_mask,
1733 uint32_t *access_mask_out)
1735 NTSTATUS status;
1736 uint32_t orig_access_mask = access_mask;
1737 uint32_t rejected_share_access;
1740 * Convert GENERIC bits to specific bits.
1743 se_map_generic(&access_mask, &file_generic_mapping);
1745 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1746 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1748 status = smbd_calculate_maximum_allowed_access(
1749 conn, smb_fname, use_privs, &access_mask);
1751 if (!NT_STATUS_IS_OK(status)) {
1752 return status;
1755 access_mask &= conn->share_access;
1758 rejected_share_access = access_mask & ~(conn->share_access);
1760 if (rejected_share_access) {
1761 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1762 "file %s: rejected by share access mask[0x%08X] "
1763 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1764 smb_fname_str_dbg(smb_fname),
1765 conn->share_access,
1766 orig_access_mask, access_mask,
1767 rejected_share_access));
1768 return NT_STATUS_ACCESS_DENIED;
1771 *access_mask_out = access_mask;
1772 return NT_STATUS_OK;
1775 /****************************************************************************
1776 Remove the deferred open entry under lock.
1777 ****************************************************************************/
1779 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1780 struct server_id pid)
1782 struct share_mode_lock *lck = get_existing_share_mode_lock(
1783 talloc_tos(), id);
1784 if (lck == NULL) {
1785 DEBUG(0, ("could not get share mode lock\n"));
1786 return;
1788 del_deferred_open_entry(lck, mid, pid);
1789 TALLOC_FREE(lck);
1792 /****************************************************************************
1793 Return true if this is a state pointer to an asynchronous create.
1794 ****************************************************************************/
1796 bool is_deferred_open_async(const void *ptr)
1798 const struct deferred_open_record *state = (const struct deferred_open_record *)ptr;
1800 return state->async_open;
1803 static bool clear_ads(uint32_t create_disposition)
1805 bool ret = false;
1807 switch (create_disposition) {
1808 case FILE_SUPERSEDE:
1809 case FILE_OVERWRITE_IF:
1810 case FILE_OVERWRITE:
1811 ret = true;
1812 break;
1813 default:
1814 break;
1816 return ret;
1819 static int disposition_to_open_flags(uint32_t create_disposition)
1821 int ret = 0;
1824 * Currently we're using FILE_SUPERSEDE as the same as
1825 * FILE_OVERWRITE_IF but they really are
1826 * different. FILE_SUPERSEDE deletes an existing file
1827 * (requiring delete access) then recreates it.
1830 switch (create_disposition) {
1831 case FILE_SUPERSEDE:
1832 case FILE_OVERWRITE_IF:
1834 * If file exists replace/overwrite. If file doesn't
1835 * exist create.
1837 ret = O_CREAT|O_TRUNC;
1838 break;
1840 case FILE_OPEN:
1842 * If file exists open. If file doesn't exist error.
1844 ret = 0;
1845 break;
1847 case FILE_OVERWRITE:
1849 * If file exists overwrite. If file doesn't exist
1850 * error.
1852 ret = O_TRUNC;
1853 break;
1855 case FILE_CREATE:
1857 * If file exists error. If file doesn't exist create.
1859 ret = O_CREAT|O_EXCL;
1860 break;
1862 case FILE_OPEN_IF:
1864 * If file exists open. If file doesn't exist create.
1866 ret = O_CREAT;
1867 break;
1869 return ret;
1872 static int calculate_open_access_flags(uint32_t access_mask,
1873 int oplock_request,
1874 uint32_t private_flags)
1876 int flags;
1879 * Note that we ignore the append flag as append does not
1880 * mean the same thing under DOS and Unix.
1883 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1884 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1885 /* DENY_DOS opens are always underlying read-write on the
1886 file handle, no matter what the requested access mask
1887 says. */
1888 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1889 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
1890 FILE_READ_EA|FILE_EXECUTE)) {
1891 flags = O_RDWR;
1892 } else {
1893 flags = O_WRONLY;
1895 } else {
1896 flags = O_RDONLY;
1898 return flags;
1901 /****************************************************************************
1902 Open a file with a share mode. Passed in an already created files_struct *.
1903 ****************************************************************************/
1905 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1906 struct smb_request *req,
1907 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1908 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1909 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1910 uint32 create_options, /* options such as delete on close. */
1911 uint32 new_dos_attributes, /* attributes used for new file. */
1912 int oplock_request, /* internal Samba oplock codes. */
1913 /* Information (FILE_EXISTS etc.) */
1914 uint32_t private_flags, /* Samba specific flags. */
1915 int *pinfo,
1916 files_struct *fsp)
1918 struct smb_filename *smb_fname = fsp->fsp_name;
1919 int flags=0;
1920 int flags2=0;
1921 bool file_existed = VALID_STAT(smb_fname->st);
1922 bool def_acl = False;
1923 bool posix_open = False;
1924 bool new_file_created = False;
1925 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1926 mode_t new_unx_mode = (mode_t)0;
1927 mode_t unx_mode = (mode_t)0;
1928 int info;
1929 uint32 existing_dos_attributes = 0;
1930 struct timeval request_time = timeval_zero();
1931 struct share_mode_lock *lck = NULL;
1932 uint32 open_access_mask = access_mask;
1933 NTSTATUS status;
1934 char *parent_dir;
1935 SMB_STRUCT_STAT saved_stat = smb_fname->st;
1937 if (conn->printer) {
1939 * Printers are handled completely differently.
1940 * Most of the passed parameters are ignored.
1943 if (pinfo) {
1944 *pinfo = FILE_WAS_CREATED;
1947 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1948 smb_fname_str_dbg(smb_fname)));
1950 if (!req) {
1951 DEBUG(0,("open_file_ntcreate: printer open without "
1952 "an SMB request!\n"));
1953 return NT_STATUS_INTERNAL_ERROR;
1956 return print_spool_open(fsp, smb_fname->base_name,
1957 req->vuid);
1960 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1961 NULL)) {
1962 return NT_STATUS_NO_MEMORY;
1965 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1966 posix_open = True;
1967 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1968 new_dos_attributes = 0;
1969 } else {
1970 /* Windows allows a new file to be created and
1971 silently removes a FILE_ATTRIBUTE_DIRECTORY
1972 sent by the client. Do the same. */
1974 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1976 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1977 * created new. */
1978 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1979 smb_fname, parent_dir);
1982 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1983 "access_mask=0x%x share_access=0x%x "
1984 "create_disposition = 0x%x create_options=0x%x "
1985 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1986 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1987 access_mask, share_access, create_disposition,
1988 create_options, (unsigned int)unx_mode, oplock_request,
1989 (unsigned int)private_flags));
1991 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1992 DEBUG(0, ("No smb request but not an internal only open!\n"));
1993 return NT_STATUS_INTERNAL_ERROR;
1997 * Only non-internal opens can be deferred at all
2000 if (req) {
2001 void *ptr;
2002 if (get_deferred_open_message_state(req,
2003 &request_time,
2004 &ptr)) {
2005 /* Remember the absolute time of the original
2006 request with this mid. We'll use it later to
2007 see if this has timed out. */
2009 /* If it was an async create retry, the file
2010 didn't exist. */
2012 if (is_deferred_open_async(ptr)) {
2013 SET_STAT_INVALID(smb_fname->st);
2014 file_existed = false;
2015 } else {
2016 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
2017 /* Remove the deferred open entry under lock. */
2018 remove_deferred_open_entry(
2019 state->id, req->mid,
2020 messaging_server_id(req->sconn->msg_ctx));
2023 /* Ensure we don't reprocess this message. */
2024 remove_deferred_open_message_smb(req->sconn, req->mid);
2028 if (!posix_open) {
2029 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
2030 if (file_existed) {
2031 existing_dos_attributes = dos_mode(conn, smb_fname);
2035 /* ignore any oplock requests if oplocks are disabled */
2036 if (!lp_oplocks(SNUM(conn)) ||
2037 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
2038 /* Mask off everything except the private Samba bits. */
2039 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2042 /* this is for OS/2 long file names - say we don't support them */
2043 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
2044 /* OS/2 Workplace shell fix may be main code stream in a later
2045 * release. */
2046 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2047 "supported.\n"));
2048 if (use_nt_status()) {
2049 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2051 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2054 switch( create_disposition ) {
2055 case FILE_OPEN:
2056 /* If file exists open. If file doesn't exist error. */
2057 if (!file_existed) {
2058 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2059 "requested for file %s and file "
2060 "doesn't exist.\n",
2061 smb_fname_str_dbg(smb_fname)));
2062 errno = ENOENT;
2063 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2065 break;
2067 case FILE_OVERWRITE:
2068 /* If file exists overwrite. If file doesn't exist
2069 * error. */
2070 if (!file_existed) {
2071 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2072 "requested for file %s and file "
2073 "doesn't exist.\n",
2074 smb_fname_str_dbg(smb_fname) ));
2075 errno = ENOENT;
2076 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2078 break;
2080 case FILE_CREATE:
2081 /* If file exists error. If file doesn't exist
2082 * create. */
2083 if (file_existed) {
2084 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2085 "requested for file %s and file "
2086 "already exists.\n",
2087 smb_fname_str_dbg(smb_fname)));
2088 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2089 errno = EISDIR;
2090 } else {
2091 errno = EEXIST;
2093 return map_nt_error_from_unix(errno);
2095 break;
2097 case FILE_SUPERSEDE:
2098 case FILE_OVERWRITE_IF:
2099 case FILE_OPEN_IF:
2100 break;
2101 default:
2102 return NT_STATUS_INVALID_PARAMETER;
2105 flags2 = disposition_to_open_flags(create_disposition);
2107 /* We only care about matching attributes on file exists and
2108 * overwrite. */
2110 if (!posix_open && file_existed &&
2111 ((create_disposition == FILE_OVERWRITE) ||
2112 (create_disposition == FILE_OVERWRITE_IF))) {
2113 if (!open_match_attributes(conn, existing_dos_attributes,
2114 new_dos_attributes,
2115 smb_fname->st.st_ex_mode,
2116 unx_mode, &new_unx_mode)) {
2117 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2118 "for file %s (%x %x) (0%o, 0%o)\n",
2119 smb_fname_str_dbg(smb_fname),
2120 existing_dos_attributes,
2121 new_dos_attributes,
2122 (unsigned int)smb_fname->st.st_ex_mode,
2123 (unsigned int)unx_mode ));
2124 errno = EACCES;
2125 return NT_STATUS_ACCESS_DENIED;
2129 status = smbd_calculate_access_mask(conn, smb_fname,
2130 false,
2131 access_mask,
2132 &access_mask);
2133 if (!NT_STATUS_IS_OK(status)) {
2134 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2135 "on file %s returned %s\n",
2136 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2137 return status;
2140 open_access_mask = access_mask;
2142 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
2143 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2146 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2147 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2148 access_mask));
2151 * Note that we ignore the append flag as append does not
2152 * mean the same thing under DOS and Unix.
2155 flags = calculate_open_access_flags(access_mask, oplock_request,
2156 private_flags);
2159 * Currently we only look at FILE_WRITE_THROUGH for create options.
2162 #if defined(O_SYNC)
2163 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2164 flags2 |= O_SYNC;
2166 #endif /* O_SYNC */
2168 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2169 flags2 |= O_APPEND;
2172 if (!posix_open && !CAN_WRITE(conn)) {
2174 * We should really return a permission denied error if either
2175 * O_CREAT or O_TRUNC are set, but for compatibility with
2176 * older versions of Samba we just AND them out.
2178 flags2 &= ~(O_CREAT|O_TRUNC);
2182 * Ensure we can't write on a read-only share or file.
2185 if (flags != O_RDONLY && file_existed &&
2186 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2187 DEBUG(5,("open_file_ntcreate: write access requested for "
2188 "file %s on read only %s\n",
2189 smb_fname_str_dbg(smb_fname),
2190 !CAN_WRITE(conn) ? "share" : "file" ));
2191 errno = EACCES;
2192 return NT_STATUS_ACCESS_DENIED;
2195 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2196 fsp->share_access = share_access;
2197 fsp->fh->private_options = private_flags;
2198 fsp->access_mask = open_access_mask; /* We change this to the
2199 * requested access_mask after
2200 * the open is done. */
2201 fsp->posix_open = posix_open;
2203 /* Ensure no SAMBA_PRIVATE bits can be set. */
2204 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2206 if (timeval_is_zero(&request_time)) {
2207 request_time = fsp->open_time;
2210 if (file_existed) {
2211 struct share_mode_entry *batch_entry = NULL;
2212 struct share_mode_entry *exclusive_entry = NULL;
2213 bool got_level2_oplock = false;
2214 bool got_a_none_oplock = false;
2215 struct file_id id;
2217 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2218 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2220 lck = get_share_mode_lock(talloc_tos(), id,
2221 conn->connectpath,
2222 smb_fname, &old_write_time);
2223 if (lck == NULL) {
2224 DEBUG(0, ("Could not get share mode lock\n"));
2225 return NT_STATUS_SHARING_VIOLATION;
2228 /* Get the types we need to examine. */
2229 find_oplock_types(fsp,
2230 oplock_request,
2231 lck,
2232 &batch_entry,
2233 &exclusive_entry,
2234 &got_level2_oplock,
2235 &got_a_none_oplock);
2237 /* First pass - send break only on batch oplocks. */
2238 if ((req != NULL) &&
2239 delay_for_batch_oplocks(fsp,
2240 req->mid,
2241 oplock_request,
2242 batch_entry)) {
2243 schedule_defer_open(lck, request_time, req);
2244 TALLOC_FREE(lck);
2245 return NT_STATUS_SHARING_VIOLATION;
2248 /* Use the client requested access mask here, not the one we
2249 * open with. */
2250 status = open_mode_check(conn, lck, fsp->name_hash,
2251 access_mask, share_access,
2252 create_options, &file_existed);
2254 if (NT_STATUS_IS_OK(status)) {
2255 /* We might be going to allow this open. Check oplock
2256 * status again. */
2257 /* Second pass - send break for both batch or
2258 * exclusive oplocks. */
2259 if ((req != NULL) &&
2260 delay_for_exclusive_oplocks(
2261 fsp,
2262 req->mid,
2263 oplock_request,
2264 exclusive_entry)) {
2265 schedule_defer_open(lck, request_time, req);
2266 TALLOC_FREE(lck);
2267 return NT_STATUS_SHARING_VIOLATION;
2271 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2272 /* DELETE_PENDING is not deferred for a second */
2273 TALLOC_FREE(lck);
2274 return status;
2277 grant_fsp_oplock_type(fsp,
2278 oplock_request,
2279 got_level2_oplock,
2280 got_a_none_oplock);
2282 if (!NT_STATUS_IS_OK(status)) {
2283 uint32 can_access_mask;
2284 bool can_access = True;
2286 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2288 /* Check if this can be done with the deny_dos and fcb
2289 * calls. */
2290 if (private_flags &
2291 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2292 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2293 if (req == NULL) {
2294 DEBUG(0, ("DOS open without an SMB "
2295 "request!\n"));
2296 TALLOC_FREE(lck);
2297 return NT_STATUS_INTERNAL_ERROR;
2300 /* Use the client requested access mask here,
2301 * not the one we open with. */
2302 status = fcb_or_dos_open(req,
2303 conn,
2304 fsp,
2305 smb_fname,
2307 req->smbpid,
2308 req->vuid,
2309 access_mask,
2310 share_access,
2311 create_options);
2313 if (NT_STATUS_IS_OK(status)) {
2314 TALLOC_FREE(lck);
2315 if (pinfo) {
2316 *pinfo = FILE_WAS_OPENED;
2318 return NT_STATUS_OK;
2323 * This next line is a subtlety we need for
2324 * MS-Access. If a file open will fail due to share
2325 * permissions and also for security (access) reasons,
2326 * we need to return the access failed error, not the
2327 * share error. We can't open the file due to kernel
2328 * oplock deadlock (it's possible we failed above on
2329 * the open_mode_check()) so use a userspace check.
2332 if (flags & O_RDWR) {
2333 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2334 } else if (flags & O_WRONLY) {
2335 can_access_mask = FILE_WRITE_DATA;
2336 } else {
2337 can_access_mask = FILE_READ_DATA;
2340 if (((can_access_mask & FILE_WRITE_DATA) &&
2341 !CAN_WRITE(conn)) ||
2342 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2343 smb_fname,
2344 false,
2345 can_access_mask))) {
2346 can_access = False;
2350 * If we're returning a share violation, ensure we
2351 * cope with the braindead 1 second delay.
2354 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2355 lp_defer_sharing_violations()) {
2356 struct timeval timeout;
2357 struct deferred_open_record state;
2358 int timeout_usecs;
2360 /* this is a hack to speed up torture tests
2361 in 'make test' */
2362 timeout_usecs = lp_parm_int(SNUM(conn),
2363 "smbd","sharedelay",
2364 SHARING_VIOLATION_USEC_WAIT);
2366 /* This is a relative time, added to the absolute
2367 request_time value to get the absolute timeout time.
2368 Note that if this is the second or greater time we enter
2369 this codepath for this particular request mid then
2370 request_time is left as the absolute time of the *first*
2371 time this request mid was processed. This is what allows
2372 the request to eventually time out. */
2374 timeout = timeval_set(0, timeout_usecs);
2376 /* Nothing actually uses state.delayed_for_oplocks
2377 but it's handy to differentiate in debug messages
2378 between a 30 second delay due to oplock break, and
2379 a 1 second delay for share mode conflicts. */
2381 state.delayed_for_oplocks = False;
2382 state.async_open = false;
2383 state.id = id;
2385 if ((req != NULL)
2386 && !request_timed_out(request_time,
2387 timeout)) {
2388 defer_open(lck, request_time, timeout,
2389 req, &state);
2393 TALLOC_FREE(lck);
2394 if (can_access) {
2396 * We have detected a sharing violation here
2397 * so return the correct error code
2399 status = NT_STATUS_SHARING_VIOLATION;
2400 } else {
2401 status = NT_STATUS_ACCESS_DENIED;
2403 return status;
2407 * We exit this block with the share entry *locked*.....
2411 SMB_ASSERT(!file_existed || (lck != NULL));
2414 * Ensure we pay attention to default ACLs on directories if required.
2417 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2418 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2419 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2422 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2423 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2424 (unsigned int)flags, (unsigned int)flags2,
2425 (unsigned int)unx_mode, (unsigned int)access_mask,
2426 (unsigned int)open_access_mask));
2428 fsp_open = open_file(fsp, conn, req, parent_dir,
2429 flags|flags2, unx_mode, access_mask,
2430 open_access_mask, &new_file_created);
2432 if (!NT_STATUS_IS_OK(fsp_open)) {
2433 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2434 schedule_async_open(request_time, req);
2436 TALLOC_FREE(lck);
2437 return fsp_open;
2440 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2442 * The file did exist, but some other (local or NFS)
2443 * process either renamed/unlinked and re-created the
2444 * file with different dev/ino after we walked the path,
2445 * but before we did the open. We could retry the
2446 * open but it's a rare enough case it's easier to
2447 * just fail the open to prevent creating any problems
2448 * in the open file db having the wrong dev/ino key.
2450 TALLOC_FREE(lck);
2451 fd_close(fsp);
2452 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2453 "Old (dev=0x%llu, ino =0x%llu). "
2454 "New (dev=0x%llu, ino=0x%llu). Failing open "
2455 " with NT_STATUS_ACCESS_DENIED.\n",
2456 smb_fname_str_dbg(smb_fname),
2457 (unsigned long long)saved_stat.st_ex_dev,
2458 (unsigned long long)saved_stat.st_ex_ino,
2459 (unsigned long long)smb_fname->st.st_ex_dev,
2460 (unsigned long long)smb_fname->st.st_ex_ino));
2461 return NT_STATUS_ACCESS_DENIED;
2464 if (!file_existed) {
2465 struct share_mode_entry *batch_entry = NULL;
2466 struct share_mode_entry *exclusive_entry = NULL;
2467 bool got_level2_oplock = false;
2468 bool got_a_none_oplock = false;
2469 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2470 struct file_id id;
2472 * Deal with the race condition where two smbd's detect the
2473 * file doesn't exist and do the create at the same time. One
2474 * of them will win and set a share mode, the other (ie. this
2475 * one) should check if the requested share mode for this
2476 * create is allowed.
2480 * Now the file exists and fsp is successfully opened,
2481 * fsp->dev and fsp->inode are valid and should replace the
2482 * dev=0,inode=0 from a non existent file. Spotted by
2483 * Nadav Danieli <nadavd@exanet.com>. JRA.
2486 id = fsp->file_id;
2488 lck = get_share_mode_lock(talloc_tos(), id,
2489 conn->connectpath,
2490 smb_fname, &old_write_time);
2492 if (lck == NULL) {
2493 DEBUG(0, ("open_file_ntcreate: Could not get share "
2494 "mode lock for %s\n",
2495 smb_fname_str_dbg(smb_fname)));
2496 fd_close(fsp);
2497 return NT_STATUS_SHARING_VIOLATION;
2500 /* Get the types we need to examine. */
2501 find_oplock_types(fsp,
2502 oplock_request,
2503 lck,
2504 &batch_entry,
2505 &exclusive_entry,
2506 &got_level2_oplock,
2507 &got_a_none_oplock);
2509 /* First pass - send break only on batch oplocks. */
2510 if ((req != NULL) &&
2511 delay_for_batch_oplocks(fsp,
2512 req->mid,
2513 oplock_request,
2514 batch_entry)) {
2515 schedule_defer_open(lck, request_time, req);
2516 TALLOC_FREE(lck);
2517 fd_close(fsp);
2518 return NT_STATUS_SHARING_VIOLATION;
2521 status = open_mode_check(conn, lck, fsp->name_hash,
2522 access_mask, share_access,
2523 create_options, &file_existed);
2525 if (NT_STATUS_IS_OK(status)) {
2526 /* We might be going to allow this open. Check oplock
2527 * status again. */
2528 /* Second pass - send break for both batch or
2529 * exclusive oplocks. */
2530 if ((req != NULL) &&
2531 delay_for_exclusive_oplocks(
2532 fsp,
2533 req->mid,
2534 oplock_request,
2535 exclusive_entry)) {
2536 schedule_defer_open(lck, request_time, req);
2537 TALLOC_FREE(lck);
2538 fd_close(fsp);
2539 return NT_STATUS_SHARING_VIOLATION;
2543 if (!NT_STATUS_IS_OK(status)) {
2544 struct deferred_open_record state;
2546 state.delayed_for_oplocks = False;
2547 state.async_open = false;
2548 state.id = id;
2550 /* Do it all over again immediately. In the second
2551 * round we will find that the file existed and handle
2552 * the DELETE_PENDING and FCB cases correctly. No need
2553 * to duplicate the code here. Essentially this is a
2554 * "goto top of this function", but don't tell
2555 * anybody... */
2557 if (req != NULL) {
2558 defer_open(lck, request_time, timeval_zero(),
2559 req, &state);
2561 TALLOC_FREE(lck);
2562 fd_close(fsp);
2563 return status;
2566 grant_fsp_oplock_type(fsp,
2567 oplock_request,
2568 got_level2_oplock,
2569 got_a_none_oplock);
2572 * We exit this block with the share entry *locked*.....
2577 SMB_ASSERT(lck != NULL);
2579 /* Delete streams if create_disposition requires it */
2580 if (!new_file_created && clear_ads(create_disposition) &&
2581 !is_ntfs_stream_smb_fname(smb_fname)) {
2582 status = delete_all_streams(conn, smb_fname->base_name);
2583 if (!NT_STATUS_IS_OK(status)) {
2584 TALLOC_FREE(lck);
2585 fd_close(fsp);
2586 return status;
2590 /* note that we ignore failure for the following. It is
2591 basically a hack for NFS, and NFS will never set one of
2592 these only read them. Nobody but Samba can ever set a deny
2593 mode and we have already checked our more authoritative
2594 locking database for permission to set this deny mode. If
2595 the kernel refuses the operations then the kernel is wrong.
2596 note that GPFS supports it as well - jmcd */
2598 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
2599 int ret_flock;
2600 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2601 if(ret_flock == -1 ){
2603 TALLOC_FREE(lck);
2604 fd_close(fsp);
2606 return NT_STATUS_SHARING_VIOLATION;
2611 * At this point onwards, we can guarantee that the share entry
2612 * is locked, whether we created the file or not, and that the
2613 * deny mode is compatible with all current opens.
2617 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2618 * but we don't have to store this - just ignore it on access check.
2620 if (conn->sconn->using_smb2) {
2622 * SMB2 doesn't return it (according to Microsoft tests).
2623 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2624 * File created with access = 0x7 (Read, Write, Delete)
2625 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2627 fsp->access_mask = access_mask;
2628 } else {
2629 /* But SMB1 does. */
2630 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2633 if (file_existed) {
2634 /* stat opens on existing files don't get oplocks. */
2635 if (is_stat_open(open_access_mask)) {
2636 fsp->oplock_type = NO_OPLOCK;
2640 if (new_file_created) {
2641 info = FILE_WAS_CREATED;
2642 } else {
2643 if (flags2 & O_TRUNC) {
2644 info = FILE_WAS_OVERWRITTEN;
2645 } else {
2646 info = FILE_WAS_OPENED;
2650 if (pinfo) {
2651 *pinfo = info;
2655 * Setup the oplock info in both the shared memory and
2656 * file structs.
2659 status = set_file_oplock(fsp, fsp->oplock_type);
2660 if (!NT_STATUS_IS_OK(status)) {
2662 * Could not get the kernel oplock or there are byte-range
2663 * locks on the file.
2665 fsp->oplock_type = NO_OPLOCK;
2668 set_share_mode(lck, fsp, get_current_uid(conn),
2669 req ? req->mid : 0,
2670 fsp->oplock_type);
2672 /* Handle strange delete on close create semantics. */
2673 if (create_options & FILE_DELETE_ON_CLOSE) {
2675 status = can_set_delete_on_close(fsp, new_dos_attributes);
2677 if (!NT_STATUS_IS_OK(status)) {
2678 /* Remember to delete the mode we just added. */
2679 del_share_mode(lck, fsp);
2680 TALLOC_FREE(lck);
2681 fd_close(fsp);
2682 return status;
2684 /* Note that here we set the *inital* delete on close flag,
2685 not the regular one. The magic gets handled in close. */
2686 fsp->initial_delete_on_close = True;
2689 if (info != FILE_WAS_OPENED) {
2690 /* Files should be initially set as archive */
2691 if (lp_map_archive(SNUM(conn)) ||
2692 lp_store_dos_attributes(SNUM(conn))) {
2693 if (!posix_open) {
2694 if (file_set_dosmode(conn, smb_fname,
2695 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2696 parent_dir, true) == 0) {
2697 unx_mode = smb_fname->st.st_ex_mode;
2703 /* Determine sparse flag. */
2704 if (posix_open) {
2705 /* POSIX opens are sparse by default. */
2706 fsp->is_sparse = true;
2707 } else {
2708 fsp->is_sparse = (file_existed &&
2709 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2713 * Take care of inherited ACLs on created files - if default ACL not
2714 * selected.
2717 if (!posix_open && new_file_created && !def_acl) {
2719 int saved_errno = errno; /* We might get ENOSYS in the next
2720 * call.. */
2722 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2723 errno == ENOSYS) {
2724 errno = saved_errno; /* Ignore ENOSYS */
2727 } else if (new_unx_mode) {
2729 int ret = -1;
2731 /* Attributes need changing. File already existed. */
2734 int saved_errno = errno; /* We might get ENOSYS in the
2735 * next call.. */
2736 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2738 if (ret == -1 && errno == ENOSYS) {
2739 errno = saved_errno; /* Ignore ENOSYS */
2740 } else {
2741 DEBUG(5, ("open_file_ntcreate: reset "
2742 "attributes of file %s to 0%o\n",
2743 smb_fname_str_dbg(smb_fname),
2744 (unsigned int)new_unx_mode));
2745 ret = 0; /* Don't do the fchmod below. */
2749 if ((ret == -1) &&
2750 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2751 DEBUG(5, ("open_file_ntcreate: failed to reset "
2752 "attributes of file %s to 0%o\n",
2753 smb_fname_str_dbg(smb_fname),
2754 (unsigned int)new_unx_mode));
2757 /* If this is a successful open, we must remove any deferred open
2758 * records. */
2759 if (req != NULL) {
2760 del_deferred_open_entry(lck, req->mid,
2761 messaging_server_id(req->sconn->msg_ctx));
2763 TALLOC_FREE(lck);
2765 return NT_STATUS_OK;
2769 /****************************************************************************
2770 Open a file for for write to ensure that we can fchmod it.
2771 ****************************************************************************/
2773 NTSTATUS open_file_fchmod(connection_struct *conn,
2774 struct smb_filename *smb_fname,
2775 files_struct **result)
2777 if (!VALID_STAT(smb_fname->st)) {
2778 return NT_STATUS_INVALID_PARAMETER;
2781 return SMB_VFS_CREATE_FILE(
2782 conn, /* conn */
2783 NULL, /* req */
2784 0, /* root_dir_fid */
2785 smb_fname, /* fname */
2786 FILE_WRITE_DATA, /* access_mask */
2787 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2788 FILE_SHARE_DELETE),
2789 FILE_OPEN, /* create_disposition*/
2790 0, /* create_options */
2791 0, /* file_attributes */
2792 INTERNAL_OPEN_ONLY, /* oplock_request */
2793 0, /* allocation_size */
2794 0, /* private_flags */
2795 NULL, /* sd */
2796 NULL, /* ea_list */
2797 result, /* result */
2798 NULL); /* pinfo */
2801 static NTSTATUS mkdir_internal(connection_struct *conn,
2802 struct smb_filename *smb_dname,
2803 uint32 file_attributes)
2805 mode_t mode;
2806 char *parent_dir = NULL;
2807 NTSTATUS status;
2808 bool posix_open = false;
2809 bool need_re_stat = false;
2810 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2812 if(access_mask & ~(conn->share_access)) {
2813 DEBUG(5,("mkdir_internal: failing share access "
2814 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
2815 return NT_STATUS_ACCESS_DENIED;
2818 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2819 NULL)) {
2820 return NT_STATUS_NO_MEMORY;
2823 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2824 posix_open = true;
2825 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2826 } else {
2827 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2830 status = check_parent_access(conn,
2831 smb_dname,
2832 access_mask);
2833 if(!NT_STATUS_IS_OK(status)) {
2834 DEBUG(5,("mkdir_internal: check_parent_access "
2835 "on directory %s for path %s returned %s\n",
2836 parent_dir,
2837 smb_dname->base_name,
2838 nt_errstr(status) ));
2839 return status;
2842 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2843 return map_nt_error_from_unix(errno);
2846 /* Ensure we're checking for a symlink here.... */
2847 /* We don't want to get caught by a symlink racer. */
2849 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2850 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2851 smb_fname_str_dbg(smb_dname), strerror(errno)));
2852 return map_nt_error_from_unix(errno);
2855 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2856 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2857 smb_fname_str_dbg(smb_dname)));
2858 return NT_STATUS_NOT_A_DIRECTORY;
2861 if (lp_store_dos_attributes(SNUM(conn))) {
2862 if (!posix_open) {
2863 file_set_dosmode(conn, smb_dname,
2864 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2865 parent_dir, true);
2869 if (lp_inherit_perms(SNUM(conn))) {
2870 inherit_access_posix_acl(conn, parent_dir,
2871 smb_dname->base_name, mode);
2872 need_re_stat = true;
2875 if (!posix_open) {
2877 * Check if high bits should have been set,
2878 * then (if bits are missing): add them.
2879 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2880 * dir.
2882 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2883 (mode & ~smb_dname->st.st_ex_mode)) {
2884 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2885 (smb_dname->st.st_ex_mode |
2886 (mode & ~smb_dname->st.st_ex_mode)));
2887 need_re_stat = true;
2891 /* Change the owner if required. */
2892 if (lp_inherit_owner(SNUM(conn))) {
2893 change_dir_owner_to_parent(conn, parent_dir,
2894 smb_dname->base_name,
2895 &smb_dname->st);
2896 need_re_stat = true;
2899 if (need_re_stat) {
2900 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2901 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2902 smb_fname_str_dbg(smb_dname), strerror(errno)));
2903 return map_nt_error_from_unix(errno);
2907 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2908 smb_dname->base_name);
2910 return NT_STATUS_OK;
2913 /****************************************************************************
2914 Open a directory from an NT SMB call.
2915 ****************************************************************************/
2917 static NTSTATUS open_directory(connection_struct *conn,
2918 struct smb_request *req,
2919 struct smb_filename *smb_dname,
2920 uint32 access_mask,
2921 uint32 share_access,
2922 uint32 create_disposition,
2923 uint32 create_options,
2924 uint32 file_attributes,
2925 int *pinfo,
2926 files_struct **result)
2928 files_struct *fsp = NULL;
2929 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2930 struct share_mode_lock *lck = NULL;
2931 NTSTATUS status;
2932 struct timespec mtimespec;
2933 int info = 0;
2935 if (is_ntfs_stream_smb_fname(smb_dname)) {
2936 DEBUG(2, ("open_directory: %s is a stream name!\n",
2937 smb_fname_str_dbg(smb_dname)));
2938 return NT_STATUS_NOT_A_DIRECTORY;
2941 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2942 /* Ensure we have a directory attribute. */
2943 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2946 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2947 "share_access = 0x%x create_options = 0x%x, "
2948 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2949 smb_fname_str_dbg(smb_dname),
2950 (unsigned int)access_mask,
2951 (unsigned int)share_access,
2952 (unsigned int)create_options,
2953 (unsigned int)create_disposition,
2954 (unsigned int)file_attributes));
2956 status = smbd_calculate_access_mask(conn, smb_dname, false,
2957 access_mask, &access_mask);
2958 if (!NT_STATUS_IS_OK(status)) {
2959 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2960 "on file %s returned %s\n",
2961 smb_fname_str_dbg(smb_dname),
2962 nt_errstr(status)));
2963 return status;
2966 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2967 !security_token_has_privilege(get_current_nttok(conn),
2968 SEC_PRIV_SECURITY)) {
2969 DEBUG(10, ("open_directory: open on %s "
2970 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2971 smb_fname_str_dbg(smb_dname)));
2972 return NT_STATUS_PRIVILEGE_NOT_HELD;
2975 switch( create_disposition ) {
2976 case FILE_OPEN:
2978 if (!dir_existed) {
2979 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2982 info = FILE_WAS_OPENED;
2983 break;
2985 case FILE_CREATE:
2987 /* If directory exists error. If directory doesn't
2988 * exist create. */
2990 if (dir_existed) {
2991 status = NT_STATUS_OBJECT_NAME_COLLISION;
2992 DEBUG(2, ("open_directory: unable to create "
2993 "%s. Error was %s\n",
2994 smb_fname_str_dbg(smb_dname),
2995 nt_errstr(status)));
2996 return status;
2999 status = mkdir_internal(conn, smb_dname,
3000 file_attributes);
3002 if (!NT_STATUS_IS_OK(status)) {
3003 DEBUG(2, ("open_directory: unable to create "
3004 "%s. Error was %s\n",
3005 smb_fname_str_dbg(smb_dname),
3006 nt_errstr(status)));
3007 return status;
3010 info = FILE_WAS_CREATED;
3011 break;
3013 case FILE_OPEN_IF:
3015 * If directory exists open. If directory doesn't
3016 * exist create.
3019 if (dir_existed) {
3020 status = NT_STATUS_OK;
3021 info = FILE_WAS_OPENED;
3022 } else {
3023 status = mkdir_internal(conn, smb_dname,
3024 file_attributes);
3026 if (NT_STATUS_IS_OK(status)) {
3027 info = FILE_WAS_CREATED;
3028 } else {
3029 /* Cope with create race. */
3030 if (!NT_STATUS_EQUAL(status,
3031 NT_STATUS_OBJECT_NAME_COLLISION)) {
3032 DEBUG(2, ("open_directory: unable to create "
3033 "%s. Error was %s\n",
3034 smb_fname_str_dbg(smb_dname),
3035 nt_errstr(status)));
3036 return status;
3038 info = FILE_WAS_OPENED;
3042 break;
3044 case FILE_SUPERSEDE:
3045 case FILE_OVERWRITE:
3046 case FILE_OVERWRITE_IF:
3047 default:
3048 DEBUG(5,("open_directory: invalid create_disposition "
3049 "0x%x for directory %s\n",
3050 (unsigned int)create_disposition,
3051 smb_fname_str_dbg(smb_dname)));
3052 return NT_STATUS_INVALID_PARAMETER;
3055 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3056 DEBUG(5,("open_directory: %s is not a directory !\n",
3057 smb_fname_str_dbg(smb_dname)));
3058 return NT_STATUS_NOT_A_DIRECTORY;
3061 if (info == FILE_WAS_OPENED) {
3062 status = smbd_check_access_rights(conn,
3063 smb_dname,
3064 false,
3065 access_mask);
3066 if (!NT_STATUS_IS_OK(status)) {
3067 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3068 "file %s failed with %s\n",
3069 smb_fname_str_dbg(smb_dname),
3070 nt_errstr(status)));
3071 return status;
3075 status = file_new(req, conn, &fsp);
3076 if(!NT_STATUS_IS_OK(status)) {
3077 return status;
3081 * Setup the files_struct for it.
3084 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3085 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3086 fsp->file_pid = req ? req->smbpid : 0;
3087 fsp->can_lock = False;
3088 fsp->can_read = False;
3089 fsp->can_write = False;
3091 fsp->share_access = share_access;
3092 fsp->fh->private_options = 0;
3094 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3096 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3097 fsp->print_file = NULL;
3098 fsp->modified = False;
3099 fsp->oplock_type = NO_OPLOCK;
3100 fsp->sent_oplock_break = NO_BREAK_SENT;
3101 fsp->is_directory = True;
3102 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
3103 status = fsp_set_smb_fname(fsp, smb_dname);
3104 if (!NT_STATUS_IS_OK(status)) {
3105 file_free(req, fsp);
3106 return status;
3109 mtimespec = smb_dname->st.st_ex_mtime;
3111 #ifdef O_DIRECTORY
3112 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3113 #else
3114 /* POSIX allows us to open a directory with O_RDONLY. */
3115 status = fd_open(conn, fsp, O_RDONLY, 0);
3116 #endif
3117 if (!NT_STATUS_IS_OK(status)) {
3118 DEBUG(5, ("open_directory: Could not open fd for "
3119 "%s (%s)\n",
3120 smb_fname_str_dbg(smb_dname),
3121 nt_errstr(status)));
3122 file_free(req, fsp);
3123 return status;
3126 status = vfs_stat_fsp(fsp);
3127 if (!NT_STATUS_IS_OK(status)) {
3128 fd_close(fsp);
3129 file_free(req, fsp);
3130 return status;
3133 /* Ensure there was no race condition. */
3134 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
3135 DEBUG(5,("open_directory: stat struct differs for "
3136 "directory %s.\n",
3137 smb_fname_str_dbg(smb_dname)));
3138 fd_close(fsp);
3139 file_free(req, fsp);
3140 return NT_STATUS_ACCESS_DENIED;
3143 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3144 conn->connectpath, smb_dname,
3145 &mtimespec);
3147 if (lck == NULL) {
3148 DEBUG(0, ("open_directory: Could not get share mode lock for "
3149 "%s\n", smb_fname_str_dbg(smb_dname)));
3150 fd_close(fsp);
3151 file_free(req, fsp);
3152 return NT_STATUS_SHARING_VIOLATION;
3155 status = open_mode_check(conn, lck, fsp->name_hash,
3156 access_mask, share_access,
3157 create_options, &dir_existed);
3159 if (!NT_STATUS_IS_OK(status)) {
3160 TALLOC_FREE(lck);
3161 fd_close(fsp);
3162 file_free(req, fsp);
3163 return status;
3166 set_share_mode(lck, fsp, get_current_uid(conn),
3167 req ? req->mid : 0, NO_OPLOCK);
3169 /* For directories the delete on close bit at open time seems
3170 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3171 if (create_options & FILE_DELETE_ON_CLOSE) {
3172 status = can_set_delete_on_close(fsp, 0);
3173 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3174 TALLOC_FREE(lck);
3175 fd_close(fsp);
3176 file_free(req, fsp);
3177 return status;
3180 if (NT_STATUS_IS_OK(status)) {
3181 /* Note that here we set the *inital* delete on close flag,
3182 not the regular one. The magic gets handled in close. */
3183 fsp->initial_delete_on_close = True;
3187 TALLOC_FREE(lck);
3189 if (pinfo) {
3190 *pinfo = info;
3193 *result = fsp;
3194 return NT_STATUS_OK;
3197 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3198 struct smb_filename *smb_dname)
3200 NTSTATUS status;
3201 files_struct *fsp;
3203 status = SMB_VFS_CREATE_FILE(
3204 conn, /* conn */
3205 req, /* req */
3206 0, /* root_dir_fid */
3207 smb_dname, /* fname */
3208 FILE_READ_ATTRIBUTES, /* access_mask */
3209 FILE_SHARE_NONE, /* share_access */
3210 FILE_CREATE, /* create_disposition*/
3211 FILE_DIRECTORY_FILE, /* create_options */
3212 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3213 0, /* oplock_request */
3214 0, /* allocation_size */
3215 0, /* private_flags */
3216 NULL, /* sd */
3217 NULL, /* ea_list */
3218 &fsp, /* result */
3219 NULL); /* pinfo */
3221 if (NT_STATUS_IS_OK(status)) {
3222 close_file(req, fsp, NORMAL_CLOSE);
3225 return status;
3228 /****************************************************************************
3229 Receive notification that one of our open files has been renamed by another
3230 smbd process.
3231 ****************************************************************************/
3233 void msg_file_was_renamed(struct messaging_context *msg,
3234 void *private_data,
3235 uint32_t msg_type,
3236 struct server_id server_id,
3237 DATA_BLOB *data)
3239 files_struct *fsp;
3240 char *frm = (char *)data->data;
3241 struct file_id id;
3242 const char *sharepath;
3243 const char *base_name;
3244 const char *stream_name;
3245 struct smb_filename *smb_fname = NULL;
3246 size_t sp_len, bn_len;
3247 NTSTATUS status;
3248 struct smbd_server_connection *sconn =
3249 talloc_get_type_abort(private_data,
3250 struct smbd_server_connection);
3252 if (data->data == NULL
3253 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3254 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3255 (int)data->length));
3256 return;
3259 /* Unpack the message. */
3260 pull_file_id_24(frm, &id);
3261 sharepath = &frm[24];
3262 sp_len = strlen(sharepath);
3263 base_name = sharepath + sp_len + 1;
3264 bn_len = strlen(base_name);
3265 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3267 /* stream_name must always be NULL if there is no stream. */
3268 if (stream_name[0] == '\0') {
3269 stream_name = NULL;
3272 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3273 stream_name, NULL, &smb_fname);
3274 if (!NT_STATUS_IS_OK(status)) {
3275 return;
3278 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3279 "file_id %s\n",
3280 sharepath, smb_fname_str_dbg(smb_fname),
3281 file_id_string_tos(&id)));
3283 for(fsp = file_find_di_first(sconn, id); fsp;
3284 fsp = file_find_di_next(fsp)) {
3285 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3287 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3288 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3289 smb_fname_str_dbg(smb_fname)));
3290 status = fsp_set_smb_fname(fsp, smb_fname);
3291 if (!NT_STATUS_IS_OK(status)) {
3292 goto out;
3294 } else {
3295 /* TODO. JRA. */
3296 /* Now we have the complete path we can work out if this is
3297 actually within this share and adjust newname accordingly. */
3298 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3299 "not sharepath %s) "
3300 "%s from %s -> %s\n",
3301 fsp->conn->connectpath,
3302 sharepath,
3303 fsp_fnum_dbg(fsp),
3304 fsp_str_dbg(fsp),
3305 smb_fname_str_dbg(smb_fname)));
3308 out:
3309 TALLOC_FREE(smb_fname);
3310 return;
3314 * If a main file is opened for delete, all streams need to be checked for
3315 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3316 * If that works, delete them all by setting the delete on close and close.
3319 NTSTATUS open_streams_for_delete(connection_struct *conn,
3320 const char *fname)
3322 struct stream_struct *stream_info = NULL;
3323 files_struct **streams = NULL;
3324 int i;
3325 unsigned int num_streams = 0;
3326 TALLOC_CTX *frame = talloc_stackframe();
3327 NTSTATUS status;
3329 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3330 &num_streams, &stream_info);
3332 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3333 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3334 DEBUG(10, ("no streams around\n"));
3335 TALLOC_FREE(frame);
3336 return NT_STATUS_OK;
3339 if (!NT_STATUS_IS_OK(status)) {
3340 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3341 nt_errstr(status)));
3342 goto fail;
3345 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3346 num_streams));
3348 if (num_streams == 0) {
3349 TALLOC_FREE(frame);
3350 return NT_STATUS_OK;
3353 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3354 if (streams == NULL) {
3355 DEBUG(0, ("talloc failed\n"));
3356 status = NT_STATUS_NO_MEMORY;
3357 goto fail;
3360 for (i=0; i<num_streams; i++) {
3361 struct smb_filename *smb_fname = NULL;
3363 if (strequal(stream_info[i].name, "::$DATA")) {
3364 streams[i] = NULL;
3365 continue;
3368 status = create_synthetic_smb_fname(talloc_tos(), fname,
3369 stream_info[i].name,
3370 NULL, &smb_fname);
3371 if (!NT_STATUS_IS_OK(status)) {
3372 goto fail;
3375 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3376 DEBUG(10, ("Unable to stat stream: %s\n",
3377 smb_fname_str_dbg(smb_fname)));
3380 status = SMB_VFS_CREATE_FILE(
3381 conn, /* conn */
3382 NULL, /* req */
3383 0, /* root_dir_fid */
3384 smb_fname, /* fname */
3385 DELETE_ACCESS, /* access_mask */
3386 (FILE_SHARE_READ | /* share_access */
3387 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3388 FILE_OPEN, /* create_disposition*/
3389 0, /* create_options */
3390 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3391 0, /* oplock_request */
3392 0, /* allocation_size */
3393 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3394 NULL, /* sd */
3395 NULL, /* ea_list */
3396 &streams[i], /* result */
3397 NULL); /* pinfo */
3399 if (!NT_STATUS_IS_OK(status)) {
3400 DEBUG(10, ("Could not open stream %s: %s\n",
3401 smb_fname_str_dbg(smb_fname),
3402 nt_errstr(status)));
3404 TALLOC_FREE(smb_fname);
3405 break;
3407 TALLOC_FREE(smb_fname);
3411 * don't touch the variable "status" beyond this point :-)
3414 for (i -= 1 ; i >= 0; i--) {
3415 if (streams[i] == NULL) {
3416 continue;
3419 DEBUG(10, ("Closing stream # %d, %s\n", i,
3420 fsp_str_dbg(streams[i])));
3421 close_file(NULL, streams[i], NORMAL_CLOSE);
3424 fail:
3425 TALLOC_FREE(frame);
3426 return status;
3429 /*********************************************************************
3430 Create a default ACL by inheriting from the parent. If no inheritance
3431 from the parent available, don't set anything. This will leave the actual
3432 permissions the new file or directory already got from the filesystem
3433 as the NT ACL when read.
3434 *********************************************************************/
3436 static NTSTATUS inherit_new_acl(files_struct *fsp)
3438 TALLOC_CTX *ctx = talloc_tos();
3439 char *parent_name = NULL;
3440 struct security_descriptor *parent_desc = NULL;
3441 NTSTATUS status = NT_STATUS_OK;
3442 struct security_descriptor *psd = NULL;
3443 struct dom_sid *owner_sid = NULL;
3444 struct dom_sid *group_sid = NULL;
3445 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3446 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3447 bool inheritable_components = false;
3448 size_t size = 0;
3450 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3451 return NT_STATUS_NO_MEMORY;
3454 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3455 parent_name,
3456 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3457 &parent_desc);
3458 if (!NT_STATUS_IS_OK(status)) {
3459 return status;
3462 inheritable_components = sd_has_inheritable_components(parent_desc,
3463 fsp->is_directory);
3465 if (!inheritable_components && !inherit_owner) {
3466 /* Nothing to inherit and not setting owner. */
3467 return NT_STATUS_OK;
3470 /* Create an inherited descriptor from the parent. */
3472 if (DEBUGLEVEL >= 10) {
3473 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3474 fsp_str_dbg(fsp) ));
3475 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3478 /* Inherit from parent descriptor if "inherit owner" set. */
3479 if (inherit_owner) {
3480 owner_sid = parent_desc->owner_sid;
3481 group_sid = parent_desc->group_sid;
3484 if (owner_sid == NULL) {
3485 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3487 if (group_sid == NULL) {
3488 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3491 status = se_create_child_secdesc(ctx,
3492 &psd,
3493 &size,
3494 parent_desc,
3495 owner_sid,
3496 group_sid,
3497 fsp->is_directory);
3498 if (!NT_STATUS_IS_OK(status)) {
3499 return status;
3502 /* If inheritable_components == false,
3503 se_create_child_secdesc()
3504 creates a security desriptor with a NULL dacl
3505 entry, but with SEC_DESC_DACL_PRESENT. We need
3506 to remove that flag. */
3508 if (!inheritable_components) {
3509 security_info_sent &= ~SECINFO_DACL;
3510 psd->type &= ~SEC_DESC_DACL_PRESENT;
3513 if (DEBUGLEVEL >= 10) {
3514 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3515 fsp_str_dbg(fsp) ));
3516 NDR_PRINT_DEBUG(security_descriptor, psd);
3519 if (inherit_owner) {
3520 /* We need to be root to force this. */
3521 become_root();
3523 status = SMB_VFS_FSET_NT_ACL(fsp,
3524 security_info_sent,
3525 psd);
3526 if (inherit_owner) {
3527 unbecome_root();
3529 return status;
3533 * Wrapper around open_file_ntcreate and open_directory
3536 static NTSTATUS create_file_unixpath(connection_struct *conn,
3537 struct smb_request *req,
3538 struct smb_filename *smb_fname,
3539 uint32_t access_mask,
3540 uint32_t share_access,
3541 uint32_t create_disposition,
3542 uint32_t create_options,
3543 uint32_t file_attributes,
3544 uint32_t oplock_request,
3545 uint64_t allocation_size,
3546 uint32_t private_flags,
3547 struct security_descriptor *sd,
3548 struct ea_list *ea_list,
3550 files_struct **result,
3551 int *pinfo)
3553 int info = FILE_WAS_OPENED;
3554 files_struct *base_fsp = NULL;
3555 files_struct *fsp = NULL;
3556 NTSTATUS status;
3558 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3559 "file_attributes = 0x%x, share_access = 0x%x, "
3560 "create_disposition = 0x%x create_options = 0x%x "
3561 "oplock_request = 0x%x private_flags = 0x%x "
3562 "ea_list = 0x%p, sd = 0x%p, "
3563 "fname = %s\n",
3564 (unsigned int)access_mask,
3565 (unsigned int)file_attributes,
3566 (unsigned int)share_access,
3567 (unsigned int)create_disposition,
3568 (unsigned int)create_options,
3569 (unsigned int)oplock_request,
3570 (unsigned int)private_flags,
3571 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3573 if (create_options & FILE_OPEN_BY_FILE_ID) {
3574 status = NT_STATUS_NOT_SUPPORTED;
3575 goto fail;
3578 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3579 status = NT_STATUS_INVALID_PARAMETER;
3580 goto fail;
3583 if (req == NULL) {
3584 oplock_request |= INTERNAL_OPEN_ONLY;
3587 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3588 && (access_mask & DELETE_ACCESS)
3589 && !is_ntfs_stream_smb_fname(smb_fname)) {
3591 * We can't open a file with DELETE access if any of the
3592 * streams is open without FILE_SHARE_DELETE
3594 status = open_streams_for_delete(conn, smb_fname->base_name);
3596 if (!NT_STATUS_IS_OK(status)) {
3597 goto fail;
3601 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3602 !security_token_has_privilege(get_current_nttok(conn),
3603 SEC_PRIV_SECURITY)) {
3604 DEBUG(10, ("create_file_unixpath: open on %s "
3605 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3606 smb_fname_str_dbg(smb_fname)));
3607 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3608 goto fail;
3611 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3612 && is_ntfs_stream_smb_fname(smb_fname)
3613 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3614 uint32 base_create_disposition;
3615 struct smb_filename *smb_fname_base = NULL;
3617 if (create_options & FILE_DIRECTORY_FILE) {
3618 status = NT_STATUS_NOT_A_DIRECTORY;
3619 goto fail;
3622 switch (create_disposition) {
3623 case FILE_OPEN:
3624 base_create_disposition = FILE_OPEN;
3625 break;
3626 default:
3627 base_create_disposition = FILE_OPEN_IF;
3628 break;
3631 /* Create an smb_filename with stream_name == NULL. */
3632 status = create_synthetic_smb_fname(talloc_tos(),
3633 smb_fname->base_name,
3634 NULL, NULL,
3635 &smb_fname_base);
3636 if (!NT_STATUS_IS_OK(status)) {
3637 goto fail;
3640 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3641 DEBUG(10, ("Unable to stat stream: %s\n",
3642 smb_fname_str_dbg(smb_fname_base)));
3645 /* Open the base file. */
3646 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3647 FILE_SHARE_READ
3648 | FILE_SHARE_WRITE
3649 | FILE_SHARE_DELETE,
3650 base_create_disposition,
3651 0, 0, 0, 0, 0, NULL, NULL,
3652 &base_fsp, NULL);
3653 TALLOC_FREE(smb_fname_base);
3655 if (!NT_STATUS_IS_OK(status)) {
3656 DEBUG(10, ("create_file_unixpath for base %s failed: "
3657 "%s\n", smb_fname->base_name,
3658 nt_errstr(status)));
3659 goto fail;
3661 /* we don't need to low level fd */
3662 fd_close(base_fsp);
3666 * If it's a request for a directory open, deal with it separately.
3669 if (create_options & FILE_DIRECTORY_FILE) {
3671 if (create_options & FILE_NON_DIRECTORY_FILE) {
3672 status = NT_STATUS_INVALID_PARAMETER;
3673 goto fail;
3676 /* Can't open a temp directory. IFS kit test. */
3677 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3678 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3679 status = NT_STATUS_INVALID_PARAMETER;
3680 goto fail;
3684 * We will get a create directory here if the Win32
3685 * app specified a security descriptor in the
3686 * CreateDirectory() call.
3689 oplock_request = 0;
3690 status = open_directory(
3691 conn, req, smb_fname, access_mask, share_access,
3692 create_disposition, create_options, file_attributes,
3693 &info, &fsp);
3694 } else {
3697 * Ordinary file case.
3700 status = file_new(req, conn, &fsp);
3701 if(!NT_STATUS_IS_OK(status)) {
3702 goto fail;
3705 status = fsp_set_smb_fname(fsp, smb_fname);
3706 if (!NT_STATUS_IS_OK(status)) {
3707 goto fail;
3710 if (base_fsp) {
3712 * We're opening the stream element of a
3713 * base_fsp we already opened. Set up the
3714 * base_fsp pointer.
3716 fsp->base_fsp = base_fsp;
3719 if (allocation_size) {
3720 fsp->initial_allocation_size = smb_roundup(fsp->conn,
3721 allocation_size);
3724 status = open_file_ntcreate(conn,
3725 req,
3726 access_mask,
3727 share_access,
3728 create_disposition,
3729 create_options,
3730 file_attributes,
3731 oplock_request,
3732 private_flags,
3733 &info,
3734 fsp);
3736 if(!NT_STATUS_IS_OK(status)) {
3737 file_free(req, fsp);
3738 fsp = NULL;
3741 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3743 /* A stream open never opens a directory */
3745 if (base_fsp) {
3746 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3747 goto fail;
3751 * Fail the open if it was explicitly a non-directory
3752 * file.
3755 if (create_options & FILE_NON_DIRECTORY_FILE) {
3756 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3757 goto fail;
3760 oplock_request = 0;
3761 status = open_directory(
3762 conn, req, smb_fname, access_mask,
3763 share_access, create_disposition,
3764 create_options, file_attributes,
3765 &info, &fsp);
3769 if (!NT_STATUS_IS_OK(status)) {
3770 goto fail;
3773 fsp->base_fsp = base_fsp;
3775 if ((ea_list != NULL) &&
3776 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3777 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3778 if (!NT_STATUS_IS_OK(status)) {
3779 goto fail;
3783 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3784 status = NT_STATUS_ACCESS_DENIED;
3785 goto fail;
3788 /* Save the requested allocation size. */
3789 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3790 if (allocation_size
3791 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3792 fsp->initial_allocation_size = smb_roundup(
3793 fsp->conn, allocation_size);
3794 if (fsp->is_directory) {
3795 /* Can't set allocation size on a directory. */
3796 status = NT_STATUS_ACCESS_DENIED;
3797 goto fail;
3799 if (vfs_allocate_file_space(
3800 fsp, fsp->initial_allocation_size) == -1) {
3801 status = NT_STATUS_DISK_FULL;
3802 goto fail;
3804 } else {
3805 fsp->initial_allocation_size = smb_roundup(
3806 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3808 } else {
3809 fsp->initial_allocation_size = 0;
3812 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3813 fsp->base_fsp == NULL) {
3814 if (sd != NULL) {
3816 * According to the MS documentation, the only time the security
3817 * descriptor is applied to the opened file is iff we *created* the
3818 * file; an existing file stays the same.
3820 * Also, it seems (from observation) that you can open the file with
3821 * any access mask but you can still write the sd. We need to override
3822 * the granted access before we call set_sd
3823 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3826 uint32_t sec_info_sent;
3827 uint32_t saved_access_mask = fsp->access_mask;
3829 sec_info_sent = get_sec_info(sd);
3831 fsp->access_mask = FILE_GENERIC_ALL;
3833 if (sec_info_sent & (SECINFO_OWNER|
3834 SECINFO_GROUP|
3835 SECINFO_DACL|
3836 SECINFO_SACL)) {
3837 status = set_sd(fsp, sd, sec_info_sent);
3840 fsp->access_mask = saved_access_mask;
3842 if (!NT_STATUS_IS_OK(status)) {
3843 goto fail;
3845 } else if (lp_inherit_acls(SNUM(conn))) {
3846 /* Inherit from parent. Errors here are not fatal. */
3847 status = inherit_new_acl(fsp);
3848 if (!NT_STATUS_IS_OK(status)) {
3849 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3850 fsp_str_dbg(fsp),
3851 nt_errstr(status) ));
3856 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3858 *result = fsp;
3859 if (pinfo != NULL) {
3860 *pinfo = info;
3863 smb_fname->st = fsp->fsp_name->st;
3865 return NT_STATUS_OK;
3867 fail:
3868 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3870 if (fsp != NULL) {
3871 if (base_fsp && fsp->base_fsp == base_fsp) {
3873 * The close_file below will close
3874 * fsp->base_fsp.
3876 base_fsp = NULL;
3878 close_file(req, fsp, ERROR_CLOSE);
3879 fsp = NULL;
3881 if (base_fsp != NULL) {
3882 close_file(req, base_fsp, ERROR_CLOSE);
3883 base_fsp = NULL;
3885 return status;
3889 * Calculate the full path name given a relative fid.
3891 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3892 struct smb_request *req,
3893 uint16_t root_dir_fid,
3894 const struct smb_filename *smb_fname,
3895 struct smb_filename **smb_fname_out)
3897 files_struct *dir_fsp;
3898 char *parent_fname = NULL;
3899 char *new_base_name = NULL;
3900 NTSTATUS status;
3902 if (root_dir_fid == 0 || !smb_fname) {
3903 status = NT_STATUS_INTERNAL_ERROR;
3904 goto out;
3907 dir_fsp = file_fsp(req, root_dir_fid);
3909 if (dir_fsp == NULL) {
3910 status = NT_STATUS_INVALID_HANDLE;
3911 goto out;
3914 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3915 status = NT_STATUS_INVALID_HANDLE;
3916 goto out;
3919 if (!dir_fsp->is_directory) {
3922 * Check to see if this is a mac fork of some kind.
3925 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3926 is_ntfs_stream_smb_fname(smb_fname)) {
3927 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3928 goto out;
3932 we need to handle the case when we get a
3933 relative open relative to a file and the
3934 pathname is blank - this is a reopen!
3935 (hint from demyn plantenberg)
3938 status = NT_STATUS_INVALID_HANDLE;
3939 goto out;
3942 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3944 * We're at the toplevel dir, the final file name
3945 * must not contain ./, as this is filtered out
3946 * normally by srvstr_get_path and unix_convert
3947 * explicitly rejects paths containing ./.
3949 parent_fname = talloc_strdup(talloc_tos(), "");
3950 if (parent_fname == NULL) {
3951 status = NT_STATUS_NO_MEMORY;
3952 goto out;
3954 } else {
3955 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3958 * Copy in the base directory name.
3961 parent_fname = talloc_array(talloc_tos(), char,
3962 dir_name_len+2);
3963 if (parent_fname == NULL) {
3964 status = NT_STATUS_NO_MEMORY;
3965 goto out;
3967 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3968 dir_name_len+1);
3971 * Ensure it ends in a '/'.
3972 * We used TALLOC_SIZE +2 to add space for the '/'.
3975 if(dir_name_len
3976 && (parent_fname[dir_name_len-1] != '\\')
3977 && (parent_fname[dir_name_len-1] != '/')) {
3978 parent_fname[dir_name_len] = '/';
3979 parent_fname[dir_name_len+1] = '\0';
3983 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3984 smb_fname->base_name);
3985 if (new_base_name == NULL) {
3986 status = NT_STATUS_NO_MEMORY;
3987 goto out;
3990 status = filename_convert(req,
3991 conn,
3992 req->flags2 & FLAGS2_DFS_PATHNAMES,
3993 new_base_name,
3995 NULL,
3996 smb_fname_out);
3997 if (!NT_STATUS_IS_OK(status)) {
3998 goto out;
4001 out:
4002 TALLOC_FREE(parent_fname);
4003 TALLOC_FREE(new_base_name);
4004 return status;
4007 NTSTATUS create_file_default(connection_struct *conn,
4008 struct smb_request *req,
4009 uint16_t root_dir_fid,
4010 struct smb_filename *smb_fname,
4011 uint32_t access_mask,
4012 uint32_t share_access,
4013 uint32_t create_disposition,
4014 uint32_t create_options,
4015 uint32_t file_attributes,
4016 uint32_t oplock_request,
4017 uint64_t allocation_size,
4018 uint32_t private_flags,
4019 struct security_descriptor *sd,
4020 struct ea_list *ea_list,
4021 files_struct **result,
4022 int *pinfo)
4024 int info = FILE_WAS_OPENED;
4025 files_struct *fsp = NULL;
4026 NTSTATUS status;
4027 bool stream_name = false;
4029 DEBUG(10,("create_file: access_mask = 0x%x "
4030 "file_attributes = 0x%x, share_access = 0x%x, "
4031 "create_disposition = 0x%x create_options = 0x%x "
4032 "oplock_request = 0x%x "
4033 "private_flags = 0x%x "
4034 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4035 "fname = %s\n",
4036 (unsigned int)access_mask,
4037 (unsigned int)file_attributes,
4038 (unsigned int)share_access,
4039 (unsigned int)create_disposition,
4040 (unsigned int)create_options,
4041 (unsigned int)oplock_request,
4042 (unsigned int)private_flags,
4043 (unsigned int)root_dir_fid,
4044 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4047 * Calculate the filename from the root_dir_if if necessary.
4050 if (root_dir_fid != 0) {
4051 struct smb_filename *smb_fname_out = NULL;
4052 status = get_relative_fid_filename(conn, req, root_dir_fid,
4053 smb_fname, &smb_fname_out);
4054 if (!NT_STATUS_IS_OK(status)) {
4055 goto fail;
4057 smb_fname = smb_fname_out;
4061 * Check to see if this is a mac fork of some kind.
4064 stream_name = is_ntfs_stream_smb_fname(smb_fname);
4065 if (stream_name) {
4066 enum FAKE_FILE_TYPE fake_file_type;
4068 fake_file_type = is_fake_file(smb_fname);
4070 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
4073 * Here we go! support for changing the disk quotas
4074 * --metze
4076 * We need to fake up to open this MAGIC QUOTA file
4077 * and return a valid FID.
4079 * w2k close this file directly after openening xp
4080 * also tries a QUERY_FILE_INFO on the file and then
4081 * close it
4083 status = open_fake_file(req, conn, req->vuid,
4084 fake_file_type, smb_fname,
4085 access_mask, &fsp);
4086 if (!NT_STATUS_IS_OK(status)) {
4087 goto fail;
4090 ZERO_STRUCT(smb_fname->st);
4091 goto done;
4094 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
4095 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4096 goto fail;
4100 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
4101 int ret;
4102 smb_fname->stream_name = NULL;
4103 /* We have to handle this error here. */
4104 if (create_options & FILE_DIRECTORY_FILE) {
4105 status = NT_STATUS_NOT_A_DIRECTORY;
4106 goto fail;
4108 if (lp_posix_pathnames()) {
4109 ret = SMB_VFS_LSTAT(conn, smb_fname);
4110 } else {
4111 ret = SMB_VFS_STAT(conn, smb_fname);
4114 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
4115 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4116 goto fail;
4120 status = create_file_unixpath(
4121 conn, req, smb_fname, access_mask, share_access,
4122 create_disposition, create_options, file_attributes,
4123 oplock_request, allocation_size, private_flags,
4124 sd, ea_list,
4125 &fsp, &info);
4127 if (!NT_STATUS_IS_OK(status)) {
4128 goto fail;
4131 done:
4132 DEBUG(10, ("create_file: info=%d\n", info));
4134 *result = fsp;
4135 if (pinfo != NULL) {
4136 *pinfo = info;
4138 return NT_STATUS_OK;
4140 fail:
4141 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
4143 if (fsp != NULL) {
4144 close_file(req, fsp, ERROR_CLOSE);
4145 fsp = NULL;
4147 return status;