Add comment explaining exactly *why* we don't check FILE_READ_ATTRIBUTES when evaluat...
[Samba.git] / source3 / smbd / open.c
blobf97a3ec5e581550a48b71bccb3c06f2212becd70
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 "auth.h"
31 #include "messages.h"
33 extern const struct generic_mapping file_generic_mapping;
35 struct deferred_open_record {
36 bool delayed_for_oplocks;
37 struct file_id id;
40 /****************************************************************************
41 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
42 ****************************************************************************/
44 NTSTATUS smb1_file_se_access_check(struct connection_struct *conn,
45 const struct security_descriptor *sd,
46 const struct security_token *token,
47 uint32_t access_desired,
48 uint32_t *access_granted)
50 *access_granted = 0;
52 if (get_current_uid(conn) == (uid_t)0) {
53 /* I'm sorry sir, I didn't know you were root... */
54 *access_granted = access_desired;
55 if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
56 *access_granted |= FILE_GENERIC_ALL;
58 return NT_STATUS_OK;
62 * If we can access the path to this file, by
63 * default we have FILE_READ_ATTRIBUTES from the
64 * containing directory. See the section:
65 * "Algorithm to Check Access to an Existing File"
66 * in MS-FSA.pdf.
68 return se_access_check(sd,
69 token,
70 (access_desired & ~FILE_READ_ATTRIBUTES),
71 access_granted);
74 /****************************************************************************
75 Check if we have open rights.
76 ****************************************************************************/
78 NTSTATUS smbd_check_open_rights(struct connection_struct *conn,
79 const struct smb_filename *smb_fname,
80 uint32_t access_mask,
81 uint32_t *access_granted)
83 /* Check if we have rights to open. */
84 NTSTATUS status;
85 struct security_descriptor *sd = NULL;
86 uint32_t rejected_share_access;
88 rejected_share_access = access_mask & ~(conn->share_access);
90 if (rejected_share_access) {
91 *access_granted = rejected_share_access;
92 return NT_STATUS_ACCESS_DENIED;
95 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
96 *access_granted = access_mask;
98 DEBUG(10,("smbd_check_open_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_granted ));
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_open_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_open_rights: Could not get acl "
122 "on %s: %s\n",
123 smb_fname_str_dbg(smb_fname),
124 nt_errstr(status)));
125 return status;
128 status = smb1_file_se_access_check(conn,
130 get_current_nttok(conn),
131 access_mask,
132 access_granted);
134 DEBUG(10,("smbd_check_open_rights: file %s requesting "
135 "0x%x returning 0x%x (%s)\n",
136 smb_fname_str_dbg(smb_fname),
137 (unsigned int)access_mask,
138 (unsigned int)*access_granted,
139 nt_errstr(status) ));
141 if (!NT_STATUS_IS_OK(status)) {
142 if (DEBUGLEVEL >= 10) {
143 DEBUG(10,("smbd_check_open_rights: acl for %s is:\n",
144 smb_fname_str_dbg(smb_fname) ));
145 NDR_PRINT_DEBUG(security_descriptor, sd);
149 TALLOC_FREE(sd);
151 return status;
154 /****************************************************************************
155 fd support routines - attempt to do a dos_open.
156 ****************************************************************************/
158 static NTSTATUS fd_open(struct connection_struct *conn,
159 files_struct *fsp,
160 int flags,
161 mode_t mode)
163 struct smb_filename *smb_fname = fsp->fsp_name;
164 NTSTATUS status = NT_STATUS_OK;
166 #ifdef O_NOFOLLOW
168 * Never follow symlinks on a POSIX client. The
169 * client should be doing this.
172 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
173 flags |= O_NOFOLLOW;
175 #endif
177 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
178 if (fsp->fh->fd == -1) {
179 status = map_nt_error_from_unix(errno);
180 if (errno == EMFILE) {
181 static time_t last_warned = 0L;
183 if (time((time_t *) NULL) > last_warned) {
184 DEBUG(0,("Too many open files, unable "
185 "to open more! smbd's max "
186 "open files = %d\n",
187 lp_max_open_files()));
188 last_warned = time((time_t *) NULL);
194 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
195 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
196 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
198 return status;
201 /****************************************************************************
202 Close the file associated with a fsp.
203 ****************************************************************************/
205 NTSTATUS fd_close(files_struct *fsp)
207 int ret;
209 if (fsp->dptr) {
210 dptr_CloseDir(fsp);
212 if (fsp->fh->fd == -1) {
213 return NT_STATUS_OK; /* What we used to call a stat open. */
215 if (fsp->fh->ref_count > 1) {
216 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
219 ret = SMB_VFS_CLOSE(fsp);
220 fsp->fh->fd = -1;
221 if (ret == -1) {
222 return map_nt_error_from_unix(errno);
224 return NT_STATUS_OK;
227 /****************************************************************************
228 Change the ownership of a file to that of the parent directory.
229 Do this by fd if possible.
230 ****************************************************************************/
232 void change_file_owner_to_parent(connection_struct *conn,
233 const char *inherit_from_dir,
234 files_struct *fsp)
236 struct smb_filename *smb_fname_parent = NULL;
237 NTSTATUS status;
238 int ret;
240 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
241 NULL, NULL, &smb_fname_parent);
242 if (!NT_STATUS_IS_OK(status)) {
243 return;
246 ret = SMB_VFS_STAT(conn, smb_fname_parent);
247 if (ret == -1) {
248 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
249 "directory %s. Error was %s\n",
250 smb_fname_str_dbg(smb_fname_parent),
251 strerror(errno)));
252 TALLOC_FREE(smb_fname_parent);
253 return;
256 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
257 /* Already this uid - no need to change. */
258 DEBUG(10,("change_file_owner_to_parent: file %s "
259 "is already owned by uid %d\n",
260 fsp_str_dbg(fsp),
261 (int)fsp->fsp_name->st.st_ex_uid ));
262 TALLOC_FREE(smb_fname_parent);
263 return;
266 become_root();
267 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
268 unbecome_root();
269 if (ret == -1) {
270 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
271 "file %s to parent directory uid %u. Error "
272 "was %s\n", fsp_str_dbg(fsp),
273 (unsigned int)smb_fname_parent->st.st_ex_uid,
274 strerror(errno) ));
275 } else {
276 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
277 "parent directory uid %u.\n", fsp_str_dbg(fsp),
278 (unsigned int)smb_fname_parent->st.st_ex_uid));
279 /* Ensure the uid entry is updated. */
280 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
283 TALLOC_FREE(smb_fname_parent);
286 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
287 const char *inherit_from_dir,
288 const char *fname,
289 SMB_STRUCT_STAT *psbuf)
291 struct smb_filename *smb_fname_parent = NULL;
292 struct smb_filename *smb_fname_cwd = NULL;
293 char *saved_dir = NULL;
294 TALLOC_CTX *ctx = talloc_tos();
295 NTSTATUS status = NT_STATUS_OK;
296 int ret;
298 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
299 &smb_fname_parent);
300 if (!NT_STATUS_IS_OK(status)) {
301 return status;
304 ret = SMB_VFS_STAT(conn, smb_fname_parent);
305 if (ret == -1) {
306 status = map_nt_error_from_unix(errno);
307 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
308 "directory %s. Error was %s\n",
309 smb_fname_str_dbg(smb_fname_parent),
310 strerror(errno)));
311 goto out;
314 /* We've already done an lstat into psbuf, and we know it's a
315 directory. If we can cd into the directory and the dev/ino
316 are the same then we can safely chown without races as
317 we're locking the directory in place by being in it. This
318 should work on any UNIX (thanks tridge :-). JRA.
321 saved_dir = vfs_GetWd(ctx,conn);
322 if (!saved_dir) {
323 status = map_nt_error_from_unix(errno);
324 DEBUG(0,("change_dir_owner_to_parent: failed to get "
325 "current working directory. Error was %s\n",
326 strerror(errno)));
327 goto out;
330 /* Chdir into the new path. */
331 if (vfs_ChDir(conn, fname) == -1) {
332 status = map_nt_error_from_unix(errno);
333 DEBUG(0,("change_dir_owner_to_parent: failed to change "
334 "current working directory to %s. Error "
335 "was %s\n", fname, strerror(errno) ));
336 goto chdir;
339 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
340 &smb_fname_cwd);
341 if (!NT_STATUS_IS_OK(status)) {
342 return status;
345 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
346 if (ret == -1) {
347 status = map_nt_error_from_unix(errno);
348 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
349 "directory '.' (%s) Error was %s\n",
350 fname, strerror(errno)));
351 goto chdir;
354 /* Ensure we're pointing at the same place. */
355 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
356 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
357 DEBUG(0,("change_dir_owner_to_parent: "
358 "device/inode on directory %s changed. "
359 "Refusing to chown !\n", fname ));
360 status = NT_STATUS_ACCESS_DENIED;
361 goto chdir;
364 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
365 /* Already this uid - no need to change. */
366 DEBUG(10,("change_dir_owner_to_parent: directory %s "
367 "is already owned by uid %d\n",
368 fname,
369 (int)smb_fname_cwd->st.st_ex_uid ));
370 status = NT_STATUS_OK;
371 goto chdir;
374 become_root();
375 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
376 (gid_t)-1);
377 unbecome_root();
378 if (ret == -1) {
379 status = map_nt_error_from_unix(errno);
380 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
381 "directory %s to parent directory uid %u. "
382 "Error was %s\n", fname,
383 (unsigned int)smb_fname_parent->st.st_ex_uid,
384 strerror(errno) ));
385 goto chdir;
388 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
389 "directory %s to parent directory uid %u.\n",
390 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
392 chdir:
393 vfs_ChDir(conn,saved_dir);
394 out:
395 TALLOC_FREE(smb_fname_parent);
396 TALLOC_FREE(smb_fname_cwd);
397 return status;
400 /****************************************************************************
401 Open a file.
402 ****************************************************************************/
404 static NTSTATUS open_file(files_struct *fsp,
405 connection_struct *conn,
406 struct smb_request *req,
407 const char *parent_dir,
408 int flags,
409 mode_t unx_mode,
410 uint32 access_mask, /* client requested access mask. */
411 uint32 open_access_mask) /* what we're actually using in the open. */
413 struct smb_filename *smb_fname = fsp->fsp_name;
414 NTSTATUS status = NT_STATUS_OK;
415 int accmode = (flags & O_ACCMODE);
416 int local_flags = flags;
417 bool file_existed = VALID_STAT(fsp->fsp_name->st);
418 bool file_created = false;
420 fsp->fh->fd = -1;
421 errno = EPERM;
423 /* Check permissions */
426 * This code was changed after seeing a client open request
427 * containing the open mode of (DENY_WRITE/read-only) with
428 * the 'create if not exist' bit set. The previous code
429 * would fail to open the file read only on a read-only share
430 * as it was checking the flags parameter directly against O_RDONLY,
431 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
432 * JRA.
435 if (!CAN_WRITE(conn)) {
436 /* It's a read-only share - fail if we wanted to write. */
437 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
438 DEBUG(3,("Permission denied opening %s\n",
439 smb_fname_str_dbg(smb_fname)));
440 return NT_STATUS_ACCESS_DENIED;
441 } else if(flags & O_CREAT) {
442 /* We don't want to write - but we must make sure that
443 O_CREAT doesn't create the file if we have write
444 access into the directory.
446 flags &= ~(O_CREAT|O_EXCL);
447 local_flags &= ~(O_CREAT|O_EXCL);
452 * This little piece of insanity is inspired by the
453 * fact that an NT client can open a file for O_RDONLY,
454 * but set the create disposition to FILE_EXISTS_TRUNCATE.
455 * If the client *can* write to the file, then it expects to
456 * truncate the file, even though it is opening for readonly.
457 * Quicken uses this stupid trick in backup file creation...
458 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
459 * for helping track this one down. It didn't bite us in 2.0.x
460 * as we always opened files read-write in that release. JRA.
463 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
464 DEBUG(10,("open_file: truncate requested on read-only open "
465 "for file %s\n", smb_fname_str_dbg(smb_fname)));
466 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
469 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
470 (!file_existed && (local_flags & O_CREAT)) ||
471 ((local_flags & O_TRUNC) == O_TRUNC) ) {
472 const char *wild;
475 * We can't actually truncate here as the file may be locked.
476 * open_file_ntcreate will take care of the truncate later. JRA.
479 local_flags &= ~O_TRUNC;
481 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
483 * We would block on opening a FIFO with no one else on the
484 * other end. Do what we used to do and add O_NONBLOCK to the
485 * open flags. JRA.
488 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
489 local_flags |= O_NONBLOCK;
491 #endif
493 /* Don't create files with Microsoft wildcard characters. */
494 if (fsp->base_fsp) {
496 * wildcard characters are allowed in stream names
497 * only test the basefilename
499 wild = fsp->base_fsp->fsp_name->base_name;
500 } else {
501 wild = smb_fname->base_name;
503 if ((local_flags & O_CREAT) && !file_existed &&
504 ms_has_wild(wild)) {
505 return NT_STATUS_OBJECT_NAME_INVALID;
508 /* Actually do the open */
509 status = fd_open(conn, fsp, local_flags, unx_mode);
510 if (!NT_STATUS_IS_OK(status)) {
511 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
512 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
513 nt_errstr(status),local_flags,flags));
514 return status;
517 if ((local_flags & O_CREAT) && !file_existed) {
518 file_created = true;
521 } else {
522 fsp->fh->fd = -1; /* What we used to call a stat open. */
523 if (file_existed) {
524 uint32_t access_granted = 0;
526 status = smbd_check_open_rights(conn,
527 smb_fname,
528 access_mask,
529 &access_granted);
530 if (!NT_STATUS_IS_OK(status)) {
531 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
533 * On NT_STATUS_ACCESS_DENIED, access_granted
534 * contains the denied bits.
537 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
538 (access_granted & FILE_WRITE_ATTRIBUTES) &&
539 (lp_map_readonly(SNUM(conn)) ||
540 lp_map_archive(SNUM(conn)) ||
541 lp_map_hidden(SNUM(conn)) ||
542 lp_map_system(SNUM(conn)))) {
543 access_granted &= ~FILE_WRITE_ATTRIBUTES;
545 DEBUG(10,("open_file: "
546 "overrode "
547 "FILE_WRITE_"
548 "ATTRIBUTES "
549 "on file %s\n",
550 smb_fname_str_dbg(
551 smb_fname)));
554 if ((access_mask & DELETE_ACCESS) &&
555 (access_granted & DELETE_ACCESS) &&
556 can_delete_file_in_directory(conn,
557 smb_fname)) {
558 /* Were we trying to do a stat open
559 * for delete and didn't get DELETE
560 * access (only) ? Check if the
561 * directory allows DELETE_CHILD.
562 * See here:
563 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
564 * for details. */
566 access_granted &= ~DELETE_ACCESS;
568 DEBUG(10,("open_file: "
569 "overrode "
570 "DELETE_ACCESS on "
571 "file %s\n",
572 smb_fname_str_dbg(
573 smb_fname)));
576 if (access_granted != 0) {
577 DEBUG(10,("open_file: Access "
578 "denied on file "
579 "%s\n",
580 smb_fname_str_dbg(
581 smb_fname)));
582 return status;
584 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
585 fsp->posix_open &&
586 S_ISLNK(smb_fname->st.st_ex_mode)) {
587 /* This is a POSIX stat open for delete
588 * or rename on a symlink that points
589 * nowhere. Allow. */
590 DEBUG(10,("open_file: allowing POSIX "
591 "open on bad symlink %s\n",
592 smb_fname_str_dbg(
593 smb_fname)));
594 } else {
595 DEBUG(10,("open_file: "
596 "smbd_check_open_rights on file "
597 "%s returned %s\n",
598 smb_fname_str_dbg(smb_fname),
599 nt_errstr(status) ));
600 return status;
606 if (!file_existed) {
607 int ret;
609 if (fsp->fh->fd == -1) {
610 ret = SMB_VFS_STAT(conn, smb_fname);
611 } else {
612 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
613 /* If we have an fd, this stat should succeed. */
614 if (ret == -1) {
615 DEBUG(0,("Error doing fstat on open file %s "
616 "(%s)\n",
617 smb_fname_str_dbg(smb_fname),
618 strerror(errno) ));
622 /* For a non-io open, this stat failing means file not found. JRA */
623 if (ret == -1) {
624 status = map_nt_error_from_unix(errno);
625 fd_close(fsp);
626 return status;
629 if (file_created) {
630 bool need_re_stat = false;
631 /* Do all inheritance work after we've
632 done a successful stat call and filled
633 in the stat struct in fsp->fsp_name. */
635 /* Inherit the ACL if required */
636 if (lp_inherit_perms(SNUM(conn))) {
637 inherit_access_posix_acl(conn, parent_dir,
638 smb_fname->base_name,
639 unx_mode);
640 need_re_stat = true;
643 /* Change the owner if required. */
644 if (lp_inherit_owner(SNUM(conn))) {
645 change_file_owner_to_parent(conn, parent_dir,
646 fsp);
647 need_re_stat = true;
650 if (need_re_stat) {
651 if (fsp->fh->fd == -1) {
652 ret = SMB_VFS_STAT(conn, smb_fname);
653 } else {
654 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
655 /* If we have an fd, this stat should succeed. */
656 if (ret == -1) {
657 DEBUG(0,("Error doing fstat on open file %s "
658 "(%s)\n",
659 smb_fname_str_dbg(smb_fname),
660 strerror(errno) ));
665 notify_fname(conn, NOTIFY_ACTION_ADDED,
666 FILE_NOTIFY_CHANGE_FILE_NAME,
667 smb_fname->base_name);
672 * POSIX allows read-only opens of directories. We don't
673 * want to do this (we use a different code path for this)
674 * so catch a directory open and return an EISDIR. JRA.
677 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
678 fd_close(fsp);
679 errno = EISDIR;
680 return NT_STATUS_FILE_IS_A_DIRECTORY;
683 fsp->mode = smb_fname->st.st_ex_mode;
684 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
685 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
686 fsp->file_pid = req ? req->smbpid : 0;
687 fsp->can_lock = True;
688 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
689 if (!CAN_WRITE(conn)) {
690 fsp->can_write = False;
691 } else {
692 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
693 True : False;
695 fsp->print_file = NULL;
696 fsp->modified = False;
697 fsp->sent_oplock_break = NO_BREAK_SENT;
698 fsp->is_directory = False;
699 if (conn->aio_write_behind_list &&
700 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
701 conn->case_sensitive)) {
702 fsp->aio_write_behind = True;
705 fsp->wcp = NULL; /* Write cache pointer. */
707 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
708 conn->session_info->unix_name,
709 smb_fname_str_dbg(smb_fname),
710 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
711 conn->num_files_open));
713 errno = 0;
714 return NT_STATUS_OK;
717 /****************************************************************************
718 Check if we can open a file with a share mode.
719 Returns True if conflict, False if not.
720 ****************************************************************************/
722 static bool share_conflict(struct share_mode_entry *entry,
723 uint32 access_mask,
724 uint32 share_access)
726 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
727 "entry->share_access = 0x%x, "
728 "entry->private_options = 0x%x\n",
729 (unsigned int)entry->access_mask,
730 (unsigned int)entry->share_access,
731 (unsigned int)entry->private_options));
733 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
734 (unsigned int)access_mask, (unsigned int)share_access));
736 if ((entry->access_mask & (FILE_WRITE_DATA|
737 FILE_APPEND_DATA|
738 FILE_READ_DATA|
739 FILE_EXECUTE|
740 DELETE_ACCESS)) == 0) {
741 DEBUG(10,("share_conflict: No conflict due to "
742 "entry->access_mask = 0x%x\n",
743 (unsigned int)entry->access_mask ));
744 return False;
747 if ((access_mask & (FILE_WRITE_DATA|
748 FILE_APPEND_DATA|
749 FILE_READ_DATA|
750 FILE_EXECUTE|
751 DELETE_ACCESS)) == 0) {
752 DEBUG(10,("share_conflict: No conflict due to "
753 "access_mask = 0x%x\n",
754 (unsigned int)access_mask ));
755 return False;
758 #if 1 /* JRA TEST - Superdebug. */
759 #define CHECK_MASK(num, am, right, sa, share) \
760 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
761 (unsigned int)(num), (unsigned int)(am), \
762 (unsigned int)(right), (unsigned int)(am)&(right) )); \
763 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
764 (unsigned int)(num), (unsigned int)(sa), \
765 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
766 if (((am) & (right)) && !((sa) & (share))) { \
767 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
768 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
769 (unsigned int)(share) )); \
770 return True; \
772 #else
773 #define CHECK_MASK(num, am, right, sa, share) \
774 if (((am) & (right)) && !((sa) & (share))) { \
775 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
776 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
777 (unsigned int)(share) )); \
778 return True; \
780 #endif
782 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
783 share_access, FILE_SHARE_WRITE);
784 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
785 entry->share_access, FILE_SHARE_WRITE);
787 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
788 share_access, FILE_SHARE_READ);
789 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
790 entry->share_access, FILE_SHARE_READ);
792 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
793 share_access, FILE_SHARE_DELETE);
794 CHECK_MASK(6, access_mask, DELETE_ACCESS,
795 entry->share_access, FILE_SHARE_DELETE);
797 DEBUG(10,("share_conflict: No conflict.\n"));
798 return False;
801 #if defined(DEVELOPER)
802 static void validate_my_share_entries(struct smbd_server_connection *sconn,
803 int num,
804 struct share_mode_entry *share_entry)
806 files_struct *fsp;
808 if (!procid_is_me(&share_entry->pid)) {
809 return;
812 if (is_deferred_open_entry(share_entry) &&
813 !open_was_deferred(share_entry->op_mid)) {
814 char *str = talloc_asprintf(talloc_tos(),
815 "Got a deferred entry without a request: "
816 "PANIC: %s\n",
817 share_mode_str(talloc_tos(), num, share_entry));
818 smb_panic(str);
821 if (!is_valid_share_mode_entry(share_entry)) {
822 return;
825 fsp = file_find_dif(sconn, share_entry->id,
826 share_entry->share_file_id);
827 if (!fsp) {
828 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
829 share_mode_str(talloc_tos(), num, share_entry) ));
830 smb_panic("validate_my_share_entries: Cannot match a "
831 "share entry with an open file\n");
834 if (is_deferred_open_entry(share_entry) ||
835 is_unused_share_mode_entry(share_entry)) {
836 goto panic;
839 if ((share_entry->op_type == NO_OPLOCK) &&
840 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
841 /* Someone has already written to it, but I haven't yet
842 * noticed */
843 return;
846 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
847 goto panic;
850 return;
852 panic:
854 char *str;
855 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
856 share_mode_str(talloc_tos(), num, share_entry) ));
857 str = talloc_asprintf(talloc_tos(),
858 "validate_my_share_entries: "
859 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
860 fsp->fsp_name->base_name,
861 (unsigned int)fsp->oplock_type,
862 (unsigned int)share_entry->op_type );
863 smb_panic(str);
866 #endif
868 bool is_stat_open(uint32 access_mask)
870 return (access_mask &&
871 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
872 FILE_WRITE_ATTRIBUTES))==0) &&
873 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
874 FILE_WRITE_ATTRIBUTES)) != 0));
877 /****************************************************************************
878 Deal with share modes
879 Invarient: Share mode must be locked on entry and exit.
880 Returns -1 on error, or number of share modes on success (may be zero).
881 ****************************************************************************/
883 static NTSTATUS open_mode_check(connection_struct *conn,
884 struct share_mode_lock *lck,
885 uint32_t name_hash,
886 uint32 access_mask,
887 uint32 share_access,
888 uint32 create_options,
889 bool *file_existed)
891 int i;
893 if(lck->num_share_modes == 0) {
894 return NT_STATUS_OK;
897 *file_existed = True;
899 /* A delete on close prohibits everything */
901 if (is_delete_on_close_set(lck, name_hash)) {
902 return NT_STATUS_DELETE_PENDING;
905 if (is_stat_open(access_mask)) {
906 /* Stat open that doesn't trigger oplock breaks or share mode
907 * checks... ! JRA. */
908 return NT_STATUS_OK;
912 * Check if the share modes will give us access.
915 #if defined(DEVELOPER)
916 for(i = 0; i < lck->num_share_modes; i++) {
917 validate_my_share_entries(conn->sconn, i,
918 &lck->share_modes[i]);
920 #endif
922 if (!lp_share_modes(SNUM(conn))) {
923 return NT_STATUS_OK;
926 /* Now we check the share modes, after any oplock breaks. */
927 for(i = 0; i < lck->num_share_modes; i++) {
929 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
930 continue;
933 /* someone else has a share lock on it, check to see if we can
934 * too */
935 if (share_conflict(&lck->share_modes[i],
936 access_mask, share_access)) {
937 return NT_STATUS_SHARING_VIOLATION;
941 return NT_STATUS_OK;
945 * Send a break message to the oplock holder and delay the open for
946 * our client.
949 static NTSTATUS send_break_message(files_struct *fsp,
950 struct share_mode_entry *exclusive,
951 uint64_t mid,
952 int oplock_request)
954 NTSTATUS status;
955 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
957 DEBUG(10, ("Sending break request to PID %s\n",
958 procid_str_static(&exclusive->pid)));
959 exclusive->op_mid = mid;
961 /* Create the message. */
962 share_mode_entry_to_message(msg, exclusive);
964 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
965 don't want this set in the share mode struct pointed to by lck. */
967 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
968 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
969 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
972 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
973 MSG_SMB_BREAK_REQUEST,
974 (uint8 *)msg,
975 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
976 if (!NT_STATUS_IS_OK(status)) {
977 DEBUG(3, ("Could not send oplock break message: %s\n",
978 nt_errstr(status)));
981 return status;
985 * Return share_mode_entry pointers for :
986 * 1). Batch oplock entry.
987 * 2). Batch or exclusive oplock entry (may be identical to #1).
988 * bool have_level2_oplock
989 * bool have_no_oplock.
990 * Do internal consistency checks on the share mode for a file.
993 static void find_oplock_types(files_struct *fsp,
994 int oplock_request,
995 struct share_mode_lock *lck,
996 struct share_mode_entry **pp_batch,
997 struct share_mode_entry **pp_ex_or_batch,
998 bool *got_level2,
999 bool *got_no_oplock)
1001 int i;
1003 *pp_batch = NULL;
1004 *pp_ex_or_batch = NULL;
1005 *got_level2 = false;
1006 *got_no_oplock = false;
1008 /* Ignore stat or internal opens, as is done in
1009 delay_for_batch_oplocks() and
1010 delay_for_exclusive_oplocks().
1012 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1013 return;
1016 for (i=0; i<lck->num_share_modes; i++) {
1017 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
1018 continue;
1021 if (lck->share_modes[i].op_type == NO_OPLOCK &&
1022 is_stat_open(lck->share_modes[i].access_mask)) {
1023 /* We ignore stat opens in the table - they
1024 always have NO_OPLOCK and never get or
1025 cause breaks. JRA. */
1026 continue;
1029 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1030 /* batch - can only be one. */
1031 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1032 smb_panic("Bad batch oplock entry.");
1034 *pp_batch = &lck->share_modes[i];
1037 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1038 /* Exclusive or batch - can only be one. */
1039 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1040 smb_panic("Bad exclusive or batch oplock entry.");
1042 *pp_ex_or_batch = &lck->share_modes[i];
1045 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1046 if (*pp_batch || *pp_ex_or_batch) {
1047 smb_panic("Bad levelII oplock entry.");
1049 *got_level2 = true;
1052 if (lck->share_modes[i].op_type == NO_OPLOCK) {
1053 if (*pp_batch || *pp_ex_or_batch) {
1054 smb_panic("Bad no oplock entry.");
1056 *got_no_oplock = true;
1061 static bool delay_for_batch_oplocks(files_struct *fsp,
1062 uint64_t mid,
1063 int oplock_request,
1064 struct share_mode_entry *batch_entry)
1066 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1067 return false;
1070 if (batch_entry != NULL) {
1071 /* Found a batch oplock */
1072 send_break_message(fsp, batch_entry, mid, oplock_request);
1073 return true;
1075 return false;
1078 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1079 uint64_t mid,
1080 int oplock_request,
1081 struct share_mode_entry *ex_entry)
1083 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1084 return false;
1087 if (ex_entry != NULL) {
1088 send_break_message(fsp, ex_entry, mid, oplock_request);
1089 return true;
1091 return false;
1094 static void grant_fsp_oplock_type(files_struct *fsp,
1095 const struct byte_range_lock *br_lck,
1096 int oplock_request,
1097 bool got_level2_oplock,
1098 bool got_a_none_oplock)
1100 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1101 lp_level2_oplocks(SNUM(fsp->conn));
1103 /* Start by granting what the client asked for,
1104 but ensure no SAMBA_PRIVATE bits can be set. */
1105 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1107 if (oplock_request & INTERNAL_OPEN_ONLY) {
1108 /* No oplocks on internal open. */
1109 fsp->oplock_type = NO_OPLOCK;
1110 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1111 fsp->oplock_type, fsp_str_dbg(fsp)));
1112 return;
1113 } else if (br_lck && br_lck->num_locks > 0) {
1114 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1115 fsp_str_dbg(fsp)));
1116 fsp->oplock_type = NO_OPLOCK;
1119 if (is_stat_open(fsp->access_mask)) {
1120 /* Leave the value already set. */
1121 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1122 fsp->oplock_type, fsp_str_dbg(fsp)));
1123 return;
1127 * Match what was requested (fsp->oplock_type) with
1128 * what was found in the existing share modes.
1131 if (got_a_none_oplock) {
1132 fsp->oplock_type = NO_OPLOCK;
1133 } else if (got_level2_oplock) {
1134 if (fsp->oplock_type == NO_OPLOCK ||
1135 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1136 /* Store a level2 oplock, but don't tell the client */
1137 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1138 } else {
1139 fsp->oplock_type = LEVEL_II_OPLOCK;
1141 } else {
1142 /* All share_mode_entries are placeholders or deferred.
1143 * Silently upgrade to fake levelII if the client didn't
1144 * ask for an oplock. */
1145 if (fsp->oplock_type == NO_OPLOCK) {
1146 /* Store a level2 oplock, but don't tell the client */
1147 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1152 * Don't grant level2 to clients that don't want them
1153 * or if we've turned them off.
1155 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1156 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1159 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1160 fsp->oplock_type, fsp_str_dbg(fsp)));
1163 bool request_timed_out(struct timeval request_time,
1164 struct timeval timeout)
1166 struct timeval now, end_time;
1167 GetTimeOfDay(&now);
1168 end_time = timeval_sum(&request_time, &timeout);
1169 return (timeval_compare(&end_time, &now) < 0);
1172 /****************************************************************************
1173 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1174 ****************************************************************************/
1176 static void defer_open(struct share_mode_lock *lck,
1177 struct timeval request_time,
1178 struct timeval timeout,
1179 struct smb_request *req,
1180 struct deferred_open_record *state)
1182 int i;
1184 /* Paranoia check */
1186 for (i=0; i<lck->num_share_modes; i++) {
1187 struct share_mode_entry *e = &lck->share_modes[i];
1189 if (!is_deferred_open_entry(e)) {
1190 continue;
1193 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
1194 DEBUG(0, ("Trying to defer an already deferred "
1195 "request: mid=%llu, exiting\n",
1196 (unsigned long long)req->mid));
1197 exit_server("attempt to defer a deferred request");
1201 /* End paranoia check */
1203 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1204 "open entry for mid %llu\n",
1205 (unsigned int)request_time.tv_sec,
1206 (unsigned int)request_time.tv_usec,
1207 (unsigned long long)req->mid));
1209 if (!push_deferred_open_message_smb(req, request_time, timeout,
1210 state->id, (char *)state, sizeof(*state))) {
1211 exit_server("push_deferred_open_message_smb failed");
1213 add_deferred_open(lck, req->mid, request_time,
1214 sconn_server_id(req->sconn), state->id);
1218 /****************************************************************************
1219 On overwrite open ensure that the attributes match.
1220 ****************************************************************************/
1222 bool open_match_attributes(connection_struct *conn,
1223 uint32 old_dos_attr,
1224 uint32 new_dos_attr,
1225 mode_t existing_unx_mode,
1226 mode_t new_unx_mode,
1227 mode_t *returned_unx_mode)
1229 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1231 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1232 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1234 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1235 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1236 *returned_unx_mode = new_unx_mode;
1237 } else {
1238 *returned_unx_mode = (mode_t)0;
1241 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1242 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1243 "returned_unx_mode = 0%o\n",
1244 (unsigned int)old_dos_attr,
1245 (unsigned int)existing_unx_mode,
1246 (unsigned int)new_dos_attr,
1247 (unsigned int)*returned_unx_mode ));
1249 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1250 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1251 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1252 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1253 return False;
1256 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1257 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1258 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1259 return False;
1262 return True;
1265 /****************************************************************************
1266 Special FCB or DOS processing in the case of a sharing violation.
1267 Try and find a duplicated file handle.
1268 ****************************************************************************/
1270 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1271 connection_struct *conn,
1272 files_struct *fsp_to_dup_into,
1273 const struct smb_filename *smb_fname,
1274 struct file_id id,
1275 uint16 file_pid,
1276 uint16 vuid,
1277 uint32 access_mask,
1278 uint32 share_access,
1279 uint32 create_options)
1281 files_struct *fsp;
1283 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1284 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1286 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1287 fsp = file_find_di_next(fsp)) {
1289 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1290 "vuid = %u, file_pid = %u, private_options = 0x%x "
1291 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1292 fsp->fh->fd, (unsigned int)fsp->vuid,
1293 (unsigned int)fsp->file_pid,
1294 (unsigned int)fsp->fh->private_options,
1295 (unsigned int)fsp->access_mask ));
1297 if (fsp->fh->fd != -1 &&
1298 fsp->vuid == vuid &&
1299 fsp->file_pid == file_pid &&
1300 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1301 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1302 (fsp->access_mask & FILE_WRITE_DATA) &&
1303 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1304 strequal(fsp->fsp_name->stream_name,
1305 smb_fname->stream_name)) {
1306 DEBUG(10,("fcb_or_dos_open: file match\n"));
1307 break;
1311 if (!fsp) {
1312 return NT_STATUS_NOT_FOUND;
1315 /* quite an insane set of semantics ... */
1316 if (is_executable(smb_fname->base_name) &&
1317 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1318 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1319 return NT_STATUS_INVALID_PARAMETER;
1322 /* We need to duplicate this fsp. */
1323 return dup_file_fsp(req, fsp, access_mask, share_access,
1324 create_options, fsp_to_dup_into);
1327 static void schedule_defer_open(struct share_mode_lock *lck,
1328 struct timeval request_time,
1329 struct smb_request *req)
1331 struct deferred_open_record state;
1333 /* This is a relative time, added to the absolute
1334 request_time value to get the absolute timeout time.
1335 Note that if this is the second or greater time we enter
1336 this codepath for this particular request mid then
1337 request_time is left as the absolute time of the *first*
1338 time this request mid was processed. This is what allows
1339 the request to eventually time out. */
1341 struct timeval timeout;
1343 /* Normally the smbd we asked should respond within
1344 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1345 * the client did, give twice the timeout as a safety
1346 * measure here in case the other smbd is stuck
1347 * somewhere else. */
1349 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1351 /* Nothing actually uses state.delayed_for_oplocks
1352 but it's handy to differentiate in debug messages
1353 between a 30 second delay due to oplock break, and
1354 a 1 second delay for share mode conflicts. */
1356 state.delayed_for_oplocks = True;
1357 state.id = lck->id;
1359 if (!request_timed_out(request_time, timeout)) {
1360 defer_open(lck, request_time, timeout, req, &state);
1364 /****************************************************************************
1365 Work out what access_mask to use from what the client sent us.
1366 ****************************************************************************/
1368 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1369 const struct smb_filename *smb_fname,
1370 bool file_existed,
1371 uint32_t access_mask,
1372 uint32_t *access_mask_out)
1374 NTSTATUS status;
1375 uint32_t orig_access_mask = access_mask;
1376 uint32_t rejected_share_access;
1379 * Convert GENERIC bits to specific bits.
1382 se_map_generic(&access_mask, &file_generic_mapping);
1384 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1385 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1386 if (file_existed) {
1388 struct security_descriptor *sd;
1389 uint32_t access_granted = 0;
1391 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1392 (SECINFO_OWNER |
1393 SECINFO_GROUP |
1394 SECINFO_DACL),&sd);
1396 if (!NT_STATUS_IS_OK(status)) {
1397 DEBUG(10,("smbd_calculate_access_mask: "
1398 "Could not get acl on file %s: %s\n",
1399 smb_fname_str_dbg(smb_fname),
1400 nt_errstr(status)));
1401 return NT_STATUS_ACCESS_DENIED;
1404 status = smb1_file_se_access_check(conn,
1406 get_current_nttok(conn),
1407 access_mask,
1408 &access_granted);
1410 TALLOC_FREE(sd);
1412 if (!NT_STATUS_IS_OK(status)) {
1413 DEBUG(10, ("smbd_calculate_access_mask: "
1414 "Access denied on file %s: "
1415 "when calculating maximum access\n",
1416 smb_fname_str_dbg(smb_fname)));
1417 return NT_STATUS_ACCESS_DENIED;
1420 if (!(access_granted & DELETE_ACCESS)) {
1421 if (can_delete_file_in_directory(conn, smb_fname)) {
1422 access_granted |= DELETE_ACCESS;
1426 access_mask = access_granted;
1427 } else {
1428 access_mask = FILE_GENERIC_ALL;
1431 access_mask &= conn->share_access;
1434 rejected_share_access = access_mask & ~(conn->share_access);
1436 if (rejected_share_access) {
1437 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1438 "file %s: rejected by share access mask[0x%08X] "
1439 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1440 smb_fname_str_dbg(smb_fname),
1441 conn->share_access,
1442 orig_access_mask, access_mask,
1443 rejected_share_access));
1444 return NT_STATUS_ACCESS_DENIED;
1447 *access_mask_out = access_mask;
1448 return NT_STATUS_OK;
1451 /****************************************************************************
1452 Remove the deferred open entry under lock.
1453 ****************************************************************************/
1455 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1456 struct server_id pid)
1458 struct share_mode_lock *lck = get_share_mode_lock(talloc_tos(), id,
1459 NULL, NULL, NULL);
1460 if (lck == NULL) {
1461 DEBUG(0, ("could not get share mode lock\n"));
1462 } else {
1463 del_deferred_open_entry(lck, mid, pid);
1464 TALLOC_FREE(lck);
1468 /****************************************************************
1469 Ensure we get the brlock lock followed by the share mode lock
1470 in the correct order to prevent deadlocks if other smbd's are
1471 using the brlock database on this file simultaneously with this open
1472 (that code also gets the locks in brlock -> share mode lock order).
1473 ****************************************************************/
1475 static bool acquire_ordered_locks(TALLOC_CTX *mem_ctx,
1476 files_struct *fsp,
1477 const struct file_id id,
1478 const char *connectpath,
1479 const struct smb_filename *smb_fname,
1480 const struct timespec *p_old_write_time,
1481 struct share_mode_lock **p_lck,
1482 struct byte_range_lock **p_br_lck)
1484 /* Ordering - we must get the br_lck for this
1485 file before the share mode. */
1486 if (lp_locking(fsp->conn->params)) {
1487 *p_br_lck = brl_get_locks_readonly(fsp);
1488 if (*p_br_lck == NULL) {
1489 DEBUG(0, ("Could not get br_lock\n"));
1490 return false;
1492 /* Note - we don't need to free the returned
1493 br_lck explicitly as it was allocated on talloc_tos()
1494 and so will be autofreed (and release the lock)
1495 once the frame context disappears.
1497 If it was set to fsp->brlock_rec then it was
1498 talloc_move'd to hang off the fsp pointer and
1499 in this case is guarenteed to not be holding the
1500 lock on the brlock database. */
1503 *p_lck = get_share_mode_lock(mem_ctx,
1505 connectpath,
1506 smb_fname,
1507 p_old_write_time);
1509 if (*p_lck == NULL) {
1510 DEBUG(0, ("Could not get share mode lock\n"));
1511 TALLOC_FREE(*p_br_lck);
1512 return false;
1514 return true;
1517 /****************************************************************************
1518 Open a file with a share mode. Passed in an already created files_struct *.
1519 ****************************************************************************/
1521 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1522 struct smb_request *req,
1523 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1524 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1525 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1526 uint32 create_options, /* options such as delete on close. */
1527 uint32 new_dos_attributes, /* attributes used for new file. */
1528 int oplock_request, /* internal Samba oplock codes. */
1529 /* Information (FILE_EXISTS etc.) */
1530 uint32_t private_flags, /* Samba specific flags. */
1531 int *pinfo,
1532 files_struct *fsp)
1534 struct smb_filename *smb_fname = fsp->fsp_name;
1535 int flags=0;
1536 int flags2=0;
1537 bool file_existed = VALID_STAT(smb_fname->st);
1538 bool def_acl = False;
1539 bool posix_open = False;
1540 bool new_file_created = False;
1541 bool clear_ads = false;
1542 struct file_id id;
1543 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1544 mode_t new_unx_mode = (mode_t)0;
1545 mode_t unx_mode = (mode_t)0;
1546 int info;
1547 uint32 existing_dos_attributes = 0;
1548 struct timeval request_time = timeval_zero();
1549 struct share_mode_lock *lck = NULL;
1550 uint32 open_access_mask = access_mask;
1551 NTSTATUS status;
1552 char *parent_dir;
1554 ZERO_STRUCT(id);
1556 if (conn->printer) {
1558 * Printers are handled completely differently.
1559 * Most of the passed parameters are ignored.
1562 if (pinfo) {
1563 *pinfo = FILE_WAS_CREATED;
1566 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1567 smb_fname_str_dbg(smb_fname)));
1569 if (!req) {
1570 DEBUG(0,("open_file_ntcreate: printer open without "
1571 "an SMB request!\n"));
1572 return NT_STATUS_INTERNAL_ERROR;
1575 return print_spool_open(fsp, smb_fname->base_name,
1576 req->vuid);
1579 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1580 NULL)) {
1581 return NT_STATUS_NO_MEMORY;
1584 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1585 posix_open = True;
1586 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1587 new_dos_attributes = 0;
1588 } else {
1589 /* Windows allows a new file to be created and
1590 silently removes a FILE_ATTRIBUTE_DIRECTORY
1591 sent by the client. Do the same. */
1593 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1595 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1596 * created new. */
1597 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1598 smb_fname, parent_dir);
1601 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1602 "access_mask=0x%x share_access=0x%x "
1603 "create_disposition = 0x%x create_options=0x%x "
1604 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1605 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1606 access_mask, share_access, create_disposition,
1607 create_options, (unsigned int)unx_mode, oplock_request,
1608 (unsigned int)private_flags));
1610 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1611 DEBUG(0, ("No smb request but not an internal only open!\n"));
1612 return NT_STATUS_INTERNAL_ERROR;
1616 * Only non-internal opens can be deferred at all
1619 if (req) {
1620 void *ptr;
1621 if (get_deferred_open_message_state(req,
1622 &request_time,
1623 &ptr)) {
1625 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1626 /* Remember the absolute time of the original
1627 request with this mid. We'll use it later to
1628 see if this has timed out. */
1630 /* Remove the deferred open entry under lock. */
1631 remove_deferred_open_entry(
1632 state->id, req->mid,
1633 sconn_server_id(req->sconn));
1635 /* Ensure we don't reprocess this message. */
1636 remove_deferred_open_message_smb(req->mid);
1640 if (!posix_open) {
1641 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1642 if (file_existed) {
1643 existing_dos_attributes = dos_mode(conn, smb_fname);
1647 /* ignore any oplock requests if oplocks are disabled */
1648 if (!lp_oplocks(SNUM(conn)) ||
1649 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1650 /* Mask off everything except the private Samba bits. */
1651 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1654 /* this is for OS/2 long file names - say we don't support them */
1655 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1656 /* OS/2 Workplace shell fix may be main code stream in a later
1657 * release. */
1658 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1659 "supported.\n"));
1660 if (use_nt_status()) {
1661 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1663 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1666 switch( create_disposition ) {
1668 * Currently we're using FILE_SUPERSEDE as the same as
1669 * FILE_OVERWRITE_IF but they really are
1670 * different. FILE_SUPERSEDE deletes an existing file
1671 * (requiring delete access) then recreates it.
1673 case FILE_SUPERSEDE:
1674 /* If file exists replace/overwrite. If file doesn't
1675 * exist create. */
1676 flags2 |= (O_CREAT | O_TRUNC);
1677 clear_ads = true;
1678 break;
1680 case FILE_OVERWRITE_IF:
1681 /* If file exists replace/overwrite. If file doesn't
1682 * exist create. */
1683 flags2 |= (O_CREAT | O_TRUNC);
1684 clear_ads = true;
1685 break;
1687 case FILE_OPEN:
1688 /* If file exists open. If file doesn't exist error. */
1689 if (!file_existed) {
1690 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1691 "requested for file %s and file "
1692 "doesn't exist.\n",
1693 smb_fname_str_dbg(smb_fname)));
1694 errno = ENOENT;
1695 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1697 break;
1699 case FILE_OVERWRITE:
1700 /* If file exists overwrite. If file doesn't exist
1701 * error. */
1702 if (!file_existed) {
1703 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1704 "requested for file %s and file "
1705 "doesn't exist.\n",
1706 smb_fname_str_dbg(smb_fname) ));
1707 errno = ENOENT;
1708 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1710 flags2 |= O_TRUNC;
1711 clear_ads = true;
1712 break;
1714 case FILE_CREATE:
1715 /* If file exists error. If file doesn't exist
1716 * create. */
1717 if (file_existed) {
1718 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1719 "requested for file %s and file "
1720 "already exists.\n",
1721 smb_fname_str_dbg(smb_fname)));
1722 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1723 errno = EISDIR;
1724 } else {
1725 errno = EEXIST;
1727 return map_nt_error_from_unix(errno);
1729 flags2 |= (O_CREAT|O_EXCL);
1730 break;
1732 case FILE_OPEN_IF:
1733 /* If file exists open. If file doesn't exist
1734 * create. */
1735 flags2 |= O_CREAT;
1736 break;
1738 default:
1739 return NT_STATUS_INVALID_PARAMETER;
1742 /* We only care about matching attributes on file exists and
1743 * overwrite. */
1745 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1746 (create_disposition == FILE_OVERWRITE_IF))) {
1747 if (!open_match_attributes(conn, existing_dos_attributes,
1748 new_dos_attributes,
1749 smb_fname->st.st_ex_mode,
1750 unx_mode, &new_unx_mode)) {
1751 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1752 "for file %s (%x %x) (0%o, 0%o)\n",
1753 smb_fname_str_dbg(smb_fname),
1754 existing_dos_attributes,
1755 new_dos_attributes,
1756 (unsigned int)smb_fname->st.st_ex_mode,
1757 (unsigned int)unx_mode ));
1758 errno = EACCES;
1759 return NT_STATUS_ACCESS_DENIED;
1763 status = smbd_calculate_access_mask(conn, smb_fname, file_existed,
1764 access_mask,
1765 &access_mask);
1766 if (!NT_STATUS_IS_OK(status)) {
1767 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1768 "on file %s returned %s\n",
1769 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1770 return status;
1773 open_access_mask = access_mask;
1775 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1776 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1779 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1780 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1781 access_mask));
1784 * Note that we ignore the append flag as append does not
1785 * mean the same thing under DOS and Unix.
1788 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1789 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1790 /* DENY_DOS opens are always underlying read-write on the
1791 file handle, no matter what the requested access mask
1792 says. */
1793 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1794 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1795 flags = O_RDWR;
1796 } else {
1797 flags = O_WRONLY;
1799 } else {
1800 flags = O_RDONLY;
1804 * Currently we only look at FILE_WRITE_THROUGH for create options.
1807 #if defined(O_SYNC)
1808 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1809 flags2 |= O_SYNC;
1811 #endif /* O_SYNC */
1813 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1814 flags2 |= O_APPEND;
1817 if (!posix_open && !CAN_WRITE(conn)) {
1819 * We should really return a permission denied error if either
1820 * O_CREAT or O_TRUNC are set, but for compatibility with
1821 * older versions of Samba we just AND them out.
1823 flags2 &= ~(O_CREAT|O_TRUNC);
1827 * Ensure we can't write on a read-only share or file.
1830 if (flags != O_RDONLY && file_existed &&
1831 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1832 DEBUG(5,("open_file_ntcreate: write access requested for "
1833 "file %s on read only %s\n",
1834 smb_fname_str_dbg(smb_fname),
1835 !CAN_WRITE(conn) ? "share" : "file" ));
1836 errno = EACCES;
1837 return NT_STATUS_ACCESS_DENIED;
1840 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1841 fsp->share_access = share_access;
1842 fsp->fh->private_options = private_flags;
1843 fsp->access_mask = open_access_mask; /* We change this to the
1844 * requested access_mask after
1845 * the open is done. */
1846 fsp->posix_open = posix_open;
1848 /* Ensure no SAMBA_PRIVATE bits can be set. */
1849 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1851 if (timeval_is_zero(&request_time)) {
1852 request_time = fsp->open_time;
1855 if (file_existed) {
1856 struct byte_range_lock *br_lck = NULL;
1857 struct share_mode_entry *batch_entry = NULL;
1858 struct share_mode_entry *exclusive_entry = NULL;
1859 bool got_level2_oplock = false;
1860 bool got_a_none_oplock = false;
1862 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1863 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1865 if (!acquire_ordered_locks(talloc_tos(),
1866 fsp,
1868 conn->connectpath,
1869 smb_fname,
1870 &old_write_time,
1871 &lck,
1872 &br_lck)) {
1873 return NT_STATUS_SHARING_VIOLATION;
1876 /* Get the types we need to examine. */
1877 find_oplock_types(fsp,
1878 oplock_request,
1879 lck,
1880 &batch_entry,
1881 &exclusive_entry,
1882 &got_level2_oplock,
1883 &got_a_none_oplock);
1885 /* First pass - send break only on batch oplocks. */
1886 if ((req != NULL) &&
1887 delay_for_batch_oplocks(fsp,
1888 req->mid,
1889 oplock_request,
1890 batch_entry)) {
1891 schedule_defer_open(lck, request_time, req);
1892 TALLOC_FREE(lck);
1893 return NT_STATUS_SHARING_VIOLATION;
1896 /* Use the client requested access mask here, not the one we
1897 * open with. */
1898 status = open_mode_check(conn, lck, fsp->name_hash,
1899 access_mask, share_access,
1900 create_options, &file_existed);
1902 if (NT_STATUS_IS_OK(status)) {
1903 /* We might be going to allow this open. Check oplock
1904 * status again. */
1905 /* Second pass - send break for both batch or
1906 * exclusive oplocks. */
1907 if ((req != NULL) &&
1908 delay_for_exclusive_oplocks(
1909 fsp,
1910 req->mid,
1911 oplock_request,
1912 exclusive_entry)) {
1913 schedule_defer_open(lck, request_time, req);
1914 TALLOC_FREE(lck);
1915 return NT_STATUS_SHARING_VIOLATION;
1919 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1920 /* DELETE_PENDING is not deferred for a second */
1921 TALLOC_FREE(lck);
1922 return status;
1925 grant_fsp_oplock_type(fsp,
1926 br_lck,
1927 oplock_request,
1928 got_level2_oplock,
1929 got_a_none_oplock);
1931 if (!NT_STATUS_IS_OK(status)) {
1932 uint32 can_access_mask;
1933 bool can_access = True;
1935 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1937 /* Check if this can be done with the deny_dos and fcb
1938 * calls. */
1939 if (private_flags &
1940 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1941 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1942 if (req == NULL) {
1943 DEBUG(0, ("DOS open without an SMB "
1944 "request!\n"));
1945 TALLOC_FREE(lck);
1946 return NT_STATUS_INTERNAL_ERROR;
1949 /* Use the client requested access mask here,
1950 * not the one we open with. */
1951 status = fcb_or_dos_open(req,
1952 conn,
1953 fsp,
1954 smb_fname,
1956 req->smbpid,
1957 req->vuid,
1958 access_mask,
1959 share_access,
1960 create_options);
1962 if (NT_STATUS_IS_OK(status)) {
1963 TALLOC_FREE(lck);
1964 if (pinfo) {
1965 *pinfo = FILE_WAS_OPENED;
1967 return NT_STATUS_OK;
1972 * This next line is a subtlety we need for
1973 * MS-Access. If a file open will fail due to share
1974 * permissions and also for security (access) reasons,
1975 * we need to return the access failed error, not the
1976 * share error. We can't open the file due to kernel
1977 * oplock deadlock (it's possible we failed above on
1978 * the open_mode_check()) so use a userspace check.
1981 if (flags & O_RDWR) {
1982 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1983 } else if (flags & O_WRONLY) {
1984 can_access_mask = FILE_WRITE_DATA;
1985 } else {
1986 can_access_mask = FILE_READ_DATA;
1989 if (((can_access_mask & FILE_WRITE_DATA) &&
1990 !CAN_WRITE(conn)) ||
1991 !can_access_file_data(conn, smb_fname,
1992 can_access_mask)) {
1993 can_access = False;
1997 * If we're returning a share violation, ensure we
1998 * cope with the braindead 1 second delay.
2001 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2002 lp_defer_sharing_violations()) {
2003 struct timeval timeout;
2004 struct deferred_open_record state;
2005 int timeout_usecs;
2007 /* this is a hack to speed up torture tests
2008 in 'make test' */
2009 timeout_usecs = lp_parm_int(SNUM(conn),
2010 "smbd","sharedelay",
2011 SHARING_VIOLATION_USEC_WAIT);
2013 /* This is a relative time, added to the absolute
2014 request_time value to get the absolute timeout time.
2015 Note that if this is the second or greater time we enter
2016 this codepath for this particular request mid then
2017 request_time is left as the absolute time of the *first*
2018 time this request mid was processed. This is what allows
2019 the request to eventually time out. */
2021 timeout = timeval_set(0, timeout_usecs);
2023 /* Nothing actually uses state.delayed_for_oplocks
2024 but it's handy to differentiate in debug messages
2025 between a 30 second delay due to oplock break, and
2026 a 1 second delay for share mode conflicts. */
2028 state.delayed_for_oplocks = False;
2029 state.id = id;
2031 if ((req != NULL)
2032 && !request_timed_out(request_time,
2033 timeout)) {
2034 defer_open(lck, request_time, timeout,
2035 req, &state);
2039 TALLOC_FREE(lck);
2040 if (can_access) {
2042 * We have detected a sharing violation here
2043 * so return the correct error code
2045 status = NT_STATUS_SHARING_VIOLATION;
2046 } else {
2047 status = NT_STATUS_ACCESS_DENIED;
2049 return status;
2053 * We exit this block with the share entry *locked*.....
2057 SMB_ASSERT(!file_existed || (lck != NULL));
2060 * Ensure we pay attention to default ACLs on directories if required.
2063 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2064 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2065 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2068 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2069 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2070 (unsigned int)flags, (unsigned int)flags2,
2071 (unsigned int)unx_mode, (unsigned int)access_mask,
2072 (unsigned int)open_access_mask));
2075 * open_file strips any O_TRUNC flags itself.
2078 fsp_open = open_file(fsp, conn, req, parent_dir,
2079 flags|flags2, unx_mode, access_mask,
2080 open_access_mask);
2082 if (!NT_STATUS_IS_OK(fsp_open)) {
2083 if (lck != NULL) {
2084 TALLOC_FREE(lck);
2086 return fsp_open;
2089 if (!file_existed) {
2090 struct byte_range_lock *br_lck = NULL;
2091 struct share_mode_entry *batch_entry = NULL;
2092 struct share_mode_entry *exclusive_entry = NULL;
2093 bool got_level2_oplock = false;
2094 bool got_a_none_oplock = false;
2095 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2097 * Deal with the race condition where two smbd's detect the
2098 * file doesn't exist and do the create at the same time. One
2099 * of them will win and set a share mode, the other (ie. this
2100 * one) should check if the requested share mode for this
2101 * create is allowed.
2105 * Now the file exists and fsp is successfully opened,
2106 * fsp->dev and fsp->inode are valid and should replace the
2107 * dev=0,inode=0 from a non existent file. Spotted by
2108 * Nadav Danieli <nadavd@exanet.com>. JRA.
2111 id = fsp->file_id;
2113 if (!acquire_ordered_locks(talloc_tos(),
2114 fsp,
2116 conn->connectpath,
2117 smb_fname,
2118 &old_write_time,
2119 &lck,
2120 &br_lck)) {
2121 return NT_STATUS_SHARING_VIOLATION;
2124 /* Get the types we need to examine. */
2125 find_oplock_types(fsp,
2126 oplock_request,
2127 lck,
2128 &batch_entry,
2129 &exclusive_entry,
2130 &got_level2_oplock,
2131 &got_a_none_oplock);
2133 /* First pass - send break only on batch oplocks. */
2134 if ((req != NULL) &&
2135 delay_for_batch_oplocks(fsp,
2136 req->mid,
2137 oplock_request,
2138 batch_entry)) {
2139 schedule_defer_open(lck, request_time, req);
2140 TALLOC_FREE(lck);
2141 fd_close(fsp);
2142 return NT_STATUS_SHARING_VIOLATION;
2145 status = open_mode_check(conn, lck, fsp->name_hash,
2146 access_mask, share_access,
2147 create_options, &file_existed);
2149 if (NT_STATUS_IS_OK(status)) {
2150 /* We might be going to allow this open. Check oplock
2151 * status again. */
2152 /* Second pass - send break for both batch or
2153 * exclusive oplocks. */
2154 if ((req != NULL) &&
2155 delay_for_exclusive_oplocks(
2156 fsp,
2157 req->mid,
2158 oplock_request,
2159 exclusive_entry)) {
2160 schedule_defer_open(lck, request_time, req);
2161 TALLOC_FREE(lck);
2162 fd_close(fsp);
2163 return NT_STATUS_SHARING_VIOLATION;
2167 if (!NT_STATUS_IS_OK(status)) {
2168 struct deferred_open_record state;
2170 fd_close(fsp);
2172 state.delayed_for_oplocks = False;
2173 state.id = id;
2175 /* Do it all over again immediately. In the second
2176 * round we will find that the file existed and handle
2177 * the DELETE_PENDING and FCB cases correctly. No need
2178 * to duplicate the code here. Essentially this is a
2179 * "goto top of this function", but don't tell
2180 * anybody... */
2182 if (req != NULL) {
2183 defer_open(lck, request_time, timeval_zero(),
2184 req, &state);
2186 TALLOC_FREE(lck);
2187 return status;
2190 grant_fsp_oplock_type(fsp,
2191 br_lck,
2192 oplock_request,
2193 got_level2_oplock,
2194 got_a_none_oplock);
2197 * We exit this block with the share entry *locked*.....
2202 SMB_ASSERT(lck != NULL);
2204 /* Delete streams if create_disposition requires it */
2205 if (file_existed && clear_ads &&
2206 !is_ntfs_stream_smb_fname(smb_fname)) {
2207 status = delete_all_streams(conn, smb_fname->base_name);
2208 if (!NT_STATUS_IS_OK(status)) {
2209 TALLOC_FREE(lck);
2210 fd_close(fsp);
2211 return status;
2215 /* note that we ignore failure for the following. It is
2216 basically a hack for NFS, and NFS will never set one of
2217 these only read them. Nobody but Samba can ever set a deny
2218 mode and we have already checked our more authoritative
2219 locking database for permission to set this deny mode. If
2220 the kernel refuses the operations then the kernel is wrong.
2221 note that GPFS supports it as well - jmcd */
2223 if (fsp->fh->fd != -1) {
2224 int ret_flock;
2225 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2226 if(ret_flock == -1 ){
2228 TALLOC_FREE(lck);
2229 fd_close(fsp);
2231 return NT_STATUS_SHARING_VIOLATION;
2236 * At this point onwards, we can guarentee that the share entry
2237 * is locked, whether we created the file or not, and that the
2238 * deny mode is compatible with all current opens.
2242 * If requested, truncate the file.
2245 if (file_existed && (flags2&O_TRUNC)) {
2247 * We are modifing the file after open - update the stat
2248 * struct..
2250 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2251 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2252 status = map_nt_error_from_unix(errno);
2253 TALLOC_FREE(lck);
2254 fd_close(fsp);
2255 return status;
2260 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2261 * but we don't have to store this - just ignore it on access check.
2263 if (conn->sconn->using_smb2) {
2265 * SMB2 doesn't return it (according to Microsoft tests).
2266 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2267 * File created with access = 0x7 (Read, Write, Delete)
2268 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2270 fsp->access_mask = access_mask;
2271 } else {
2272 /* But SMB1 does. */
2273 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2276 if (file_existed) {
2277 /* stat opens on existing files don't get oplocks. */
2278 if (is_stat_open(open_access_mask)) {
2279 fsp->oplock_type = NO_OPLOCK;
2282 if (!(flags2 & O_TRUNC)) {
2283 info = FILE_WAS_OPENED;
2284 } else {
2285 info = FILE_WAS_OVERWRITTEN;
2287 } else {
2288 info = FILE_WAS_CREATED;
2291 if (pinfo) {
2292 *pinfo = info;
2296 * Setup the oplock info in both the shared memory and
2297 * file structs.
2300 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2302 * Could not get the kernel oplock or there are byte-range
2303 * locks on the file.
2305 fsp->oplock_type = NO_OPLOCK;
2308 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
2309 new_file_created = True;
2312 set_share_mode(lck, fsp, get_current_uid(conn),
2313 req ? req->mid : 0,
2314 fsp->oplock_type);
2316 /* Handle strange delete on close create semantics. */
2317 if (create_options & FILE_DELETE_ON_CLOSE) {
2319 status = can_set_delete_on_close(fsp, new_dos_attributes);
2321 if (!NT_STATUS_IS_OK(status)) {
2322 /* Remember to delete the mode we just added. */
2323 del_share_mode(lck, fsp);
2324 TALLOC_FREE(lck);
2325 fd_close(fsp);
2326 return status;
2328 /* Note that here we set the *inital* delete on close flag,
2329 not the regular one. The magic gets handled in close. */
2330 fsp->initial_delete_on_close = True;
2333 if (new_file_created) {
2334 /* Files should be initially set as archive */
2335 if (lp_map_archive(SNUM(conn)) ||
2336 lp_store_dos_attributes(SNUM(conn))) {
2337 if (!posix_open) {
2338 if (file_set_dosmode(conn, smb_fname,
2339 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2340 parent_dir, true) == 0) {
2341 unx_mode = smb_fname->st.st_ex_mode;
2347 /* Determine sparse flag. */
2348 if (posix_open) {
2349 /* POSIX opens are sparse by default. */
2350 fsp->is_sparse = true;
2351 } else {
2352 fsp->is_sparse = (file_existed &&
2353 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2357 * Take care of inherited ACLs on created files - if default ACL not
2358 * selected.
2361 if (!posix_open && !file_existed && !def_acl) {
2363 int saved_errno = errno; /* We might get ENOSYS in the next
2364 * call.. */
2366 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2367 errno == ENOSYS) {
2368 errno = saved_errno; /* Ignore ENOSYS */
2371 } else if (new_unx_mode) {
2373 int ret = -1;
2375 /* Attributes need changing. File already existed. */
2378 int saved_errno = errno; /* We might get ENOSYS in the
2379 * next call.. */
2380 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2382 if (ret == -1 && errno == ENOSYS) {
2383 errno = saved_errno; /* Ignore ENOSYS */
2384 } else {
2385 DEBUG(5, ("open_file_ntcreate: reset "
2386 "attributes of file %s to 0%o\n",
2387 smb_fname_str_dbg(smb_fname),
2388 (unsigned int)new_unx_mode));
2389 ret = 0; /* Don't do the fchmod below. */
2393 if ((ret == -1) &&
2394 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2395 DEBUG(5, ("open_file_ntcreate: failed to reset "
2396 "attributes of file %s to 0%o\n",
2397 smb_fname_str_dbg(smb_fname),
2398 (unsigned int)new_unx_mode));
2401 /* If this is a successful open, we must remove any deferred open
2402 * records. */
2403 if (req != NULL) {
2404 del_deferred_open_entry(lck, req->mid,
2405 sconn_server_id(req->sconn));
2407 TALLOC_FREE(lck);
2409 return NT_STATUS_OK;
2413 /****************************************************************************
2414 Open a file for for write to ensure that we can fchmod it.
2415 ****************************************************************************/
2417 NTSTATUS open_file_fchmod(connection_struct *conn,
2418 struct smb_filename *smb_fname,
2419 files_struct **result)
2421 if (!VALID_STAT(smb_fname->st)) {
2422 return NT_STATUS_INVALID_PARAMETER;
2425 return SMB_VFS_CREATE_FILE(
2426 conn, /* conn */
2427 NULL, /* req */
2428 0, /* root_dir_fid */
2429 smb_fname, /* fname */
2430 FILE_WRITE_DATA, /* access_mask */
2431 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2432 FILE_SHARE_DELETE),
2433 FILE_OPEN, /* create_disposition*/
2434 0, /* create_options */
2435 0, /* file_attributes */
2436 INTERNAL_OPEN_ONLY, /* oplock_request */
2437 0, /* allocation_size */
2438 0, /* private_flags */
2439 NULL, /* sd */
2440 NULL, /* ea_list */
2441 result, /* result */
2442 NULL); /* pinfo */
2445 static NTSTATUS mkdir_internal(connection_struct *conn,
2446 struct smb_filename *smb_dname,
2447 uint32 file_attributes)
2449 mode_t mode;
2450 char *parent_dir;
2451 NTSTATUS status;
2452 bool posix_open = false;
2453 bool need_re_stat = false;
2455 if(!CAN_WRITE(conn)) {
2456 DEBUG(5,("mkdir_internal: failing create on read-only share "
2457 "%s\n", lp_servicename(SNUM(conn))));
2458 return NT_STATUS_ACCESS_DENIED;
2461 status = check_name(conn, smb_dname->base_name);
2462 if (!NT_STATUS_IS_OK(status)) {
2463 return status;
2466 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2467 NULL)) {
2468 return NT_STATUS_NO_MEMORY;
2471 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2472 posix_open = true;
2473 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2474 } else {
2475 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2478 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2479 return map_nt_error_from_unix(errno);
2482 /* Ensure we're checking for a symlink here.... */
2483 /* We don't want to get caught by a symlink racer. */
2485 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2486 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2487 smb_fname_str_dbg(smb_dname), strerror(errno)));
2488 return map_nt_error_from_unix(errno);
2491 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2492 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2493 smb_fname_str_dbg(smb_dname)));
2494 return NT_STATUS_ACCESS_DENIED;
2497 if (lp_store_dos_attributes(SNUM(conn))) {
2498 if (!posix_open) {
2499 file_set_dosmode(conn, smb_dname,
2500 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2501 parent_dir, true);
2505 if (lp_inherit_perms(SNUM(conn))) {
2506 inherit_access_posix_acl(conn, parent_dir,
2507 smb_dname->base_name, mode);
2508 need_re_stat = true;
2511 if (!posix_open) {
2513 * Check if high bits should have been set,
2514 * then (if bits are missing): add them.
2515 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2516 * dir.
2518 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2519 (mode & ~smb_dname->st.st_ex_mode)) {
2520 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2521 (smb_dname->st.st_ex_mode |
2522 (mode & ~smb_dname->st.st_ex_mode)));
2523 need_re_stat = true;
2527 /* Change the owner if required. */
2528 if (lp_inherit_owner(SNUM(conn))) {
2529 change_dir_owner_to_parent(conn, parent_dir,
2530 smb_dname->base_name,
2531 &smb_dname->st);
2532 need_re_stat = true;
2535 if (need_re_stat) {
2536 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2537 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2538 smb_fname_str_dbg(smb_dname), strerror(errno)));
2539 return map_nt_error_from_unix(errno);
2543 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2544 smb_dname->base_name);
2546 return NT_STATUS_OK;
2549 /****************************************************************************
2550 Ensure we didn't get symlink raced on opening a directory.
2551 ****************************************************************************/
2553 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2554 const SMB_STRUCT_STAT *sbuf2)
2556 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2557 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2558 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2559 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2560 return false;
2562 return true;
2565 /****************************************************************************
2566 Open a directory from an NT SMB call.
2567 ****************************************************************************/
2569 static NTSTATUS open_directory(connection_struct *conn,
2570 struct smb_request *req,
2571 struct smb_filename *smb_dname,
2572 uint32 access_mask,
2573 uint32 share_access,
2574 uint32 create_disposition,
2575 uint32 create_options,
2576 uint32 file_attributes,
2577 int *pinfo,
2578 files_struct **result)
2580 files_struct *fsp = NULL;
2581 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2582 struct share_mode_lock *lck = NULL;
2583 NTSTATUS status;
2584 struct timespec mtimespec;
2585 int info = 0;
2587 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
2589 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2590 /* Ensure we have a directory attribute. */
2591 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2594 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2595 "share_access = 0x%x create_options = 0x%x, "
2596 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2597 smb_fname_str_dbg(smb_dname),
2598 (unsigned int)access_mask,
2599 (unsigned int)share_access,
2600 (unsigned int)create_options,
2601 (unsigned int)create_disposition,
2602 (unsigned int)file_attributes));
2604 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2605 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2606 is_ntfs_stream_smb_fname(smb_dname)) {
2607 DEBUG(2, ("open_directory: %s is a stream name!\n",
2608 smb_fname_str_dbg(smb_dname)));
2609 return NT_STATUS_NOT_A_DIRECTORY;
2612 status = smbd_calculate_access_mask(conn, smb_dname, dir_existed,
2613 access_mask, &access_mask);
2614 if (!NT_STATUS_IS_OK(status)) {
2615 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2616 "on file %s returned %s\n",
2617 smb_fname_str_dbg(smb_dname),
2618 nt_errstr(status)));
2619 return status;
2622 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2623 !security_token_has_privilege(get_current_nttok(conn),
2624 SEC_PRIV_SECURITY)) {
2625 DEBUG(10, ("open_directory: open on %s "
2626 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2627 smb_fname_str_dbg(smb_dname)));
2628 return NT_STATUS_PRIVILEGE_NOT_HELD;
2631 switch( create_disposition ) {
2632 case FILE_OPEN:
2634 if (!dir_existed) {
2635 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2638 info = FILE_WAS_OPENED;
2639 break;
2641 case FILE_CREATE:
2643 /* If directory exists error. If directory doesn't
2644 * exist create. */
2646 status = mkdir_internal(conn, smb_dname,
2647 file_attributes);
2649 if (!NT_STATUS_IS_OK(status)) {
2650 DEBUG(2, ("open_directory: unable to create "
2651 "%s. Error was %s\n",
2652 smb_fname_str_dbg(smb_dname),
2653 nt_errstr(status)));
2654 return status;
2657 info = FILE_WAS_CREATED;
2658 break;
2660 case FILE_OPEN_IF:
2662 * If directory exists open. If directory doesn't
2663 * exist create.
2666 status = mkdir_internal(conn, smb_dname,
2667 file_attributes);
2669 if (NT_STATUS_IS_OK(status)) {
2670 info = FILE_WAS_CREATED;
2673 if (NT_STATUS_EQUAL(status,
2674 NT_STATUS_OBJECT_NAME_COLLISION)) {
2675 info = FILE_WAS_OPENED;
2676 status = NT_STATUS_OK;
2678 break;
2680 case FILE_SUPERSEDE:
2681 case FILE_OVERWRITE:
2682 case FILE_OVERWRITE_IF:
2683 default:
2684 DEBUG(5,("open_directory: invalid create_disposition "
2685 "0x%x for directory %s\n",
2686 (unsigned int)create_disposition,
2687 smb_fname_str_dbg(smb_dname)));
2688 return NT_STATUS_INVALID_PARAMETER;
2691 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2692 DEBUG(5,("open_directory: %s is not a directory !\n",
2693 smb_fname_str_dbg(smb_dname)));
2694 return NT_STATUS_NOT_A_DIRECTORY;
2697 if (info == FILE_WAS_OPENED) {
2698 uint32_t access_granted = 0;
2699 status = smbd_check_open_rights(conn, smb_dname, access_mask,
2700 &access_granted);
2702 /* Were we trying to do a directory open
2703 * for delete and didn't get DELETE
2704 * access (only) ? Check if the
2705 * directory allows DELETE_CHILD.
2706 * See here:
2707 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
2708 * for details. */
2710 if ((NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
2711 (access_mask & DELETE_ACCESS) &&
2712 (access_granted == DELETE_ACCESS) &&
2713 can_delete_file_in_directory(conn, smb_dname))) {
2714 DEBUG(10,("open_directory: overrode ACCESS_DENIED "
2715 "on directory %s\n",
2716 smb_fname_str_dbg(smb_dname)));
2717 status = NT_STATUS_OK;
2720 if (!NT_STATUS_IS_OK(status)) {
2721 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2722 "file %s failed with %s\n",
2723 smb_fname_str_dbg(smb_dname),
2724 nt_errstr(status)));
2725 return status;
2729 status = file_new(req, conn, &fsp);
2730 if(!NT_STATUS_IS_OK(status)) {
2731 return status;
2735 * Setup the files_struct for it.
2738 fsp->mode = smb_dname->st.st_ex_mode;
2739 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2740 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2741 fsp->file_pid = req ? req->smbpid : 0;
2742 fsp->can_lock = False;
2743 fsp->can_read = False;
2744 fsp->can_write = False;
2746 fsp->share_access = share_access;
2747 fsp->fh->private_options = 0;
2748 fsp->print_file = NULL;
2749 fsp->modified = False;
2750 fsp->oplock_type = NO_OPLOCK;
2751 fsp->sent_oplock_break = NO_BREAK_SENT;
2752 fsp->is_directory = True;
2753 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2754 status = fsp_set_smb_fname(fsp, smb_dname);
2755 if (!NT_STATUS_IS_OK(status)) {
2756 file_free(req, fsp);
2757 return status;
2760 mtimespec = smb_dname->st.st_ex_mtime;
2762 fsp->access_mask = access_mask;
2764 #ifdef O_DIRECTORY
2765 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2766 #else
2767 /* POSIX allows us to open a directory with O_RDONLY. */
2768 status = fd_open(conn, fsp, O_RDONLY, 0);
2769 #endif
2770 if (!NT_STATUS_IS_OK(status)) {
2771 DEBUG(5, ("open_directory: Could not open fd for "
2772 "%s (%s)\n",
2773 smb_fname_str_dbg(smb_dname),
2774 nt_errstr(status)));
2775 file_free(req, fsp);
2776 return status;
2780 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2781 * Set the real access mask for later access (possibly delete).
2783 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2785 status = vfs_stat_fsp(fsp);
2786 if (!NT_STATUS_IS_OK(status)) {
2787 fd_close(fsp);
2788 file_free(req, fsp);
2789 return status;
2792 /* Ensure there was no race condition. */
2793 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2794 DEBUG(5,("open_directory: stat struct differs for "
2795 "directory %s.\n",
2796 smb_fname_str_dbg(smb_dname)));
2797 fd_close(fsp);
2798 file_free(req, fsp);
2799 return NT_STATUS_ACCESS_DENIED;
2802 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2803 conn->connectpath, smb_dname, &mtimespec);
2805 if (lck == NULL) {
2806 DEBUG(0, ("open_directory: Could not get share mode lock for "
2807 "%s\n", smb_fname_str_dbg(smb_dname)));
2808 fd_close(fsp);
2809 file_free(req, fsp);
2810 return NT_STATUS_SHARING_VIOLATION;
2813 status = open_mode_check(conn, lck, fsp->name_hash,
2814 access_mask, share_access,
2815 create_options, &dir_existed);
2817 if (!NT_STATUS_IS_OK(status)) {
2818 TALLOC_FREE(lck);
2819 fd_close(fsp);
2820 file_free(req, fsp);
2821 return status;
2824 set_share_mode(lck, fsp, get_current_uid(conn),
2825 req ? req->mid : 0, NO_OPLOCK);
2827 /* For directories the delete on close bit at open time seems
2828 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2829 if (create_options & FILE_DELETE_ON_CLOSE) {
2830 status = can_set_delete_on_close(fsp, 0);
2831 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2832 TALLOC_FREE(lck);
2833 fd_close(fsp);
2834 file_free(req, fsp);
2835 return status;
2838 if (NT_STATUS_IS_OK(status)) {
2839 /* Note that here we set the *inital* delete on close flag,
2840 not the regular one. The magic gets handled in close. */
2841 fsp->initial_delete_on_close = True;
2845 TALLOC_FREE(lck);
2847 if (pinfo) {
2848 *pinfo = info;
2851 *result = fsp;
2852 return NT_STATUS_OK;
2855 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2856 struct smb_filename *smb_dname)
2858 NTSTATUS status;
2859 files_struct *fsp;
2861 status = SMB_VFS_CREATE_FILE(
2862 conn, /* conn */
2863 req, /* req */
2864 0, /* root_dir_fid */
2865 smb_dname, /* fname */
2866 FILE_READ_ATTRIBUTES, /* access_mask */
2867 FILE_SHARE_NONE, /* share_access */
2868 FILE_CREATE, /* create_disposition*/
2869 FILE_DIRECTORY_FILE, /* create_options */
2870 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2871 0, /* oplock_request */
2872 0, /* allocation_size */
2873 0, /* private_flags */
2874 NULL, /* sd */
2875 NULL, /* ea_list */
2876 &fsp, /* result */
2877 NULL); /* pinfo */
2879 if (NT_STATUS_IS_OK(status)) {
2880 close_file(req, fsp, NORMAL_CLOSE);
2883 return status;
2886 /****************************************************************************
2887 Receive notification that one of our open files has been renamed by another
2888 smbd process.
2889 ****************************************************************************/
2891 void msg_file_was_renamed(struct messaging_context *msg,
2892 void *private_data,
2893 uint32_t msg_type,
2894 struct server_id server_id,
2895 DATA_BLOB *data)
2897 struct smbd_server_connection *sconn;
2898 files_struct *fsp;
2899 char *frm = (char *)data->data;
2900 struct file_id id;
2901 const char *sharepath;
2902 const char *base_name;
2903 const char *stream_name;
2904 struct smb_filename *smb_fname = NULL;
2905 size_t sp_len, bn_len;
2906 NTSTATUS status;
2908 sconn = msg_ctx_to_sconn(msg);
2909 if (sconn == NULL) {
2910 DEBUG(1, ("could not find sconn\n"));
2911 return;
2914 if (data->data == NULL
2915 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2916 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2917 (int)data->length));
2918 return;
2921 /* Unpack the message. */
2922 pull_file_id_24(frm, &id);
2923 sharepath = &frm[24];
2924 sp_len = strlen(sharepath);
2925 base_name = sharepath + sp_len + 1;
2926 bn_len = strlen(base_name);
2927 stream_name = sharepath + sp_len + 1 + bn_len + 1;
2929 /* stream_name must always be NULL if there is no stream. */
2930 if (stream_name[0] == '\0') {
2931 stream_name = NULL;
2934 status = create_synthetic_smb_fname(talloc_tos(), base_name,
2935 stream_name, NULL, &smb_fname);
2936 if (!NT_STATUS_IS_OK(status)) {
2937 return;
2940 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2941 "file_id %s\n",
2942 sharepath, smb_fname_str_dbg(smb_fname),
2943 file_id_string_tos(&id)));
2945 for(fsp = file_find_di_first(sconn, id); fsp;
2946 fsp = file_find_di_next(fsp)) {
2947 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2949 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2950 fsp->fnum, fsp_str_dbg(fsp),
2951 smb_fname_str_dbg(smb_fname)));
2952 status = fsp_set_smb_fname(fsp, smb_fname);
2953 if (!NT_STATUS_IS_OK(status)) {
2954 goto out;
2956 } else {
2957 /* TODO. JRA. */
2958 /* Now we have the complete path we can work out if this is
2959 actually within this share and adjust newname accordingly. */
2960 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2961 "not sharepath %s) "
2962 "fnum %d from %s -> %s\n",
2963 fsp->conn->connectpath,
2964 sharepath,
2965 fsp->fnum,
2966 fsp_str_dbg(fsp),
2967 smb_fname_str_dbg(smb_fname)));
2970 out:
2971 TALLOC_FREE(smb_fname);
2972 return;
2976 * If a main file is opened for delete, all streams need to be checked for
2977 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2978 * If that works, delete them all by setting the delete on close and close.
2981 NTSTATUS open_streams_for_delete(connection_struct *conn,
2982 const char *fname)
2984 struct stream_struct *stream_info = NULL;
2985 files_struct **streams = NULL;
2986 int i;
2987 unsigned int num_streams = 0;
2988 TALLOC_CTX *frame = talloc_stackframe();
2989 NTSTATUS status;
2991 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
2992 &num_streams, &stream_info);
2994 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
2995 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2996 DEBUG(10, ("no streams around\n"));
2997 TALLOC_FREE(frame);
2998 return NT_STATUS_OK;
3001 if (!NT_STATUS_IS_OK(status)) {
3002 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3003 nt_errstr(status)));
3004 goto fail;
3007 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3008 num_streams));
3010 if (num_streams == 0) {
3011 TALLOC_FREE(frame);
3012 return NT_STATUS_OK;
3015 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
3016 if (streams == NULL) {
3017 DEBUG(0, ("talloc failed\n"));
3018 status = NT_STATUS_NO_MEMORY;
3019 goto fail;
3022 for (i=0; i<num_streams; i++) {
3023 struct smb_filename *smb_fname = NULL;
3025 if (strequal(stream_info[i].name, "::$DATA")) {
3026 streams[i] = NULL;
3027 continue;
3030 status = create_synthetic_smb_fname(talloc_tos(), fname,
3031 stream_info[i].name,
3032 NULL, &smb_fname);
3033 if (!NT_STATUS_IS_OK(status)) {
3034 goto fail;
3037 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3038 DEBUG(10, ("Unable to stat stream: %s\n",
3039 smb_fname_str_dbg(smb_fname)));
3042 status = SMB_VFS_CREATE_FILE(
3043 conn, /* conn */
3044 NULL, /* req */
3045 0, /* root_dir_fid */
3046 smb_fname, /* fname */
3047 DELETE_ACCESS, /* access_mask */
3048 (FILE_SHARE_READ | /* share_access */
3049 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3050 FILE_OPEN, /* create_disposition*/
3051 0, /* create_options */
3052 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3053 0, /* oplock_request */
3054 0, /* allocation_size */
3055 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3056 NULL, /* sd */
3057 NULL, /* ea_list */
3058 &streams[i], /* result */
3059 NULL); /* pinfo */
3061 if (!NT_STATUS_IS_OK(status)) {
3062 DEBUG(10, ("Could not open stream %s: %s\n",
3063 smb_fname_str_dbg(smb_fname),
3064 nt_errstr(status)));
3066 TALLOC_FREE(smb_fname);
3067 break;
3069 TALLOC_FREE(smb_fname);
3073 * don't touch the variable "status" beyond this point :-)
3076 for (i -= 1 ; i >= 0; i--) {
3077 if (streams[i] == NULL) {
3078 continue;
3081 DEBUG(10, ("Closing stream # %d, %s\n", i,
3082 fsp_str_dbg(streams[i])));
3083 close_file(NULL, streams[i], NORMAL_CLOSE);
3086 fail:
3087 TALLOC_FREE(frame);
3088 return status;
3092 * Wrapper around open_file_ntcreate and open_directory
3095 static NTSTATUS create_file_unixpath(connection_struct *conn,
3096 struct smb_request *req,
3097 struct smb_filename *smb_fname,
3098 uint32_t access_mask,
3099 uint32_t share_access,
3100 uint32_t create_disposition,
3101 uint32_t create_options,
3102 uint32_t file_attributes,
3103 uint32_t oplock_request,
3104 uint64_t allocation_size,
3105 uint32_t private_flags,
3106 struct security_descriptor *sd,
3107 struct ea_list *ea_list,
3109 files_struct **result,
3110 int *pinfo)
3112 int info = FILE_WAS_OPENED;
3113 files_struct *base_fsp = NULL;
3114 files_struct *fsp = NULL;
3115 NTSTATUS status;
3117 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3118 "file_attributes = 0x%x, share_access = 0x%x, "
3119 "create_disposition = 0x%x create_options = 0x%x "
3120 "oplock_request = 0x%x private_flags = 0x%x "
3121 "ea_list = 0x%p, sd = 0x%p, "
3122 "fname = %s\n",
3123 (unsigned int)access_mask,
3124 (unsigned int)file_attributes,
3125 (unsigned int)share_access,
3126 (unsigned int)create_disposition,
3127 (unsigned int)create_options,
3128 (unsigned int)oplock_request,
3129 (unsigned int)private_flags,
3130 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3132 if (create_options & FILE_OPEN_BY_FILE_ID) {
3133 status = NT_STATUS_NOT_SUPPORTED;
3134 goto fail;
3137 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3138 status = NT_STATUS_INVALID_PARAMETER;
3139 goto fail;
3142 if (req == NULL) {
3143 oplock_request |= INTERNAL_OPEN_ONLY;
3146 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3147 && (access_mask & DELETE_ACCESS)
3148 && !is_ntfs_stream_smb_fname(smb_fname)) {
3150 * We can't open a file with DELETE access if any of the
3151 * streams is open without FILE_SHARE_DELETE
3153 status = open_streams_for_delete(conn, smb_fname->base_name);
3155 if (!NT_STATUS_IS_OK(status)) {
3156 goto fail;
3160 /* This is the correct thing to do (check every time) but can_delete
3161 * is expensive (it may have to read the parent directory
3162 * permissions). So for now we're not doing it unless we have a strong
3163 * hint the client is really going to delete this file. If the client
3164 * is forcing FILE_CREATE let the filesystem take care of the
3165 * permissions. */
3167 /* Setting FILE_SHARE_DELETE is the hint. */
3169 if ((create_disposition != FILE_CREATE)
3170 && (access_mask & DELETE_ACCESS)
3171 && (!(can_delete_file_in_directory(conn, smb_fname) ||
3172 can_access_file_acl(conn, smb_fname, DELETE_ACCESS)))) {
3173 status = NT_STATUS_ACCESS_DENIED;
3174 DEBUG(10,("create_file_unixpath: open file %s "
3175 "for delete ACCESS_DENIED\n",
3176 smb_fname_str_dbg(smb_fname)));
3177 goto fail;
3180 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3181 !security_token_has_privilege(get_current_nttok(conn),
3182 SEC_PRIV_SECURITY)) {
3183 DEBUG(10, ("create_file_unixpath: open on %s "
3184 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3185 smb_fname_str_dbg(smb_fname)));
3186 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3187 goto fail;
3190 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3191 && is_ntfs_stream_smb_fname(smb_fname)
3192 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3193 uint32 base_create_disposition;
3194 struct smb_filename *smb_fname_base = NULL;
3196 if (create_options & FILE_DIRECTORY_FILE) {
3197 status = NT_STATUS_NOT_A_DIRECTORY;
3198 goto fail;
3201 switch (create_disposition) {
3202 case FILE_OPEN:
3203 base_create_disposition = FILE_OPEN;
3204 break;
3205 default:
3206 base_create_disposition = FILE_OPEN_IF;
3207 break;
3210 /* Create an smb_filename with stream_name == NULL. */
3211 status = create_synthetic_smb_fname(talloc_tos(),
3212 smb_fname->base_name,
3213 NULL, NULL,
3214 &smb_fname_base);
3215 if (!NT_STATUS_IS_OK(status)) {
3216 goto fail;
3219 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3220 DEBUG(10, ("Unable to stat stream: %s\n",
3221 smb_fname_str_dbg(smb_fname_base)));
3224 /* Open the base file. */
3225 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3226 FILE_SHARE_READ
3227 | FILE_SHARE_WRITE
3228 | FILE_SHARE_DELETE,
3229 base_create_disposition,
3230 0, 0, 0, 0, 0, NULL, NULL,
3231 &base_fsp, NULL);
3232 TALLOC_FREE(smb_fname_base);
3234 if (!NT_STATUS_IS_OK(status)) {
3235 DEBUG(10, ("create_file_unixpath for base %s failed: "
3236 "%s\n", smb_fname->base_name,
3237 nt_errstr(status)));
3238 goto fail;
3240 /* we don't need to low level fd */
3241 fd_close(base_fsp);
3245 * If it's a request for a directory open, deal with it separately.
3248 if (create_options & FILE_DIRECTORY_FILE) {
3250 if (create_options & FILE_NON_DIRECTORY_FILE) {
3251 status = NT_STATUS_INVALID_PARAMETER;
3252 goto fail;
3255 /* Can't open a temp directory. IFS kit test. */
3256 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3257 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3258 status = NT_STATUS_INVALID_PARAMETER;
3259 goto fail;
3263 * We will get a create directory here if the Win32
3264 * app specified a security descriptor in the
3265 * CreateDirectory() call.
3268 oplock_request = 0;
3269 status = open_directory(
3270 conn, req, smb_fname, access_mask, share_access,
3271 create_disposition, create_options, file_attributes,
3272 &info, &fsp);
3273 } else {
3276 * Ordinary file case.
3279 status = file_new(req, conn, &fsp);
3280 if(!NT_STATUS_IS_OK(status)) {
3281 goto fail;
3284 status = fsp_set_smb_fname(fsp, smb_fname);
3285 if (!NT_STATUS_IS_OK(status)) {
3286 goto fail;
3290 * We're opening the stream element of a base_fsp
3291 * we already opened. Set up the base_fsp pointer.
3293 if (base_fsp) {
3294 fsp->base_fsp = base_fsp;
3297 status = open_file_ntcreate(conn,
3298 req,
3299 access_mask,
3300 share_access,
3301 create_disposition,
3302 create_options,
3303 file_attributes,
3304 oplock_request,
3305 private_flags,
3306 &info,
3307 fsp);
3309 if(!NT_STATUS_IS_OK(status)) {
3310 file_free(req, fsp);
3311 fsp = NULL;
3314 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3316 /* A stream open never opens a directory */
3318 if (base_fsp) {
3319 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3320 goto fail;
3324 * Fail the open if it was explicitly a non-directory
3325 * file.
3328 if (create_options & FILE_NON_DIRECTORY_FILE) {
3329 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3330 goto fail;
3333 oplock_request = 0;
3334 status = open_directory(
3335 conn, req, smb_fname, access_mask,
3336 share_access, create_disposition,
3337 create_options, file_attributes,
3338 &info, &fsp);
3342 if (!NT_STATUS_IS_OK(status)) {
3343 goto fail;
3346 fsp->base_fsp = base_fsp;
3349 * According to the MS documentation, the only time the security
3350 * descriptor is applied to the opened file is iff we *created* the
3351 * file; an existing file stays the same.
3353 * Also, it seems (from observation) that you can open the file with
3354 * any access mask but you can still write the sd. We need to override
3355 * the granted access before we call set_sd
3356 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3359 if ((sd != NULL) && (info == FILE_WAS_CREATED)
3360 && lp_nt_acl_support(SNUM(conn))) {
3362 uint32_t sec_info_sent;
3363 uint32_t saved_access_mask = fsp->access_mask;
3365 sec_info_sent = get_sec_info(sd);
3367 fsp->access_mask = FILE_GENERIC_ALL;
3369 if (sec_info_sent & (SECINFO_OWNER|
3370 SECINFO_GROUP|
3371 SECINFO_DACL|
3372 SECINFO_SACL)) {
3373 status = set_sd(fsp, sd, sec_info_sent);
3376 fsp->access_mask = saved_access_mask;
3378 if (!NT_STATUS_IS_OK(status)) {
3379 goto fail;
3383 if ((ea_list != NULL) &&
3384 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3385 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3386 if (!NT_STATUS_IS_OK(status)) {
3387 goto fail;
3391 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3392 status = NT_STATUS_ACCESS_DENIED;
3393 goto fail;
3396 /* Save the requested allocation size. */
3397 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3398 if (allocation_size
3399 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3400 fsp->initial_allocation_size = smb_roundup(
3401 fsp->conn, allocation_size);
3402 if (fsp->is_directory) {
3403 /* Can't set allocation size on a directory. */
3404 status = NT_STATUS_ACCESS_DENIED;
3405 goto fail;
3407 if (vfs_allocate_file_space(
3408 fsp, fsp->initial_allocation_size) == -1) {
3409 status = NT_STATUS_DISK_FULL;
3410 goto fail;
3412 } else {
3413 fsp->initial_allocation_size = smb_roundup(
3414 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3418 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3420 *result = fsp;
3421 if (pinfo != NULL) {
3422 *pinfo = info;
3425 smb_fname->st = fsp->fsp_name->st;
3427 return NT_STATUS_OK;
3429 fail:
3430 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3432 if (fsp != NULL) {
3433 if (base_fsp && fsp->base_fsp == base_fsp) {
3435 * The close_file below will close
3436 * fsp->base_fsp.
3438 base_fsp = NULL;
3440 close_file(req, fsp, ERROR_CLOSE);
3441 fsp = NULL;
3443 if (base_fsp != NULL) {
3444 close_file(req, base_fsp, ERROR_CLOSE);
3445 base_fsp = NULL;
3447 return status;
3451 * Calculate the full path name given a relative fid.
3453 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3454 struct smb_request *req,
3455 uint16_t root_dir_fid,
3456 const struct smb_filename *smb_fname,
3457 struct smb_filename **smb_fname_out)
3459 files_struct *dir_fsp;
3460 char *parent_fname = NULL;
3461 char *new_base_name = NULL;
3462 NTSTATUS status;
3464 if (root_dir_fid == 0 || !smb_fname) {
3465 status = NT_STATUS_INTERNAL_ERROR;
3466 goto out;
3469 dir_fsp = file_fsp(req, root_dir_fid);
3471 if (dir_fsp == NULL) {
3472 status = NT_STATUS_INVALID_HANDLE;
3473 goto out;
3476 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3477 status = NT_STATUS_INVALID_HANDLE;
3478 goto out;
3481 if (!dir_fsp->is_directory) {
3484 * Check to see if this is a mac fork of some kind.
3487 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3488 is_ntfs_stream_smb_fname(smb_fname)) {
3489 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3490 goto out;
3494 we need to handle the case when we get a
3495 relative open relative to a file and the
3496 pathname is blank - this is a reopen!
3497 (hint from demyn plantenberg)
3500 status = NT_STATUS_INVALID_HANDLE;
3501 goto out;
3504 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3506 * We're at the toplevel dir, the final file name
3507 * must not contain ./, as this is filtered out
3508 * normally by srvstr_get_path and unix_convert
3509 * explicitly rejects paths containing ./.
3511 parent_fname = talloc_strdup(talloc_tos(), "");
3512 if (parent_fname == NULL) {
3513 status = NT_STATUS_NO_MEMORY;
3514 goto out;
3516 } else {
3517 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3520 * Copy in the base directory name.
3523 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3524 dir_name_len+2);
3525 if (parent_fname == NULL) {
3526 status = NT_STATUS_NO_MEMORY;
3527 goto out;
3529 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3530 dir_name_len+1);
3533 * Ensure it ends in a '/'.
3534 * We used TALLOC_SIZE +2 to add space for the '/'.
3537 if(dir_name_len
3538 && (parent_fname[dir_name_len-1] != '\\')
3539 && (parent_fname[dir_name_len-1] != '/')) {
3540 parent_fname[dir_name_len] = '/';
3541 parent_fname[dir_name_len+1] = '\0';
3545 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3546 smb_fname->base_name);
3547 if (new_base_name == NULL) {
3548 status = NT_STATUS_NO_MEMORY;
3549 goto out;
3552 status = filename_convert(req,
3553 conn,
3554 req->flags2 & FLAGS2_DFS_PATHNAMES,
3555 new_base_name,
3557 NULL,
3558 smb_fname_out);
3559 if (!NT_STATUS_IS_OK(status)) {
3560 goto out;
3563 out:
3564 TALLOC_FREE(parent_fname);
3565 TALLOC_FREE(new_base_name);
3566 return status;
3569 NTSTATUS create_file_default(connection_struct *conn,
3570 struct smb_request *req,
3571 uint16_t root_dir_fid,
3572 struct smb_filename *smb_fname,
3573 uint32_t access_mask,
3574 uint32_t share_access,
3575 uint32_t create_disposition,
3576 uint32_t create_options,
3577 uint32_t file_attributes,
3578 uint32_t oplock_request,
3579 uint64_t allocation_size,
3580 uint32_t private_flags,
3581 struct security_descriptor *sd,
3582 struct ea_list *ea_list,
3583 files_struct **result,
3584 int *pinfo)
3586 int info = FILE_WAS_OPENED;
3587 files_struct *fsp = NULL;
3588 NTSTATUS status;
3589 bool stream_name = false;
3591 DEBUG(10,("create_file: access_mask = 0x%x "
3592 "file_attributes = 0x%x, share_access = 0x%x, "
3593 "create_disposition = 0x%x create_options = 0x%x "
3594 "oplock_request = 0x%x "
3595 "private_flags = 0x%x "
3596 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3597 "fname = %s\n",
3598 (unsigned int)access_mask,
3599 (unsigned int)file_attributes,
3600 (unsigned int)share_access,
3601 (unsigned int)create_disposition,
3602 (unsigned int)create_options,
3603 (unsigned int)oplock_request,
3604 (unsigned int)private_flags,
3605 (unsigned int)root_dir_fid,
3606 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3609 * Calculate the filename from the root_dir_if if necessary.
3612 if (root_dir_fid != 0) {
3613 struct smb_filename *smb_fname_out = NULL;
3614 status = get_relative_fid_filename(conn, req, root_dir_fid,
3615 smb_fname, &smb_fname_out);
3616 if (!NT_STATUS_IS_OK(status)) {
3617 goto fail;
3619 smb_fname = smb_fname_out;
3623 * Check to see if this is a mac fork of some kind.
3626 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3627 if (stream_name) {
3628 enum FAKE_FILE_TYPE fake_file_type;
3630 fake_file_type = is_fake_file(smb_fname);
3632 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3635 * Here we go! support for changing the disk quotas
3636 * --metze
3638 * We need to fake up to open this MAGIC QUOTA file
3639 * and return a valid FID.
3641 * w2k close this file directly after openening xp
3642 * also tries a QUERY_FILE_INFO on the file and then
3643 * close it
3645 status = open_fake_file(req, conn, req->vuid,
3646 fake_file_type, smb_fname,
3647 access_mask, &fsp);
3648 if (!NT_STATUS_IS_OK(status)) {
3649 goto fail;
3652 ZERO_STRUCT(smb_fname->st);
3653 goto done;
3656 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3657 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3658 goto fail;
3662 if (stream_name && is_ntfs_default_stream_smb_fname(smb_fname)) {
3663 int ret;
3664 smb_fname->stream_name = NULL;
3665 /* We have to handle this error here. */
3666 if (create_options & FILE_DIRECTORY_FILE) {
3667 status = NT_STATUS_NOT_A_DIRECTORY;
3668 goto fail;
3670 if (lp_posix_pathnames()) {
3671 ret = SMB_VFS_LSTAT(conn, smb_fname);
3672 } else {
3673 ret = SMB_VFS_STAT(conn, smb_fname);
3676 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3677 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3678 goto fail;
3682 status = create_file_unixpath(
3683 conn, req, smb_fname, access_mask, share_access,
3684 create_disposition, create_options, file_attributes,
3685 oplock_request, allocation_size, private_flags,
3686 sd, ea_list,
3687 &fsp, &info);
3689 if (!NT_STATUS_IS_OK(status)) {
3690 goto fail;
3693 done:
3694 DEBUG(10, ("create_file: info=%d\n", info));
3696 *result = fsp;
3697 if (pinfo != NULL) {
3698 *pinfo = info;
3700 return NT_STATUS_OK;
3702 fail:
3703 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3705 if (fsp != NULL) {
3706 close_file(req, fsp, ERROR_CLOSE);
3707 fsp = NULL;
3709 return status;