s3: Check for serverid_exists in open_mode_check
[Samba/gebeck_regimport.git] / source3 / smbd / open.c
blob8a5273ef14e9f2b6c5c575c0fbaf8e9beb1af894
1 /*
2 Unix SMB/CIFS implementation.
3 file opening and share modes
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2004
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "printing.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "fake_file.h"
28 #include "../libcli/security/security.h"
29 #include "../librpc/gen_ndr/ndr_security.h"
30 #include "../librpc/gen_ndr/open_files.h"
31 #include "auth.h"
32 #include "messages.h"
34 extern const struct generic_mapping file_generic_mapping;
36 struct deferred_open_record {
37 bool delayed_for_oplocks;
38 struct file_id id;
41 /****************************************************************************
42 If the requester wanted DELETE_ACCESS and was rejected because
43 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
44 overrides this.
45 ****************************************************************************/
47 static bool parent_override_delete(connection_struct *conn,
48 const struct smb_filename *smb_fname,
49 uint32_t access_mask,
50 uint32_t rejected_mask)
52 if ((access_mask & DELETE_ACCESS) &&
53 (rejected_mask & DELETE_ACCESS) &&
54 can_delete_file_in_directory(conn, smb_fname)) {
55 return true;
57 return false;
60 /****************************************************************************
61 Check if we have open rights.
62 ****************************************************************************/
64 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
65 const struct smb_filename *smb_fname,
66 uint32_t access_mask)
68 /* Check if we have rights to open. */
69 NTSTATUS status;
70 struct security_descriptor *sd = NULL;
71 uint32_t rejected_share_access;
72 uint32_t rejected_mask = access_mask;
74 rejected_share_access = access_mask & ~(conn->share_access);
76 if (rejected_share_access) {
77 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
78 "on %s (0x%x)\n",
79 (unsigned int)access_mask,
80 smb_fname_str_dbg(smb_fname),
81 (unsigned int)rejected_share_access ));
82 return NT_STATUS_ACCESS_DENIED;
85 if (get_current_uid(conn) == (uid_t)0) {
86 /* I'm sorry sir, I didn't know you were root... */
87 DEBUG(10,("smbd_check_access_rights: root override "
88 "on %s. Granting 0x%x\n",
89 smb_fname_str_dbg(smb_fname),
90 (unsigned int)access_mask ));
91 return NT_STATUS_OK;
94 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
95 DEBUG(10,("smbd_check_access_rights: not checking ACL "
96 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
97 smb_fname_str_dbg(smb_fname),
98 (unsigned int)access_mask ));
99 return NT_STATUS_OK;
102 if (access_mask == DELETE_ACCESS &&
103 VALID_STAT(smb_fname->st) &&
104 S_ISLNK(smb_fname->st.st_ex_mode)) {
105 /* We can always delete a symlink. */
106 DEBUG(10,("smbd_check_access_rights: not checking ACL "
107 "on DELETE_ACCESS on symlink %s.\n",
108 smb_fname_str_dbg(smb_fname) ));
109 return NT_STATUS_OK;
112 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
113 (SECINFO_OWNER |
114 SECINFO_GROUP |
115 SECINFO_DACL),&sd);
117 if (!NT_STATUS_IS_OK(status)) {
118 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
119 "on %s: %s\n",
120 smb_fname_str_dbg(smb_fname),
121 nt_errstr(status)));
123 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
124 goto access_denied;
127 return status;
131 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
132 * owner WRITE_DAC and READ_CONTROL.
134 status = se_access_check(sd,
135 get_current_nttok(conn),
136 (access_mask & ~FILE_READ_ATTRIBUTES),
137 &rejected_mask);
139 DEBUG(10,("smbd_check_access_rights: file %s requesting "
140 "0x%x returning 0x%x (%s)\n",
141 smb_fname_str_dbg(smb_fname),
142 (unsigned int)access_mask,
143 (unsigned int)rejected_mask,
144 nt_errstr(status) ));
146 if (!NT_STATUS_IS_OK(status)) {
147 if (DEBUGLEVEL >= 10) {
148 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
149 smb_fname_str_dbg(smb_fname) ));
150 NDR_PRINT_DEBUG(security_descriptor, sd);
154 TALLOC_FREE(sd);
156 if (NT_STATUS_IS_OK(status) ||
157 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
158 return status;
161 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
163 access_denied:
165 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
166 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
167 !lp_store_dos_attributes(SNUM(conn)) &&
168 (lp_map_readonly(SNUM(conn)) ||
169 lp_map_archive(SNUM(conn)) ||
170 lp_map_hidden(SNUM(conn)) ||
171 lp_map_system(SNUM(conn)))) {
172 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
174 DEBUG(10,("smbd_check_access_rights: "
175 "overrode "
176 "FILE_WRITE_ATTRIBUTES "
177 "on file %s\n",
178 smb_fname_str_dbg(smb_fname)));
181 if (parent_override_delete(conn,
182 smb_fname,
183 access_mask,
184 rejected_mask)) {
185 /* Were we trying to do an open
186 * for delete and didn't get DELETE
187 * access (only) ? Check if the
188 * directory allows DELETE_CHILD.
189 * See here:
190 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
191 * for details. */
193 rejected_mask &= ~DELETE_ACCESS;
195 DEBUG(10,("smbd_check_access_rights: "
196 "overrode "
197 "DELETE_ACCESS on "
198 "file %s\n",
199 smb_fname_str_dbg(smb_fname)));
202 if (rejected_mask != 0) {
203 return NT_STATUS_ACCESS_DENIED;
205 return NT_STATUS_OK;
208 static NTSTATUS check_parent_access(struct connection_struct *conn,
209 struct smb_filename *smb_fname,
210 uint32_t access_mask)
212 NTSTATUS status;
213 char *parent_dir = NULL;
214 struct security_descriptor *parent_sd = NULL;
215 uint32_t access_granted = 0;
217 if (!parent_dirname(talloc_tos(),
218 smb_fname->base_name,
219 &parent_dir,
220 NULL)) {
221 return NT_STATUS_NO_MEMORY;
224 if (get_current_uid(conn) == (uid_t)0) {
225 /* I'm sorry sir, I didn't know you were root... */
226 DEBUG(10,("check_parent_access: root override "
227 "on %s. Granting 0x%x\n",
228 smb_fname_str_dbg(smb_fname),
229 (unsigned int)access_mask ));
230 return NT_STATUS_OK;
233 status = SMB_VFS_GET_NT_ACL(conn,
234 parent_dir,
235 SECINFO_DACL,
236 &parent_sd);
238 if (!NT_STATUS_IS_OK(status)) {
239 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
240 "%s with error %s\n",
241 parent_dir,
242 nt_errstr(status)));
243 return status;
247 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
248 * owner WRITE_DAC and READ_CONTROL.
250 status = se_access_check(parent_sd,
251 get_current_nttok(conn),
252 (access_mask & ~FILE_READ_ATTRIBUTES),
253 &access_granted);
254 if(!NT_STATUS_IS_OK(status)) {
255 DEBUG(5,("check_parent_access: access check "
256 "on directory %s for "
257 "path %s for mask 0x%x returned (0x%x) %s\n",
258 parent_dir,
259 smb_fname->base_name,
260 access_mask,
261 access_granted,
262 nt_errstr(status) ));
263 return status;
266 return NT_STATUS_OK;
269 /****************************************************************************
270 fd support routines - attempt to do a dos_open.
271 ****************************************************************************/
273 static NTSTATUS fd_open(struct connection_struct *conn,
274 files_struct *fsp,
275 int flags,
276 mode_t mode)
278 struct smb_filename *smb_fname = fsp->fsp_name;
279 NTSTATUS status = NT_STATUS_OK;
281 #ifdef O_NOFOLLOW
283 * Never follow symlinks on a POSIX client. The
284 * client should be doing this.
287 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
288 flags |= O_NOFOLLOW;
290 #endif
292 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
293 if (fsp->fh->fd == -1) {
294 status = map_nt_error_from_unix(errno);
295 if (errno == EMFILE) {
296 static time_t last_warned = 0L;
298 if (time((time_t *) NULL) > last_warned) {
299 DEBUG(0,("Too many open files, unable "
300 "to open more! smbd's max "
301 "open files = %d\n",
302 lp_max_open_files()));
303 last_warned = time((time_t *) NULL);
309 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
310 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
311 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
313 return status;
316 /****************************************************************************
317 Close the file associated with a fsp.
318 ****************************************************************************/
320 NTSTATUS fd_close(files_struct *fsp)
322 int ret;
324 if (fsp->dptr) {
325 dptr_CloseDir(fsp);
327 if (fsp->fh->fd == -1) {
328 return NT_STATUS_OK; /* What we used to call a stat open. */
330 if (fsp->fh->ref_count > 1) {
331 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
334 ret = SMB_VFS_CLOSE(fsp);
335 fsp->fh->fd = -1;
336 if (ret == -1) {
337 return map_nt_error_from_unix(errno);
339 return NT_STATUS_OK;
342 /****************************************************************************
343 Change the ownership of a file to that of the parent directory.
344 Do this by fd if possible.
345 ****************************************************************************/
347 void change_file_owner_to_parent(connection_struct *conn,
348 const char *inherit_from_dir,
349 files_struct *fsp)
351 struct smb_filename *smb_fname_parent = NULL;
352 NTSTATUS status;
353 int ret;
355 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
356 NULL, NULL, &smb_fname_parent);
357 if (!NT_STATUS_IS_OK(status)) {
358 return;
361 ret = SMB_VFS_STAT(conn, smb_fname_parent);
362 if (ret == -1) {
363 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
364 "directory %s. Error was %s\n",
365 smb_fname_str_dbg(smb_fname_parent),
366 strerror(errno)));
367 TALLOC_FREE(smb_fname_parent);
368 return;
371 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
372 /* Already this uid - no need to change. */
373 DEBUG(10,("change_file_owner_to_parent: file %s "
374 "is already owned by uid %d\n",
375 fsp_str_dbg(fsp),
376 (int)fsp->fsp_name->st.st_ex_uid ));
377 TALLOC_FREE(smb_fname_parent);
378 return;
381 become_root();
382 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
383 unbecome_root();
384 if (ret == -1) {
385 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
386 "file %s to parent directory uid %u. Error "
387 "was %s\n", fsp_str_dbg(fsp),
388 (unsigned int)smb_fname_parent->st.st_ex_uid,
389 strerror(errno) ));
390 } else {
391 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
392 "parent directory uid %u.\n", fsp_str_dbg(fsp),
393 (unsigned int)smb_fname_parent->st.st_ex_uid));
394 /* Ensure the uid entry is updated. */
395 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
398 TALLOC_FREE(smb_fname_parent);
401 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
402 const char *inherit_from_dir,
403 const char *fname,
404 SMB_STRUCT_STAT *psbuf)
406 struct smb_filename *smb_fname_parent = NULL;
407 struct smb_filename *smb_fname_cwd = NULL;
408 char *saved_dir = NULL;
409 TALLOC_CTX *ctx = talloc_tos();
410 NTSTATUS status = NT_STATUS_OK;
411 int ret;
413 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
414 &smb_fname_parent);
415 if (!NT_STATUS_IS_OK(status)) {
416 return status;
419 ret = SMB_VFS_STAT(conn, smb_fname_parent);
420 if (ret == -1) {
421 status = map_nt_error_from_unix(errno);
422 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
423 "directory %s. Error was %s\n",
424 smb_fname_str_dbg(smb_fname_parent),
425 strerror(errno)));
426 goto out;
429 /* We've already done an lstat into psbuf, and we know it's a
430 directory. If we can cd into the directory and the dev/ino
431 are the same then we can safely chown without races as
432 we're locking the directory in place by being in it. This
433 should work on any UNIX (thanks tridge :-). JRA.
436 saved_dir = vfs_GetWd(ctx,conn);
437 if (!saved_dir) {
438 status = map_nt_error_from_unix(errno);
439 DEBUG(0,("change_dir_owner_to_parent: failed to get "
440 "current working directory. Error was %s\n",
441 strerror(errno)));
442 goto out;
445 /* Chdir into the new path. */
446 if (vfs_ChDir(conn, fname) == -1) {
447 status = map_nt_error_from_unix(errno);
448 DEBUG(0,("change_dir_owner_to_parent: failed to change "
449 "current working directory to %s. Error "
450 "was %s\n", fname, strerror(errno) ));
451 goto chdir;
454 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
455 &smb_fname_cwd);
456 if (!NT_STATUS_IS_OK(status)) {
457 return status;
460 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
461 if (ret == -1) {
462 status = map_nt_error_from_unix(errno);
463 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
464 "directory '.' (%s) Error was %s\n",
465 fname, strerror(errno)));
466 goto chdir;
469 /* Ensure we're pointing at the same place. */
470 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
471 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
472 DEBUG(0,("change_dir_owner_to_parent: "
473 "device/inode on directory %s changed. "
474 "Refusing to chown !\n", fname ));
475 status = NT_STATUS_ACCESS_DENIED;
476 goto chdir;
479 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
480 /* Already this uid - no need to change. */
481 DEBUG(10,("change_dir_owner_to_parent: directory %s "
482 "is already owned by uid %d\n",
483 fname,
484 (int)smb_fname_cwd->st.st_ex_uid ));
485 status = NT_STATUS_OK;
486 goto chdir;
489 become_root();
490 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
491 (gid_t)-1);
492 unbecome_root();
493 if (ret == -1) {
494 status = map_nt_error_from_unix(errno);
495 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
496 "directory %s to parent directory uid %u. "
497 "Error was %s\n", fname,
498 (unsigned int)smb_fname_parent->st.st_ex_uid,
499 strerror(errno) ));
500 } else {
501 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
502 "directory %s to parent directory uid %u.\n",
503 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
504 /* Ensure the uid entry is updated. */
505 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
508 chdir:
509 vfs_ChDir(conn,saved_dir);
510 out:
511 TALLOC_FREE(smb_fname_parent);
512 TALLOC_FREE(smb_fname_cwd);
513 return status;
516 /****************************************************************************
517 Open a file.
518 ****************************************************************************/
520 static NTSTATUS open_file(files_struct *fsp,
521 connection_struct *conn,
522 struct smb_request *req,
523 const char *parent_dir,
524 int flags,
525 mode_t unx_mode,
526 uint32 access_mask, /* client requested access mask. */
527 uint32 open_access_mask) /* what we're actually using in the open. */
529 struct smb_filename *smb_fname = fsp->fsp_name;
530 NTSTATUS status = NT_STATUS_OK;
531 int accmode = (flags & O_ACCMODE);
532 int local_flags = flags;
533 bool file_existed = VALID_STAT(fsp->fsp_name->st);
534 bool file_created = false;
536 fsp->fh->fd = -1;
537 errno = EPERM;
539 /* Check permissions */
542 * This code was changed after seeing a client open request
543 * containing the open mode of (DENY_WRITE/read-only) with
544 * the 'create if not exist' bit set. The previous code
545 * would fail to open the file read only on a read-only share
546 * as it was checking the flags parameter directly against O_RDONLY,
547 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
548 * JRA.
551 if (!CAN_WRITE(conn)) {
552 /* It's a read-only share - fail if we wanted to write. */
553 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
554 DEBUG(3,("Permission denied opening %s\n",
555 smb_fname_str_dbg(smb_fname)));
556 return NT_STATUS_ACCESS_DENIED;
557 } else if(flags & O_CREAT) {
558 /* We don't want to write - but we must make sure that
559 O_CREAT doesn't create the file if we have write
560 access into the directory.
562 flags &= ~(O_CREAT|O_EXCL);
563 local_flags &= ~(O_CREAT|O_EXCL);
568 * This little piece of insanity is inspired by the
569 * fact that an NT client can open a file for O_RDONLY,
570 * but set the create disposition to FILE_EXISTS_TRUNCATE.
571 * If the client *can* write to the file, then it expects to
572 * truncate the file, even though it is opening for readonly.
573 * Quicken uses this stupid trick in backup file creation...
574 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
575 * for helping track this one down. It didn't bite us in 2.0.x
576 * as we always opened files read-write in that release. JRA.
579 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
580 DEBUG(10,("open_file: truncate requested on read-only open "
581 "for file %s\n", smb_fname_str_dbg(smb_fname)));
582 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
585 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
586 (!file_existed && (local_flags & O_CREAT)) ||
587 ((local_flags & O_TRUNC) == O_TRUNC) ) {
588 const char *wild;
591 * We can't actually truncate here as the file may be locked.
592 * open_file_ntcreate will take care of the truncate later. JRA.
595 local_flags &= ~O_TRUNC;
597 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
599 * We would block on opening a FIFO with no one else on the
600 * other end. Do what we used to do and add O_NONBLOCK to the
601 * open flags. JRA.
604 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
605 local_flags |= O_NONBLOCK;
607 #endif
609 /* Don't create files with Microsoft wildcard characters. */
610 if (fsp->base_fsp) {
612 * wildcard characters are allowed in stream names
613 * only test the basefilename
615 wild = fsp->base_fsp->fsp_name->base_name;
616 } else {
617 wild = smb_fname->base_name;
619 if ((local_flags & O_CREAT) && !file_existed &&
620 ms_has_wild(wild)) {
621 return NT_STATUS_OBJECT_NAME_INVALID;
624 /* Can we access this file ? */
625 if (!fsp->base_fsp) {
626 /* Only do this check on non-stream open. */
627 if (file_existed) {
628 status = smbd_check_access_rights(conn,
629 smb_fname,
630 access_mask);
631 } else if (local_flags & O_CREAT){
632 status = check_parent_access(conn,
633 smb_fname,
634 SEC_DIR_ADD_FILE);
635 } else {
636 /* File didn't exist and no O_CREAT. */
637 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
639 if (!NT_STATUS_IS_OK(status)) {
640 DEBUG(10,("open_file: "
641 "%s on file "
642 "%s returned %s\n",
643 file_existed ?
644 "smbd_check_access_rights" :
645 "check_parent_access",
646 smb_fname_str_dbg(smb_fname),
647 nt_errstr(status) ));
648 return status;
652 /* Actually do the open */
653 status = fd_open(conn, fsp, local_flags, unx_mode);
654 if (!NT_STATUS_IS_OK(status)) {
655 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
656 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
657 nt_errstr(status),local_flags,flags));
658 return status;
661 if ((local_flags & O_CREAT) && !file_existed) {
662 file_created = true;
665 } else {
666 fsp->fh->fd = -1; /* What we used to call a stat open. */
667 if (!file_existed) {
668 /* File must exist for a stat open. */
669 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
672 status = smbd_check_access_rights(conn,
673 smb_fname,
674 access_mask);
676 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
677 fsp->posix_open &&
678 S_ISLNK(smb_fname->st.st_ex_mode)) {
679 /* This is a POSIX stat open for delete
680 * or rename on a symlink that points
681 * nowhere. Allow. */
682 DEBUG(10,("open_file: allowing POSIX "
683 "open on bad symlink %s\n",
684 smb_fname_str_dbg(smb_fname)));
685 status = NT_STATUS_OK;
688 if (!NT_STATUS_IS_OK(status)) {
689 DEBUG(10,("open_file: "
690 "smbd_check_access_rights on file "
691 "%s returned %s\n",
692 smb_fname_str_dbg(smb_fname),
693 nt_errstr(status) ));
694 return status;
698 if (!file_existed) {
699 int ret;
701 if (fsp->fh->fd == -1) {
702 ret = SMB_VFS_STAT(conn, smb_fname);
703 } else {
704 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
705 /* If we have an fd, this stat should succeed. */
706 if (ret == -1) {
707 DEBUG(0,("Error doing fstat on open file %s "
708 "(%s)\n",
709 smb_fname_str_dbg(smb_fname),
710 strerror(errno) ));
714 /* For a non-io open, this stat failing means file not found. JRA */
715 if (ret == -1) {
716 status = map_nt_error_from_unix(errno);
717 fd_close(fsp);
718 return status;
721 if (file_created) {
722 bool need_re_stat = false;
723 /* Do all inheritance work after we've
724 done a successful stat call and filled
725 in the stat struct in fsp->fsp_name. */
727 /* Inherit the ACL if required */
728 if (lp_inherit_perms(SNUM(conn))) {
729 inherit_access_posix_acl(conn, parent_dir,
730 smb_fname->base_name,
731 unx_mode);
732 need_re_stat = true;
735 /* Change the owner if required. */
736 if (lp_inherit_owner(SNUM(conn))) {
737 change_file_owner_to_parent(conn, parent_dir,
738 fsp);
739 need_re_stat = true;
742 if (need_re_stat) {
743 if (fsp->fh->fd == -1) {
744 ret = SMB_VFS_STAT(conn, smb_fname);
745 } else {
746 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
747 /* If we have an fd, this stat should succeed. */
748 if (ret == -1) {
749 DEBUG(0,("Error doing fstat on open file %s "
750 "(%s)\n",
751 smb_fname_str_dbg(smb_fname),
752 strerror(errno) ));
757 notify_fname(conn, NOTIFY_ACTION_ADDED,
758 FILE_NOTIFY_CHANGE_FILE_NAME,
759 smb_fname->base_name);
764 * POSIX allows read-only opens of directories. We don't
765 * want to do this (we use a different code path for this)
766 * so catch a directory open and return an EISDIR. JRA.
769 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
770 fd_close(fsp);
771 errno = EISDIR;
772 return NT_STATUS_FILE_IS_A_DIRECTORY;
775 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
776 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
777 fsp->file_pid = req ? req->smbpid : 0;
778 fsp->can_lock = True;
779 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
780 if (!CAN_WRITE(conn)) {
781 fsp->can_write = False;
782 } else {
783 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
784 True : False;
786 fsp->print_file = NULL;
787 fsp->modified = False;
788 fsp->sent_oplock_break = NO_BREAK_SENT;
789 fsp->is_directory = False;
790 if (conn->aio_write_behind_list &&
791 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
792 conn->case_sensitive)) {
793 fsp->aio_write_behind = True;
796 fsp->wcp = NULL; /* Write cache pointer. */
798 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
799 conn->session_info->unix_info->unix_name,
800 smb_fname_str_dbg(smb_fname),
801 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
802 conn->num_files_open));
804 errno = 0;
805 return NT_STATUS_OK;
808 /****************************************************************************
809 Check if we can open a file with a share mode.
810 Returns True if conflict, False if not.
811 ****************************************************************************/
813 static bool share_conflict(struct share_mode_entry *entry,
814 uint32 access_mask,
815 uint32 share_access)
817 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
818 "entry->share_access = 0x%x, "
819 "entry->private_options = 0x%x\n",
820 (unsigned int)entry->access_mask,
821 (unsigned int)entry->share_access,
822 (unsigned int)entry->private_options));
824 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
825 (unsigned int)access_mask, (unsigned int)share_access));
827 if ((entry->access_mask & (FILE_WRITE_DATA|
828 FILE_APPEND_DATA|
829 FILE_READ_DATA|
830 FILE_EXECUTE|
831 DELETE_ACCESS)) == 0) {
832 DEBUG(10,("share_conflict: No conflict due to "
833 "entry->access_mask = 0x%x\n",
834 (unsigned int)entry->access_mask ));
835 return False;
838 if ((access_mask & (FILE_WRITE_DATA|
839 FILE_APPEND_DATA|
840 FILE_READ_DATA|
841 FILE_EXECUTE|
842 DELETE_ACCESS)) == 0) {
843 DEBUG(10,("share_conflict: No conflict due to "
844 "access_mask = 0x%x\n",
845 (unsigned int)access_mask ));
846 return False;
849 #if 1 /* JRA TEST - Superdebug. */
850 #define CHECK_MASK(num, am, right, sa, share) \
851 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
852 (unsigned int)(num), (unsigned int)(am), \
853 (unsigned int)(right), (unsigned int)(am)&(right) )); \
854 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
855 (unsigned int)(num), (unsigned int)(sa), \
856 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
857 if (((am) & (right)) && !((sa) & (share))) { \
858 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
859 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
860 (unsigned int)(share) )); \
861 return True; \
863 #else
864 #define CHECK_MASK(num, am, right, sa, share) \
865 if (((am) & (right)) && !((sa) & (share))) { \
866 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
867 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
868 (unsigned int)(share) )); \
869 return True; \
871 #endif
873 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
874 share_access, FILE_SHARE_WRITE);
875 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
876 entry->share_access, FILE_SHARE_WRITE);
878 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
879 share_access, FILE_SHARE_READ);
880 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
881 entry->share_access, FILE_SHARE_READ);
883 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
884 share_access, FILE_SHARE_DELETE);
885 CHECK_MASK(6, access_mask, DELETE_ACCESS,
886 entry->share_access, FILE_SHARE_DELETE);
888 DEBUG(10,("share_conflict: No conflict.\n"));
889 return False;
892 #if defined(DEVELOPER)
893 static void validate_my_share_entries(struct smbd_server_connection *sconn,
894 int num,
895 struct share_mode_entry *share_entry)
897 files_struct *fsp;
899 if (!procid_is_me(&share_entry->pid)) {
900 return;
903 if (is_deferred_open_entry(share_entry) &&
904 !open_was_deferred(sconn, share_entry->op_mid)) {
905 char *str = talloc_asprintf(talloc_tos(),
906 "Got a deferred entry without a request: "
907 "PANIC: %s\n",
908 share_mode_str(talloc_tos(), num, share_entry));
909 smb_panic(str);
912 if (!is_valid_share_mode_entry(share_entry)) {
913 return;
916 fsp = file_find_dif(sconn, share_entry->id,
917 share_entry->share_file_id);
918 if (!fsp) {
919 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
920 share_mode_str(talloc_tos(), num, share_entry) ));
921 smb_panic("validate_my_share_entries: Cannot match a "
922 "share entry with an open file\n");
925 if (is_deferred_open_entry(share_entry)) {
926 goto panic;
929 if ((share_entry->op_type == NO_OPLOCK) &&
930 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
931 /* Someone has already written to it, but I haven't yet
932 * noticed */
933 return;
936 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
937 goto panic;
940 return;
942 panic:
944 char *str;
945 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
946 share_mode_str(talloc_tos(), num, share_entry) ));
947 str = talloc_asprintf(talloc_tos(),
948 "validate_my_share_entries: "
949 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
950 fsp->fsp_name->base_name,
951 (unsigned int)fsp->oplock_type,
952 (unsigned int)share_entry->op_type );
953 smb_panic(str);
956 #endif
958 bool is_stat_open(uint32 access_mask)
960 return (access_mask &&
961 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
962 FILE_WRITE_ATTRIBUTES))==0) &&
963 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
964 FILE_WRITE_ATTRIBUTES)) != 0));
967 /****************************************************************************
968 Deal with share modes
969 Invarient: Share mode must be locked on entry and exit.
970 Returns -1 on error, or number of share modes on success (may be zero).
971 ****************************************************************************/
973 static NTSTATUS open_mode_check(connection_struct *conn,
974 struct share_mode_lock *lck,
975 uint32_t name_hash,
976 uint32 access_mask,
977 uint32 share_access,
978 uint32 create_options,
979 bool *file_existed)
981 int i;
983 if(lck->data->num_share_modes == 0) {
984 return NT_STATUS_OK;
987 *file_existed = True;
989 /* A delete on close prohibits everything */
991 if (is_delete_on_close_set(lck, name_hash)) {
992 return NT_STATUS_DELETE_PENDING;
995 if (is_stat_open(access_mask)) {
996 /* Stat open that doesn't trigger oplock breaks or share mode
997 * checks... ! JRA. */
998 return NT_STATUS_OK;
1002 * Check if the share modes will give us access.
1005 #if defined(DEVELOPER)
1006 for(i = 0; i < lck->data->num_share_modes; i++) {
1007 validate_my_share_entries(conn->sconn, i,
1008 &lck->data->share_modes[i]);
1010 #endif
1012 if (!lp_share_modes(SNUM(conn))) {
1013 return NT_STATUS_OK;
1016 /* Now we check the share modes, after any oplock breaks. */
1017 for(i = 0; i < lck->data->num_share_modes; i++) {
1019 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1020 continue;
1023 /* someone else has a share lock on it, check to see if we can
1024 * too */
1025 if (share_conflict(&lck->data->share_modes[i],
1026 access_mask, share_access)) {
1028 if (share_mode_stale_pid(lck->data, i)) {
1029 continue;
1032 return NT_STATUS_SHARING_VIOLATION;
1036 return NT_STATUS_OK;
1039 static bool is_delete_request(files_struct *fsp) {
1040 return ((fsp->access_mask == DELETE_ACCESS) &&
1041 (fsp->oplock_type == NO_OPLOCK));
1045 * Send a break message to the oplock holder and delay the open for
1046 * our client.
1049 static NTSTATUS send_break_message(files_struct *fsp,
1050 struct share_mode_entry *exclusive,
1051 uint64_t mid,
1052 int oplock_request)
1054 NTSTATUS status;
1055 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1057 DEBUG(10, ("Sending break request to PID %s\n",
1058 procid_str_static(&exclusive->pid)));
1059 exclusive->op_mid = mid;
1061 /* Create the message. */
1062 share_mode_entry_to_message(msg, exclusive);
1064 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1065 don't want this set in the share mode struct pointed to by lck. */
1067 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1068 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1069 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1072 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1073 MSG_SMB_BREAK_REQUEST,
1074 (uint8 *)msg,
1075 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1076 if (!NT_STATUS_IS_OK(status)) {
1077 DEBUG(3, ("Could not send oplock break message: %s\n",
1078 nt_errstr(status)));
1081 return status;
1085 * Return share_mode_entry pointers for :
1086 * 1). Batch oplock entry.
1087 * 2). Batch or exclusive oplock entry (may be identical to #1).
1088 * bool have_level2_oplock
1089 * bool have_no_oplock.
1090 * Do internal consistency checks on the share mode for a file.
1093 static void find_oplock_types(files_struct *fsp,
1094 int oplock_request,
1095 const struct share_mode_lock *lck,
1096 struct share_mode_entry **pp_batch,
1097 struct share_mode_entry **pp_ex_or_batch,
1098 bool *got_level2,
1099 bool *got_no_oplock)
1101 int i;
1103 *pp_batch = NULL;
1104 *pp_ex_or_batch = NULL;
1105 *got_level2 = false;
1106 *got_no_oplock = false;
1108 /* Ignore stat or internal opens, as is done in
1109 delay_for_batch_oplocks() and
1110 delay_for_exclusive_oplocks().
1112 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1113 return;
1116 for (i=0; i<lck->data->num_share_modes; i++) {
1117 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1118 continue;
1121 if (lck->data->share_modes[i].op_type == NO_OPLOCK &&
1122 is_stat_open(lck->data->share_modes[i].access_mask)) {
1123 /* We ignore stat opens in the table - they
1124 always have NO_OPLOCK and never get or
1125 cause breaks. JRA. */
1126 continue;
1129 if (BATCH_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1130 /* batch - can only be one. */
1131 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1132 smb_panic("Bad batch oplock entry.");
1134 *pp_batch = &lck->data->share_modes[i];
1137 if (EXCLUSIVE_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1138 /* Exclusive or batch - can only be one. */
1139 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1140 smb_panic("Bad exclusive or batch oplock entry.");
1142 *pp_ex_or_batch = &lck->data->share_modes[i];
1145 if (LEVEL_II_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1146 if (*pp_batch || *pp_ex_or_batch) {
1147 smb_panic("Bad levelII oplock entry.");
1149 *got_level2 = true;
1152 if (lck->data->share_modes[i].op_type == NO_OPLOCK) {
1153 if (*pp_batch || *pp_ex_or_batch) {
1154 smb_panic("Bad no oplock entry.");
1156 *got_no_oplock = true;
1161 static bool delay_for_batch_oplocks(files_struct *fsp,
1162 uint64_t mid,
1163 int oplock_request,
1164 struct share_mode_entry *batch_entry)
1166 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1167 return false;
1169 if (batch_entry == NULL) {
1170 return false;
1173 /* Found a batch oplock */
1174 send_break_message(fsp, batch_entry, mid, oplock_request);
1175 return true;
1178 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1179 uint64_t mid,
1180 int oplock_request,
1181 struct share_mode_entry *ex_entry)
1183 bool delay_it;
1185 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1186 return false;
1188 if (ex_entry == NULL) {
1189 return false;
1192 /* Found an exclusive or batch oplock */
1194 delay_it = is_delete_request(fsp) ?
1195 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1197 if (!delay_it) {
1198 return false;
1201 send_break_message(fsp, ex_entry, mid, oplock_request);
1202 return true;
1205 static bool file_has_brlocks(files_struct *fsp)
1207 struct byte_range_lock *br_lck;
1209 br_lck = brl_get_locks_readonly(fsp);
1210 if (!br_lck)
1211 return false;
1213 return br_lck->num_locks > 0 ? true : false;
1216 static void grant_fsp_oplock_type(files_struct *fsp,
1217 int oplock_request,
1218 bool got_level2_oplock,
1219 bool got_a_none_oplock)
1221 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1222 lp_level2_oplocks(SNUM(fsp->conn));
1224 /* Start by granting what the client asked for,
1225 but ensure no SAMBA_PRIVATE bits can be set. */
1226 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1228 if (oplock_request & INTERNAL_OPEN_ONLY) {
1229 /* No oplocks on internal open. */
1230 fsp->oplock_type = NO_OPLOCK;
1231 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1232 fsp->oplock_type, fsp_str_dbg(fsp)));
1233 return;
1234 } else if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1235 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1236 fsp_str_dbg(fsp)));
1237 fsp->oplock_type = NO_OPLOCK;
1240 if (is_stat_open(fsp->access_mask)) {
1241 /* Leave the value already set. */
1242 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1243 fsp->oplock_type, fsp_str_dbg(fsp)));
1244 return;
1248 * Match what was requested (fsp->oplock_type) with
1249 * what was found in the existing share modes.
1252 if (got_a_none_oplock) {
1253 fsp->oplock_type = NO_OPLOCK;
1254 } else if (got_level2_oplock) {
1255 if (fsp->oplock_type == NO_OPLOCK ||
1256 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1257 /* Store a level2 oplock, but don't tell the client */
1258 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1259 } else {
1260 fsp->oplock_type = LEVEL_II_OPLOCK;
1262 } else {
1263 /* All share_mode_entries are placeholders or deferred.
1264 * Silently upgrade to fake levelII if the client didn't
1265 * ask for an oplock. */
1266 if (fsp->oplock_type == NO_OPLOCK) {
1267 /* Store a level2 oplock, but don't tell the client */
1268 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1273 * Don't grant level2 to clients that don't want them
1274 * or if we've turned them off.
1276 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1277 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1280 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1281 fsp->oplock_type, fsp_str_dbg(fsp)));
1284 bool request_timed_out(struct timeval request_time,
1285 struct timeval timeout)
1287 struct timeval now, end_time;
1288 GetTimeOfDay(&now);
1289 end_time = timeval_sum(&request_time, &timeout);
1290 return (timeval_compare(&end_time, &now) < 0);
1293 /****************************************************************************
1294 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1295 ****************************************************************************/
1297 static void defer_open(struct share_mode_lock *lck,
1298 struct timeval request_time,
1299 struct timeval timeout,
1300 struct smb_request *req,
1301 struct deferred_open_record *state)
1303 int i;
1305 /* Paranoia check */
1307 for (i=0; i<lck->data->num_share_modes; i++) {
1308 struct share_mode_entry *e = &lck->data->share_modes[i];
1310 if (is_deferred_open_entry(e) &&
1311 procid_is_me(&e->pid) &&
1312 (e->op_mid == req->mid)) {
1313 DEBUG(0, ("Trying to defer an already deferred "
1314 "request: mid=%llu, exiting\n",
1315 (unsigned long long)req->mid));
1316 exit_server("attempt to defer a deferred request");
1320 /* End paranoia check */
1322 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1323 "open entry for mid %llu\n",
1324 (unsigned int)request_time.tv_sec,
1325 (unsigned int)request_time.tv_usec,
1326 (unsigned long long)req->mid));
1328 if (!push_deferred_open_message_smb(req, request_time, timeout,
1329 state->id, (char *)state, sizeof(*state))) {
1330 exit_server("push_deferred_open_message_smb failed");
1332 add_deferred_open(lck, req->mid, request_time,
1333 messaging_server_id(req->sconn->msg_ctx), state->id);
1337 /****************************************************************************
1338 On overwrite open ensure that the attributes match.
1339 ****************************************************************************/
1341 bool open_match_attributes(connection_struct *conn,
1342 uint32 old_dos_attr,
1343 uint32 new_dos_attr,
1344 mode_t existing_unx_mode,
1345 mode_t new_unx_mode,
1346 mode_t *returned_unx_mode)
1348 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1350 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1351 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1353 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1354 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1355 *returned_unx_mode = new_unx_mode;
1356 } else {
1357 *returned_unx_mode = (mode_t)0;
1360 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1361 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1362 "returned_unx_mode = 0%o\n",
1363 (unsigned int)old_dos_attr,
1364 (unsigned int)existing_unx_mode,
1365 (unsigned int)new_dos_attr,
1366 (unsigned int)*returned_unx_mode ));
1368 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1369 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1370 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1371 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1372 return False;
1375 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1376 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1377 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1378 return False;
1381 return True;
1384 /****************************************************************************
1385 Special FCB or DOS processing in the case of a sharing violation.
1386 Try and find a duplicated file handle.
1387 ****************************************************************************/
1389 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1390 connection_struct *conn,
1391 files_struct *fsp_to_dup_into,
1392 const struct smb_filename *smb_fname,
1393 struct file_id id,
1394 uint16 file_pid,
1395 uint16 vuid,
1396 uint32 access_mask,
1397 uint32 share_access,
1398 uint32 create_options)
1400 files_struct *fsp;
1402 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1403 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1405 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1406 fsp = file_find_di_next(fsp)) {
1408 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1409 "vuid = %u, file_pid = %u, private_options = 0x%x "
1410 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1411 fsp->fh->fd, (unsigned int)fsp->vuid,
1412 (unsigned int)fsp->file_pid,
1413 (unsigned int)fsp->fh->private_options,
1414 (unsigned int)fsp->access_mask ));
1416 if (fsp->fh->fd != -1 &&
1417 fsp->vuid == vuid &&
1418 fsp->file_pid == file_pid &&
1419 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1420 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1421 (fsp->access_mask & FILE_WRITE_DATA) &&
1422 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1423 strequal(fsp->fsp_name->stream_name,
1424 smb_fname->stream_name)) {
1425 DEBUG(10,("fcb_or_dos_open: file match\n"));
1426 break;
1430 if (!fsp) {
1431 return NT_STATUS_NOT_FOUND;
1434 /* quite an insane set of semantics ... */
1435 if (is_executable(smb_fname->base_name) &&
1436 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1437 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1438 return NT_STATUS_INVALID_PARAMETER;
1441 /* We need to duplicate this fsp. */
1442 return dup_file_fsp(req, fsp, access_mask, share_access,
1443 create_options, fsp_to_dup_into);
1446 static void schedule_defer_open(struct share_mode_lock *lck,
1447 struct timeval request_time,
1448 struct smb_request *req)
1450 struct deferred_open_record state;
1452 /* This is a relative time, added to the absolute
1453 request_time value to get the absolute timeout time.
1454 Note that if this is the second or greater time we enter
1455 this codepath for this particular request mid then
1456 request_time is left as the absolute time of the *first*
1457 time this request mid was processed. This is what allows
1458 the request to eventually time out. */
1460 struct timeval timeout;
1462 /* Normally the smbd we asked should respond within
1463 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1464 * the client did, give twice the timeout as a safety
1465 * measure here in case the other smbd is stuck
1466 * somewhere else. */
1468 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1470 /* Nothing actually uses state.delayed_for_oplocks
1471 but it's handy to differentiate in debug messages
1472 between a 30 second delay due to oplock break, and
1473 a 1 second delay for share mode conflicts. */
1475 state.delayed_for_oplocks = True;
1476 state.id = lck->data->id;
1478 if (!request_timed_out(request_time, timeout)) {
1479 defer_open(lck, request_time, timeout, req, &state);
1483 /****************************************************************************
1484 Work out what access_mask to use from what the client sent us.
1485 ****************************************************************************/
1487 static NTSTATUS smbd_calculate_maximum_allowed_access(
1488 connection_struct *conn,
1489 const struct smb_filename *smb_fname,
1490 uint32_t *p_access_mask)
1492 struct security_descriptor *sd;
1493 uint32_t access_granted;
1494 NTSTATUS status;
1496 if (get_current_uid(conn) == (uid_t)0) {
1497 *p_access_mask |= FILE_GENERIC_ALL;
1498 return NT_STATUS_OK;
1501 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1502 (SECINFO_OWNER |
1503 SECINFO_GROUP |
1504 SECINFO_DACL),&sd);
1506 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1508 * File did not exist
1510 *p_access_mask = FILE_GENERIC_ALL;
1511 return NT_STATUS_OK;
1513 if (!NT_STATUS_IS_OK(status)) {
1514 DEBUG(10,("smbd_calculate_access_mask: "
1515 "Could not get acl on file %s: %s\n",
1516 smb_fname_str_dbg(smb_fname),
1517 nt_errstr(status)));
1518 return NT_STATUS_ACCESS_DENIED;
1522 * Never test FILE_READ_ATTRIBUTES. se_access_check()
1523 * also takes care of owner WRITE_DAC and READ_CONTROL.
1525 status = se_access_check(sd,
1526 get_current_nttok(conn),
1527 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1528 &access_granted);
1530 TALLOC_FREE(sd);
1532 if (!NT_STATUS_IS_OK(status)) {
1533 DEBUG(10, ("smbd_calculate_access_mask: "
1534 "Access denied on file %s: "
1535 "when calculating maximum access\n",
1536 smb_fname_str_dbg(smb_fname)));
1537 return NT_STATUS_ACCESS_DENIED;
1539 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1540 return NT_STATUS_OK;
1543 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1544 const struct smb_filename *smb_fname,
1545 uint32_t access_mask,
1546 uint32_t *access_mask_out)
1548 NTSTATUS status;
1549 uint32_t orig_access_mask = access_mask;
1550 uint32_t rejected_share_access;
1553 * Convert GENERIC bits to specific bits.
1556 se_map_generic(&access_mask, &file_generic_mapping);
1558 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1559 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1561 status = smbd_calculate_maximum_allowed_access(
1562 conn, smb_fname, &access_mask);
1564 if (!NT_STATUS_IS_OK(status)) {
1565 return status;
1568 access_mask &= conn->share_access;
1571 rejected_share_access = access_mask & ~(conn->share_access);
1573 if (rejected_share_access) {
1574 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1575 "file %s: rejected by share access mask[0x%08X] "
1576 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1577 smb_fname_str_dbg(smb_fname),
1578 conn->share_access,
1579 orig_access_mask, access_mask,
1580 rejected_share_access));
1581 return NT_STATUS_ACCESS_DENIED;
1584 *access_mask_out = access_mask;
1585 return NT_STATUS_OK;
1588 /****************************************************************************
1589 Remove the deferred open entry under lock.
1590 ****************************************************************************/
1592 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1593 struct server_id pid)
1595 struct share_mode_lock *lck = get_existing_share_mode_lock(
1596 talloc_tos(), id);
1597 if (lck == NULL) {
1598 DEBUG(0, ("could not get share mode lock\n"));
1599 return;
1601 del_deferred_open_entry(lck, mid, pid);
1602 TALLOC_FREE(lck);
1605 /****************************************************************************
1606 Open a file with a share mode. Passed in an already created files_struct *.
1607 ****************************************************************************/
1609 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1610 struct smb_request *req,
1611 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1612 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1613 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1614 uint32 create_options, /* options such as delete on close. */
1615 uint32 new_dos_attributes, /* attributes used for new file. */
1616 int oplock_request, /* internal Samba oplock codes. */
1617 /* Information (FILE_EXISTS etc.) */
1618 uint32_t private_flags, /* Samba specific flags. */
1619 int *pinfo,
1620 files_struct *fsp)
1622 struct smb_filename *smb_fname = fsp->fsp_name;
1623 int flags=0;
1624 int flags2=0;
1625 bool file_existed = VALID_STAT(smb_fname->st);
1626 bool def_acl = False;
1627 bool posix_open = False;
1628 bool new_file_created = False;
1629 bool clear_ads = false;
1630 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1631 mode_t new_unx_mode = (mode_t)0;
1632 mode_t unx_mode = (mode_t)0;
1633 int info;
1634 uint32 existing_dos_attributes = 0;
1635 struct timeval request_time = timeval_zero();
1636 struct share_mode_lock *lck = NULL;
1637 uint32 open_access_mask = access_mask;
1638 NTSTATUS status;
1639 char *parent_dir;
1641 if (conn->printer) {
1643 * Printers are handled completely differently.
1644 * Most of the passed parameters are ignored.
1647 if (pinfo) {
1648 *pinfo = FILE_WAS_CREATED;
1651 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1652 smb_fname_str_dbg(smb_fname)));
1654 if (!req) {
1655 DEBUG(0,("open_file_ntcreate: printer open without "
1656 "an SMB request!\n"));
1657 return NT_STATUS_INTERNAL_ERROR;
1660 return print_spool_open(fsp, smb_fname->base_name,
1661 req->vuid);
1664 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1665 NULL)) {
1666 return NT_STATUS_NO_MEMORY;
1669 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1670 posix_open = True;
1671 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1672 new_dos_attributes = 0;
1673 } else {
1674 /* Windows allows a new file to be created and
1675 silently removes a FILE_ATTRIBUTE_DIRECTORY
1676 sent by the client. Do the same. */
1678 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1680 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1681 * created new. */
1682 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1683 smb_fname, parent_dir);
1686 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1687 "access_mask=0x%x share_access=0x%x "
1688 "create_disposition = 0x%x create_options=0x%x "
1689 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1690 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1691 access_mask, share_access, create_disposition,
1692 create_options, (unsigned int)unx_mode, oplock_request,
1693 (unsigned int)private_flags));
1695 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1696 DEBUG(0, ("No smb request but not an internal only open!\n"));
1697 return NT_STATUS_INTERNAL_ERROR;
1701 * Only non-internal opens can be deferred at all
1704 if (req) {
1705 void *ptr;
1706 if (get_deferred_open_message_state(req,
1707 &request_time,
1708 &ptr)) {
1710 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1711 /* Remember the absolute time of the original
1712 request with this mid. We'll use it later to
1713 see if this has timed out. */
1715 /* Remove the deferred open entry under lock. */
1716 remove_deferred_open_entry(
1717 state->id, req->mid,
1718 messaging_server_id(req->sconn->msg_ctx));
1720 /* Ensure we don't reprocess this message. */
1721 remove_deferred_open_message_smb(req->sconn, req->mid);
1725 if (!posix_open) {
1726 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1727 if (file_existed) {
1728 existing_dos_attributes = dos_mode(conn, smb_fname);
1732 /* ignore any oplock requests if oplocks are disabled */
1733 if (!lp_oplocks(SNUM(conn)) ||
1734 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1735 /* Mask off everything except the private Samba bits. */
1736 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1739 /* this is for OS/2 long file names - say we don't support them */
1740 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1741 /* OS/2 Workplace shell fix may be main code stream in a later
1742 * release. */
1743 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1744 "supported.\n"));
1745 if (use_nt_status()) {
1746 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1748 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1751 switch( create_disposition ) {
1753 * Currently we're using FILE_SUPERSEDE as the same as
1754 * FILE_OVERWRITE_IF but they really are
1755 * different. FILE_SUPERSEDE deletes an existing file
1756 * (requiring delete access) then recreates it.
1758 case FILE_SUPERSEDE:
1759 /* If file exists replace/overwrite. If file doesn't
1760 * exist create. */
1761 flags2 |= (O_CREAT | O_TRUNC);
1762 clear_ads = true;
1763 break;
1765 case FILE_OVERWRITE_IF:
1766 /* If file exists replace/overwrite. If file doesn't
1767 * exist create. */
1768 flags2 |= (O_CREAT | O_TRUNC);
1769 clear_ads = true;
1770 break;
1772 case FILE_OPEN:
1773 /* If file exists open. If file doesn't exist error. */
1774 if (!file_existed) {
1775 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1776 "requested for file %s and file "
1777 "doesn't exist.\n",
1778 smb_fname_str_dbg(smb_fname)));
1779 errno = ENOENT;
1780 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1782 break;
1784 case FILE_OVERWRITE:
1785 /* If file exists overwrite. If file doesn't exist
1786 * error. */
1787 if (!file_existed) {
1788 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1789 "requested for file %s and file "
1790 "doesn't exist.\n",
1791 smb_fname_str_dbg(smb_fname) ));
1792 errno = ENOENT;
1793 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1795 flags2 |= O_TRUNC;
1796 clear_ads = true;
1797 break;
1799 case FILE_CREATE:
1800 /* If file exists error. If file doesn't exist
1801 * create. */
1802 if (file_existed) {
1803 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1804 "requested for file %s and file "
1805 "already exists.\n",
1806 smb_fname_str_dbg(smb_fname)));
1807 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1808 errno = EISDIR;
1809 } else {
1810 errno = EEXIST;
1812 return map_nt_error_from_unix(errno);
1814 flags2 |= (O_CREAT|O_EXCL);
1815 break;
1817 case FILE_OPEN_IF:
1818 /* If file exists open. If file doesn't exist
1819 * create. */
1820 flags2 |= O_CREAT;
1821 break;
1823 default:
1824 return NT_STATUS_INVALID_PARAMETER;
1827 /* We only care about matching attributes on file exists and
1828 * overwrite. */
1830 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1831 (create_disposition == FILE_OVERWRITE_IF))) {
1832 if (!open_match_attributes(conn, existing_dos_attributes,
1833 new_dos_attributes,
1834 smb_fname->st.st_ex_mode,
1835 unx_mode, &new_unx_mode)) {
1836 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1837 "for file %s (%x %x) (0%o, 0%o)\n",
1838 smb_fname_str_dbg(smb_fname),
1839 existing_dos_attributes,
1840 new_dos_attributes,
1841 (unsigned int)smb_fname->st.st_ex_mode,
1842 (unsigned int)unx_mode ));
1843 errno = EACCES;
1844 return NT_STATUS_ACCESS_DENIED;
1848 status = smbd_calculate_access_mask(conn, smb_fname,
1849 access_mask,
1850 &access_mask);
1851 if (!NT_STATUS_IS_OK(status)) {
1852 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1853 "on file %s returned %s\n",
1854 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1855 return status;
1858 open_access_mask = access_mask;
1860 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1861 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1864 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1865 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1866 access_mask));
1869 * Note that we ignore the append flag as append does not
1870 * mean the same thing under DOS and Unix.
1873 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1874 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1875 /* DENY_DOS opens are always underlying read-write on the
1876 file handle, no matter what the requested access mask
1877 says. */
1878 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1879 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1880 flags = O_RDWR;
1881 } else {
1882 flags = O_WRONLY;
1884 } else {
1885 flags = O_RDONLY;
1889 * Currently we only look at FILE_WRITE_THROUGH for create options.
1892 #if defined(O_SYNC)
1893 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1894 flags2 |= O_SYNC;
1896 #endif /* O_SYNC */
1898 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1899 flags2 |= O_APPEND;
1902 if (!posix_open && !CAN_WRITE(conn)) {
1904 * We should really return a permission denied error if either
1905 * O_CREAT or O_TRUNC are set, but for compatibility with
1906 * older versions of Samba we just AND them out.
1908 flags2 &= ~(O_CREAT|O_TRUNC);
1912 * Ensure we can't write on a read-only share or file.
1915 if (flags != O_RDONLY && file_existed &&
1916 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1917 DEBUG(5,("open_file_ntcreate: write access requested for "
1918 "file %s on read only %s\n",
1919 smb_fname_str_dbg(smb_fname),
1920 !CAN_WRITE(conn) ? "share" : "file" ));
1921 errno = EACCES;
1922 return NT_STATUS_ACCESS_DENIED;
1925 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1926 fsp->share_access = share_access;
1927 fsp->fh->private_options = private_flags;
1928 fsp->access_mask = open_access_mask; /* We change this to the
1929 * requested access_mask after
1930 * the open is done. */
1931 fsp->posix_open = posix_open;
1933 /* Ensure no SAMBA_PRIVATE bits can be set. */
1934 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1936 if (timeval_is_zero(&request_time)) {
1937 request_time = fsp->open_time;
1940 if (file_existed) {
1941 struct share_mode_entry *batch_entry = NULL;
1942 struct share_mode_entry *exclusive_entry = NULL;
1943 bool got_level2_oplock = false;
1944 bool got_a_none_oplock = false;
1945 struct file_id id;
1947 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1948 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1950 lck = get_share_mode_lock(talloc_tos(), id,
1951 conn->connectpath,
1952 smb_fname, &old_write_time);
1953 if (lck == NULL) {
1954 DEBUG(0, ("Could not get share mode lock\n"));
1955 return NT_STATUS_SHARING_VIOLATION;
1958 /* Get the types we need to examine. */
1959 find_oplock_types(fsp,
1960 oplock_request,
1961 lck,
1962 &batch_entry,
1963 &exclusive_entry,
1964 &got_level2_oplock,
1965 &got_a_none_oplock);
1967 /* First pass - send break only on batch oplocks. */
1968 if ((req != NULL) &&
1969 delay_for_batch_oplocks(fsp,
1970 req->mid,
1971 oplock_request,
1972 batch_entry)) {
1973 schedule_defer_open(lck, request_time, req);
1974 TALLOC_FREE(lck);
1975 return NT_STATUS_SHARING_VIOLATION;
1978 /* Use the client requested access mask here, not the one we
1979 * open with. */
1980 status = open_mode_check(conn, lck, fsp->name_hash,
1981 access_mask, share_access,
1982 create_options, &file_existed);
1984 if (NT_STATUS_IS_OK(status)) {
1985 /* We might be going to allow this open. Check oplock
1986 * status again. */
1987 /* Second pass - send break for both batch or
1988 * exclusive oplocks. */
1989 if ((req != NULL) &&
1990 delay_for_exclusive_oplocks(
1991 fsp,
1992 req->mid,
1993 oplock_request,
1994 exclusive_entry)) {
1995 schedule_defer_open(lck, request_time, req);
1996 TALLOC_FREE(lck);
1997 return NT_STATUS_SHARING_VIOLATION;
2001 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2002 /* DELETE_PENDING is not deferred for a second */
2003 TALLOC_FREE(lck);
2004 return status;
2007 grant_fsp_oplock_type(fsp,
2008 oplock_request,
2009 got_level2_oplock,
2010 got_a_none_oplock);
2012 if (!NT_STATUS_IS_OK(status)) {
2013 uint32 can_access_mask;
2014 bool can_access = True;
2016 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2018 /* Check if this can be done with the deny_dos and fcb
2019 * calls. */
2020 if (private_flags &
2021 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2022 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2023 if (req == NULL) {
2024 DEBUG(0, ("DOS open without an SMB "
2025 "request!\n"));
2026 TALLOC_FREE(lck);
2027 return NT_STATUS_INTERNAL_ERROR;
2030 /* Use the client requested access mask here,
2031 * not the one we open with. */
2032 status = fcb_or_dos_open(req,
2033 conn,
2034 fsp,
2035 smb_fname,
2037 req->smbpid,
2038 req->vuid,
2039 access_mask,
2040 share_access,
2041 create_options);
2043 if (NT_STATUS_IS_OK(status)) {
2044 TALLOC_FREE(lck);
2045 if (pinfo) {
2046 *pinfo = FILE_WAS_OPENED;
2048 return NT_STATUS_OK;
2053 * This next line is a subtlety we need for
2054 * MS-Access. If a file open will fail due to share
2055 * permissions and also for security (access) reasons,
2056 * we need to return the access failed error, not the
2057 * share error. We can't open the file due to kernel
2058 * oplock deadlock (it's possible we failed above on
2059 * the open_mode_check()) so use a userspace check.
2062 if (flags & O_RDWR) {
2063 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2064 } else if (flags & O_WRONLY) {
2065 can_access_mask = FILE_WRITE_DATA;
2066 } else {
2067 can_access_mask = FILE_READ_DATA;
2070 if (((can_access_mask & FILE_WRITE_DATA) &&
2071 !CAN_WRITE(conn)) ||
2072 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2073 smb_fname, can_access_mask))) {
2074 can_access = False;
2078 * If we're returning a share violation, ensure we
2079 * cope with the braindead 1 second delay.
2082 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2083 lp_defer_sharing_violations()) {
2084 struct timeval timeout;
2085 struct deferred_open_record state;
2086 int timeout_usecs;
2088 /* this is a hack to speed up torture tests
2089 in 'make test' */
2090 timeout_usecs = lp_parm_int(SNUM(conn),
2091 "smbd","sharedelay",
2092 SHARING_VIOLATION_USEC_WAIT);
2094 /* This is a relative time, added to the absolute
2095 request_time value to get the absolute timeout time.
2096 Note that if this is the second or greater time we enter
2097 this codepath for this particular request mid then
2098 request_time is left as the absolute time of the *first*
2099 time this request mid was processed. This is what allows
2100 the request to eventually time out. */
2102 timeout = timeval_set(0, timeout_usecs);
2104 /* Nothing actually uses state.delayed_for_oplocks
2105 but it's handy to differentiate in debug messages
2106 between a 30 second delay due to oplock break, and
2107 a 1 second delay for share mode conflicts. */
2109 state.delayed_for_oplocks = False;
2110 state.id = id;
2112 if ((req != NULL)
2113 && !request_timed_out(request_time,
2114 timeout)) {
2115 defer_open(lck, request_time, timeout,
2116 req, &state);
2120 TALLOC_FREE(lck);
2121 if (can_access) {
2123 * We have detected a sharing violation here
2124 * so return the correct error code
2126 status = NT_STATUS_SHARING_VIOLATION;
2127 } else {
2128 status = NT_STATUS_ACCESS_DENIED;
2130 return status;
2134 * We exit this block with the share entry *locked*.....
2138 SMB_ASSERT(!file_existed || (lck != NULL));
2141 * Ensure we pay attention to default ACLs on directories if required.
2144 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2145 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2146 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2149 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2150 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2151 (unsigned int)flags, (unsigned int)flags2,
2152 (unsigned int)unx_mode, (unsigned int)access_mask,
2153 (unsigned int)open_access_mask));
2156 * open_file strips any O_TRUNC flags itself.
2159 fsp_open = open_file(fsp, conn, req, parent_dir,
2160 flags|flags2, unx_mode, access_mask,
2161 open_access_mask);
2163 if (!NT_STATUS_IS_OK(fsp_open)) {
2164 TALLOC_FREE(lck);
2165 return fsp_open;
2168 if (!file_existed) {
2169 struct share_mode_entry *batch_entry = NULL;
2170 struct share_mode_entry *exclusive_entry = NULL;
2171 bool got_level2_oplock = false;
2172 bool got_a_none_oplock = false;
2173 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2174 struct file_id id;
2176 * Deal with the race condition where two smbd's detect the
2177 * file doesn't exist and do the create at the same time. One
2178 * of them will win and set a share mode, the other (ie. this
2179 * one) should check if the requested share mode for this
2180 * create is allowed.
2184 * Now the file exists and fsp is successfully opened,
2185 * fsp->dev and fsp->inode are valid and should replace the
2186 * dev=0,inode=0 from a non existent file. Spotted by
2187 * Nadav Danieli <nadavd@exanet.com>. JRA.
2190 id = fsp->file_id;
2192 lck = get_share_mode_lock(talloc_tos(), id,
2193 conn->connectpath,
2194 smb_fname, &old_write_time);
2196 if (lck == NULL) {
2197 DEBUG(0, ("open_file_ntcreate: Could not get share "
2198 "mode lock for %s\n",
2199 smb_fname_str_dbg(smb_fname)));
2200 fd_close(fsp);
2201 return NT_STATUS_SHARING_VIOLATION;
2204 /* Get the types we need to examine. */
2205 find_oplock_types(fsp,
2206 oplock_request,
2207 lck,
2208 &batch_entry,
2209 &exclusive_entry,
2210 &got_level2_oplock,
2211 &got_a_none_oplock);
2213 /* First pass - send break only on batch oplocks. */
2214 if ((req != NULL) &&
2215 delay_for_batch_oplocks(fsp,
2216 req->mid,
2217 oplock_request,
2218 batch_entry)) {
2219 schedule_defer_open(lck, request_time, req);
2220 TALLOC_FREE(lck);
2221 fd_close(fsp);
2222 return NT_STATUS_SHARING_VIOLATION;
2225 status = open_mode_check(conn, lck, fsp->name_hash,
2226 access_mask, share_access,
2227 create_options, &file_existed);
2229 if (NT_STATUS_IS_OK(status)) {
2230 /* We might be going to allow this open. Check oplock
2231 * status again. */
2232 /* Second pass - send break for both batch or
2233 * exclusive oplocks. */
2234 if ((req != NULL) &&
2235 delay_for_exclusive_oplocks(
2236 fsp,
2237 req->mid,
2238 oplock_request,
2239 exclusive_entry)) {
2240 schedule_defer_open(lck, request_time, req);
2241 TALLOC_FREE(lck);
2242 fd_close(fsp);
2243 return NT_STATUS_SHARING_VIOLATION;
2247 if (!NT_STATUS_IS_OK(status)) {
2248 struct deferred_open_record state;
2250 state.delayed_for_oplocks = False;
2251 state.id = id;
2253 /* Do it all over again immediately. In the second
2254 * round we will find that the file existed and handle
2255 * the DELETE_PENDING and FCB cases correctly. No need
2256 * to duplicate the code here. Essentially this is a
2257 * "goto top of this function", but don't tell
2258 * anybody... */
2260 if (req != NULL) {
2261 defer_open(lck, request_time, timeval_zero(),
2262 req, &state);
2264 TALLOC_FREE(lck);
2265 fd_close(fsp);
2266 return status;
2269 grant_fsp_oplock_type(fsp,
2270 oplock_request,
2271 got_level2_oplock,
2272 got_a_none_oplock);
2275 * We exit this block with the share entry *locked*.....
2280 SMB_ASSERT(lck != NULL);
2282 /* Delete streams if create_disposition requires it */
2283 if (file_existed && clear_ads &&
2284 !is_ntfs_stream_smb_fname(smb_fname)) {
2285 status = delete_all_streams(conn, smb_fname->base_name);
2286 if (!NT_STATUS_IS_OK(status)) {
2287 TALLOC_FREE(lck);
2288 fd_close(fsp);
2289 return status;
2293 /* note that we ignore failure for the following. It is
2294 basically a hack for NFS, and NFS will never set one of
2295 these only read them. Nobody but Samba can ever set a deny
2296 mode and we have already checked our more authoritative
2297 locking database for permission to set this deny mode. If
2298 the kernel refuses the operations then the kernel is wrong.
2299 note that GPFS supports it as well - jmcd */
2301 if (fsp->fh->fd != -1) {
2302 int ret_flock;
2303 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2304 if(ret_flock == -1 ){
2306 TALLOC_FREE(lck);
2307 fd_close(fsp);
2309 return NT_STATUS_SHARING_VIOLATION;
2314 * At this point onwards, we can guarentee that the share entry
2315 * is locked, whether we created the file or not, and that the
2316 * deny mode is compatible with all current opens.
2320 * If requested, truncate the file.
2323 if (file_existed && (flags2&O_TRUNC)) {
2325 * We are modifing the file after open - update the stat
2326 * struct..
2328 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2329 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2330 status = map_nt_error_from_unix(errno);
2331 TALLOC_FREE(lck);
2332 fd_close(fsp);
2333 return status;
2338 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2339 * but we don't have to store this - just ignore it on access check.
2341 if (conn->sconn->using_smb2) {
2343 * SMB2 doesn't return it (according to Microsoft tests).
2344 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2345 * File created with access = 0x7 (Read, Write, Delete)
2346 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2348 fsp->access_mask = access_mask;
2349 } else {
2350 /* But SMB1 does. */
2351 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2354 if (file_existed) {
2355 /* stat opens on existing files don't get oplocks. */
2356 if (is_stat_open(open_access_mask)) {
2357 fsp->oplock_type = NO_OPLOCK;
2360 if (flags2 & O_TRUNC) {
2361 info = FILE_WAS_OVERWRITTEN;
2362 } else {
2363 info = FILE_WAS_OPENED;
2365 } else {
2366 info = FILE_WAS_CREATED;
2369 if (pinfo) {
2370 *pinfo = info;
2374 * Setup the oplock info in both the shared memory and
2375 * file structs.
2378 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2380 * Could not get the kernel oplock or there are byte-range
2381 * locks on the file.
2383 fsp->oplock_type = NO_OPLOCK;
2386 set_share_mode(lck, fsp, get_current_uid(conn),
2387 req ? req->mid : 0,
2388 fsp->oplock_type);
2390 /* Handle strange delete on close create semantics. */
2391 if (create_options & FILE_DELETE_ON_CLOSE) {
2393 status = can_set_delete_on_close(fsp, new_dos_attributes);
2395 if (!NT_STATUS_IS_OK(status)) {
2396 /* Remember to delete the mode we just added. */
2397 del_share_mode(lck, fsp);
2398 TALLOC_FREE(lck);
2399 fd_close(fsp);
2400 return status;
2402 /* Note that here we set the *inital* delete on close flag,
2403 not the regular one. The magic gets handled in close. */
2404 fsp->initial_delete_on_close = True;
2407 if (info == FILE_WAS_OVERWRITTEN
2408 || info == FILE_WAS_CREATED
2409 || info == FILE_WAS_SUPERSEDED) {
2410 new_file_created = True;
2413 if (new_file_created) {
2414 /* Files should be initially set as archive */
2415 if (lp_map_archive(SNUM(conn)) ||
2416 lp_store_dos_attributes(SNUM(conn))) {
2417 if (!posix_open) {
2418 if (file_set_dosmode(conn, smb_fname,
2419 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2420 parent_dir, true) == 0) {
2421 unx_mode = smb_fname->st.st_ex_mode;
2427 /* Determine sparse flag. */
2428 if (posix_open) {
2429 /* POSIX opens are sparse by default. */
2430 fsp->is_sparse = true;
2431 } else {
2432 fsp->is_sparse = (file_existed &&
2433 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2437 * Take care of inherited ACLs on created files - if default ACL not
2438 * selected.
2441 if (!posix_open && !file_existed && !def_acl) {
2443 int saved_errno = errno; /* We might get ENOSYS in the next
2444 * call.. */
2446 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2447 errno == ENOSYS) {
2448 errno = saved_errno; /* Ignore ENOSYS */
2451 } else if (new_unx_mode) {
2453 int ret = -1;
2455 /* Attributes need changing. File already existed. */
2458 int saved_errno = errno; /* We might get ENOSYS in the
2459 * next call.. */
2460 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2462 if (ret == -1 && errno == ENOSYS) {
2463 errno = saved_errno; /* Ignore ENOSYS */
2464 } else {
2465 DEBUG(5, ("open_file_ntcreate: reset "
2466 "attributes of file %s to 0%o\n",
2467 smb_fname_str_dbg(smb_fname),
2468 (unsigned int)new_unx_mode));
2469 ret = 0; /* Don't do the fchmod below. */
2473 if ((ret == -1) &&
2474 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2475 DEBUG(5, ("open_file_ntcreate: failed to reset "
2476 "attributes of file %s to 0%o\n",
2477 smb_fname_str_dbg(smb_fname),
2478 (unsigned int)new_unx_mode));
2481 /* If this is a successful open, we must remove any deferred open
2482 * records. */
2483 if (req != NULL) {
2484 del_deferred_open_entry(lck, req->mid,
2485 messaging_server_id(req->sconn->msg_ctx));
2487 TALLOC_FREE(lck);
2489 return NT_STATUS_OK;
2493 /****************************************************************************
2494 Open a file for for write to ensure that we can fchmod it.
2495 ****************************************************************************/
2497 NTSTATUS open_file_fchmod(connection_struct *conn,
2498 struct smb_filename *smb_fname,
2499 files_struct **result)
2501 if (!VALID_STAT(smb_fname->st)) {
2502 return NT_STATUS_INVALID_PARAMETER;
2505 return SMB_VFS_CREATE_FILE(
2506 conn, /* conn */
2507 NULL, /* req */
2508 0, /* root_dir_fid */
2509 smb_fname, /* fname */
2510 FILE_WRITE_DATA, /* access_mask */
2511 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2512 FILE_SHARE_DELETE),
2513 FILE_OPEN, /* create_disposition*/
2514 0, /* create_options */
2515 0, /* file_attributes */
2516 INTERNAL_OPEN_ONLY, /* oplock_request */
2517 0, /* allocation_size */
2518 0, /* private_flags */
2519 NULL, /* sd */
2520 NULL, /* ea_list */
2521 result, /* result */
2522 NULL); /* pinfo */
2525 static NTSTATUS mkdir_internal(connection_struct *conn,
2526 struct smb_filename *smb_dname,
2527 uint32 file_attributes)
2529 mode_t mode;
2530 char *parent_dir = NULL;
2531 NTSTATUS status;
2532 bool posix_open = false;
2533 bool need_re_stat = false;
2534 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2536 if(access_mask & ~(conn->share_access)) {
2537 DEBUG(5,("mkdir_internal: failing share access "
2538 "%s\n", lp_servicename(SNUM(conn))));
2539 return NT_STATUS_ACCESS_DENIED;
2542 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2543 NULL)) {
2544 return NT_STATUS_NO_MEMORY;
2547 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2548 posix_open = true;
2549 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2550 } else {
2551 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2554 status = check_parent_access(conn,
2555 smb_dname,
2556 access_mask);
2557 if(!NT_STATUS_IS_OK(status)) {
2558 DEBUG(5,("mkdir_internal: check_parent_access "
2559 "on directory %s for path %s returned %s\n",
2560 parent_dir,
2561 smb_dname->base_name,
2562 nt_errstr(status) ));
2563 return status;
2566 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2567 return map_nt_error_from_unix(errno);
2570 /* Ensure we're checking for a symlink here.... */
2571 /* We don't want to get caught by a symlink racer. */
2573 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2574 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2575 smb_fname_str_dbg(smb_dname), strerror(errno)));
2576 return map_nt_error_from_unix(errno);
2579 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2580 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2581 smb_fname_str_dbg(smb_dname)));
2582 return NT_STATUS_NOT_A_DIRECTORY;
2585 if (lp_store_dos_attributes(SNUM(conn))) {
2586 if (!posix_open) {
2587 file_set_dosmode(conn, smb_dname,
2588 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2589 parent_dir, true);
2593 if (lp_inherit_perms(SNUM(conn))) {
2594 inherit_access_posix_acl(conn, parent_dir,
2595 smb_dname->base_name, mode);
2596 need_re_stat = true;
2599 if (!posix_open) {
2601 * Check if high bits should have been set,
2602 * then (if bits are missing): add them.
2603 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2604 * dir.
2606 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2607 (mode & ~smb_dname->st.st_ex_mode)) {
2608 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2609 (smb_dname->st.st_ex_mode |
2610 (mode & ~smb_dname->st.st_ex_mode)));
2611 need_re_stat = true;
2615 /* Change the owner if required. */
2616 if (lp_inherit_owner(SNUM(conn))) {
2617 change_dir_owner_to_parent(conn, parent_dir,
2618 smb_dname->base_name,
2619 &smb_dname->st);
2620 need_re_stat = true;
2623 if (need_re_stat) {
2624 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2625 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2626 smb_fname_str_dbg(smb_dname), strerror(errno)));
2627 return map_nt_error_from_unix(errno);
2631 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2632 smb_dname->base_name);
2634 return NT_STATUS_OK;
2637 /****************************************************************************
2638 Ensure we didn't get symlink raced on opening a directory.
2639 ****************************************************************************/
2641 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2642 const SMB_STRUCT_STAT *sbuf2)
2644 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2645 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2646 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2647 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2648 return false;
2650 return true;
2653 /****************************************************************************
2654 Open a directory from an NT SMB call.
2655 ****************************************************************************/
2657 static NTSTATUS open_directory(connection_struct *conn,
2658 struct smb_request *req,
2659 struct smb_filename *smb_dname,
2660 uint32 access_mask,
2661 uint32 share_access,
2662 uint32 create_disposition,
2663 uint32 create_options,
2664 uint32 file_attributes,
2665 int *pinfo,
2666 files_struct **result)
2668 files_struct *fsp = NULL;
2669 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2670 struct share_mode_lock *lck = NULL;
2671 NTSTATUS status;
2672 struct timespec mtimespec;
2673 int info = 0;
2675 if (is_ntfs_stream_smb_fname(smb_dname)) {
2676 DEBUG(2, ("open_directory: %s is a stream name!\n",
2677 smb_fname_str_dbg(smb_dname)));
2678 return NT_STATUS_NOT_A_DIRECTORY;
2681 /* Ensure we have a directory attribute. */
2682 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2684 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2685 "share_access = 0x%x create_options = 0x%x, "
2686 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2687 smb_fname_str_dbg(smb_dname),
2688 (unsigned int)access_mask,
2689 (unsigned int)share_access,
2690 (unsigned int)create_options,
2691 (unsigned int)create_disposition,
2692 (unsigned int)file_attributes));
2694 status = smbd_calculate_access_mask(conn, smb_dname,
2695 access_mask, &access_mask);
2696 if (!NT_STATUS_IS_OK(status)) {
2697 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2698 "on file %s returned %s\n",
2699 smb_fname_str_dbg(smb_dname),
2700 nt_errstr(status)));
2701 return status;
2704 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2705 !security_token_has_privilege(get_current_nttok(conn),
2706 SEC_PRIV_SECURITY)) {
2707 DEBUG(10, ("open_directory: open on %s "
2708 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2709 smb_fname_str_dbg(smb_dname)));
2710 return NT_STATUS_PRIVILEGE_NOT_HELD;
2713 switch( create_disposition ) {
2714 case FILE_OPEN:
2716 if (!dir_existed) {
2717 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2720 info = FILE_WAS_OPENED;
2721 break;
2723 case FILE_CREATE:
2725 /* If directory exists error. If directory doesn't
2726 * exist create. */
2728 if (dir_existed) {
2729 status = NT_STATUS_OBJECT_NAME_COLLISION;
2730 DEBUG(2, ("open_directory: unable to create "
2731 "%s. Error was %s\n",
2732 smb_fname_str_dbg(smb_dname),
2733 nt_errstr(status)));
2734 return status;
2737 status = mkdir_internal(conn, smb_dname,
2738 file_attributes);
2740 if (!NT_STATUS_IS_OK(status)) {
2741 DEBUG(2, ("open_directory: unable to create "
2742 "%s. Error was %s\n",
2743 smb_fname_str_dbg(smb_dname),
2744 nt_errstr(status)));
2745 return status;
2748 info = FILE_WAS_CREATED;
2749 break;
2751 case FILE_OPEN_IF:
2753 * If directory exists open. If directory doesn't
2754 * exist create.
2757 if (dir_existed) {
2758 status = NT_STATUS_OK;
2759 info = FILE_WAS_OPENED;
2760 } else {
2761 status = mkdir_internal(conn, smb_dname,
2762 file_attributes);
2764 if (NT_STATUS_IS_OK(status)) {
2765 info = FILE_WAS_CREATED;
2766 } else {
2767 /* Cope with create race. */
2768 if (!NT_STATUS_EQUAL(status,
2769 NT_STATUS_OBJECT_NAME_COLLISION)) {
2770 DEBUG(2, ("open_directory: unable to create "
2771 "%s. Error was %s\n",
2772 smb_fname_str_dbg(smb_dname),
2773 nt_errstr(status)));
2774 return status;
2776 info = FILE_WAS_OPENED;
2780 break;
2782 case FILE_SUPERSEDE:
2783 case FILE_OVERWRITE:
2784 case FILE_OVERWRITE_IF:
2785 default:
2786 DEBUG(5,("open_directory: invalid create_disposition "
2787 "0x%x for directory %s\n",
2788 (unsigned int)create_disposition,
2789 smb_fname_str_dbg(smb_dname)));
2790 return NT_STATUS_INVALID_PARAMETER;
2793 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2794 DEBUG(5,("open_directory: %s is not a directory !\n",
2795 smb_fname_str_dbg(smb_dname)));
2796 return NT_STATUS_NOT_A_DIRECTORY;
2799 if (info == FILE_WAS_OPENED) {
2800 status = smbd_check_access_rights(conn, smb_dname, access_mask);
2801 if (!NT_STATUS_IS_OK(status)) {
2802 DEBUG(10, ("open_directory: smbd_check_access_rights on "
2803 "file %s failed with %s\n",
2804 smb_fname_str_dbg(smb_dname),
2805 nt_errstr(status)));
2806 return status;
2810 status = file_new(req, conn, &fsp);
2811 if(!NT_STATUS_IS_OK(status)) {
2812 return status;
2816 * Setup the files_struct for it.
2819 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2820 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2821 fsp->file_pid = req ? req->smbpid : 0;
2822 fsp->can_lock = False;
2823 fsp->can_read = False;
2824 fsp->can_write = False;
2826 fsp->share_access = share_access;
2827 fsp->fh->private_options = 0;
2829 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2831 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2832 fsp->print_file = NULL;
2833 fsp->modified = False;
2834 fsp->oplock_type = NO_OPLOCK;
2835 fsp->sent_oplock_break = NO_BREAK_SENT;
2836 fsp->is_directory = True;
2837 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2838 status = fsp_set_smb_fname(fsp, smb_dname);
2839 if (!NT_STATUS_IS_OK(status)) {
2840 file_free(req, fsp);
2841 return status;
2844 mtimespec = smb_dname->st.st_ex_mtime;
2846 #ifdef O_DIRECTORY
2847 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2848 #else
2849 /* POSIX allows us to open a directory with O_RDONLY. */
2850 status = fd_open(conn, fsp, O_RDONLY, 0);
2851 #endif
2852 if (!NT_STATUS_IS_OK(status)) {
2853 DEBUG(5, ("open_directory: Could not open fd for "
2854 "%s (%s)\n",
2855 smb_fname_str_dbg(smb_dname),
2856 nt_errstr(status)));
2857 file_free(req, fsp);
2858 return status;
2861 status = vfs_stat_fsp(fsp);
2862 if (!NT_STATUS_IS_OK(status)) {
2863 fd_close(fsp);
2864 file_free(req, fsp);
2865 return status;
2868 /* Ensure there was no race condition. */
2869 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2870 DEBUG(5,("open_directory: stat struct differs for "
2871 "directory %s.\n",
2872 smb_fname_str_dbg(smb_dname)));
2873 fd_close(fsp);
2874 file_free(req, fsp);
2875 return NT_STATUS_ACCESS_DENIED;
2878 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2879 conn->connectpath, smb_dname,
2880 &mtimespec);
2882 if (lck == NULL) {
2883 DEBUG(0, ("open_directory: Could not get share mode lock for "
2884 "%s\n", smb_fname_str_dbg(smb_dname)));
2885 fd_close(fsp);
2886 file_free(req, fsp);
2887 return NT_STATUS_SHARING_VIOLATION;
2890 status = open_mode_check(conn, lck, fsp->name_hash,
2891 access_mask, share_access,
2892 create_options, &dir_existed);
2894 if (!NT_STATUS_IS_OK(status)) {
2895 TALLOC_FREE(lck);
2896 fd_close(fsp);
2897 file_free(req, fsp);
2898 return status;
2901 set_share_mode(lck, fsp, get_current_uid(conn),
2902 req ? req->mid : 0, NO_OPLOCK);
2904 /* For directories the delete on close bit at open time seems
2905 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2906 if (create_options & FILE_DELETE_ON_CLOSE) {
2907 status = can_set_delete_on_close(fsp, 0);
2908 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2909 TALLOC_FREE(lck);
2910 fd_close(fsp);
2911 file_free(req, fsp);
2912 return status;
2915 if (NT_STATUS_IS_OK(status)) {
2916 /* Note that here we set the *inital* delete on close flag,
2917 not the regular one. The magic gets handled in close. */
2918 fsp->initial_delete_on_close = True;
2922 TALLOC_FREE(lck);
2924 if (pinfo) {
2925 *pinfo = info;
2928 *result = fsp;
2929 return NT_STATUS_OK;
2932 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2933 struct smb_filename *smb_dname)
2935 NTSTATUS status;
2936 files_struct *fsp;
2938 status = SMB_VFS_CREATE_FILE(
2939 conn, /* conn */
2940 req, /* req */
2941 0, /* root_dir_fid */
2942 smb_dname, /* fname */
2943 FILE_READ_ATTRIBUTES, /* access_mask */
2944 FILE_SHARE_NONE, /* share_access */
2945 FILE_CREATE, /* create_disposition*/
2946 FILE_DIRECTORY_FILE, /* create_options */
2947 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2948 0, /* oplock_request */
2949 0, /* allocation_size */
2950 0, /* private_flags */
2951 NULL, /* sd */
2952 NULL, /* ea_list */
2953 &fsp, /* result */
2954 NULL); /* pinfo */
2956 if (NT_STATUS_IS_OK(status)) {
2957 close_file(req, fsp, NORMAL_CLOSE);
2960 return status;
2963 /****************************************************************************
2964 Receive notification that one of our open files has been renamed by another
2965 smbd process.
2966 ****************************************************************************/
2968 void msg_file_was_renamed(struct messaging_context *msg,
2969 void *private_data,
2970 uint32_t msg_type,
2971 struct server_id server_id,
2972 DATA_BLOB *data)
2974 files_struct *fsp;
2975 char *frm = (char *)data->data;
2976 struct file_id id;
2977 const char *sharepath;
2978 const char *base_name;
2979 const char *stream_name;
2980 struct smb_filename *smb_fname = NULL;
2981 size_t sp_len, bn_len;
2982 NTSTATUS status;
2983 struct smbd_server_connection *sconn =
2984 talloc_get_type_abort(private_data,
2985 struct smbd_server_connection);
2987 if (data->data == NULL
2988 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2989 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2990 (int)data->length));
2991 return;
2994 /* Unpack the message. */
2995 pull_file_id_24(frm, &id);
2996 sharepath = &frm[24];
2997 sp_len = strlen(sharepath);
2998 base_name = sharepath + sp_len + 1;
2999 bn_len = strlen(base_name);
3000 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3002 /* stream_name must always be NULL if there is no stream. */
3003 if (stream_name[0] == '\0') {
3004 stream_name = NULL;
3007 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3008 stream_name, NULL, &smb_fname);
3009 if (!NT_STATUS_IS_OK(status)) {
3010 return;
3013 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3014 "file_id %s\n",
3015 sharepath, smb_fname_str_dbg(smb_fname),
3016 file_id_string_tos(&id)));
3018 for(fsp = file_find_di_first(sconn, id); fsp;
3019 fsp = file_find_di_next(fsp)) {
3020 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3022 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3023 fsp->fnum, fsp_str_dbg(fsp),
3024 smb_fname_str_dbg(smb_fname)));
3025 status = fsp_set_smb_fname(fsp, smb_fname);
3026 if (!NT_STATUS_IS_OK(status)) {
3027 goto out;
3029 } else {
3030 /* TODO. JRA. */
3031 /* Now we have the complete path we can work out if this is
3032 actually within this share and adjust newname accordingly. */
3033 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3034 "not sharepath %s) "
3035 "fnum %d from %s -> %s\n",
3036 fsp->conn->connectpath,
3037 sharepath,
3038 fsp->fnum,
3039 fsp_str_dbg(fsp),
3040 smb_fname_str_dbg(smb_fname)));
3043 out:
3044 TALLOC_FREE(smb_fname);
3045 return;
3049 * If a main file is opened for delete, all streams need to be checked for
3050 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3051 * If that works, delete them all by setting the delete on close and close.
3054 NTSTATUS open_streams_for_delete(connection_struct *conn,
3055 const char *fname)
3057 struct stream_struct *stream_info = NULL;
3058 files_struct **streams = NULL;
3059 int i;
3060 unsigned int num_streams = 0;
3061 TALLOC_CTX *frame = talloc_stackframe();
3062 NTSTATUS status;
3064 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3065 &num_streams, &stream_info);
3067 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3068 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3069 DEBUG(10, ("no streams around\n"));
3070 TALLOC_FREE(frame);
3071 return NT_STATUS_OK;
3074 if (!NT_STATUS_IS_OK(status)) {
3075 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3076 nt_errstr(status)));
3077 goto fail;
3080 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3081 num_streams));
3083 if (num_streams == 0) {
3084 TALLOC_FREE(frame);
3085 return NT_STATUS_OK;
3088 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3089 if (streams == NULL) {
3090 DEBUG(0, ("talloc failed\n"));
3091 status = NT_STATUS_NO_MEMORY;
3092 goto fail;
3095 for (i=0; i<num_streams; i++) {
3096 struct smb_filename *smb_fname = NULL;
3098 if (strequal(stream_info[i].name, "::$DATA")) {
3099 streams[i] = NULL;
3100 continue;
3103 status = create_synthetic_smb_fname(talloc_tos(), fname,
3104 stream_info[i].name,
3105 NULL, &smb_fname);
3106 if (!NT_STATUS_IS_OK(status)) {
3107 goto fail;
3110 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3111 DEBUG(10, ("Unable to stat stream: %s\n",
3112 smb_fname_str_dbg(smb_fname)));
3115 status = SMB_VFS_CREATE_FILE(
3116 conn, /* conn */
3117 NULL, /* req */
3118 0, /* root_dir_fid */
3119 smb_fname, /* fname */
3120 DELETE_ACCESS, /* access_mask */
3121 (FILE_SHARE_READ | /* share_access */
3122 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3123 FILE_OPEN, /* create_disposition*/
3124 0, /* create_options */
3125 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3126 0, /* oplock_request */
3127 0, /* allocation_size */
3128 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3129 NULL, /* sd */
3130 NULL, /* ea_list */
3131 &streams[i], /* result */
3132 NULL); /* pinfo */
3134 if (!NT_STATUS_IS_OK(status)) {
3135 DEBUG(10, ("Could not open stream %s: %s\n",
3136 smb_fname_str_dbg(smb_fname),
3137 nt_errstr(status)));
3139 TALLOC_FREE(smb_fname);
3140 break;
3142 TALLOC_FREE(smb_fname);
3146 * don't touch the variable "status" beyond this point :-)
3149 for (i -= 1 ; i >= 0; i--) {
3150 if (streams[i] == NULL) {
3151 continue;
3154 DEBUG(10, ("Closing stream # %d, %s\n", i,
3155 fsp_str_dbg(streams[i])));
3156 close_file(NULL, streams[i], NORMAL_CLOSE);
3159 fail:
3160 TALLOC_FREE(frame);
3161 return status;
3164 /*********************************************************************
3165 Create a default ACL by inheriting from the parent. If no inheritance
3166 from the parent available, don't set anything. This will leave the actual
3167 permissions the new file or directory already got from the filesystem
3168 as the NT ACL when read.
3169 *********************************************************************/
3171 static NTSTATUS inherit_new_acl(files_struct *fsp)
3173 TALLOC_CTX *ctx = talloc_tos();
3174 char *parent_name = NULL;
3175 struct security_descriptor *parent_desc = NULL;
3176 NTSTATUS status = NT_STATUS_OK;
3177 struct security_descriptor *psd = NULL;
3178 struct dom_sid *owner_sid = NULL;
3179 struct dom_sid *group_sid = NULL;
3180 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3181 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3182 bool inheritable_components = false;
3183 size_t size = 0;
3185 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3186 return NT_STATUS_NO_MEMORY;
3189 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3190 parent_name,
3191 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3192 &parent_desc);
3193 if (!NT_STATUS_IS_OK(status)) {
3194 return status;
3197 inheritable_components = sd_has_inheritable_components(parent_desc,
3198 fsp->is_directory);
3200 if (!inheritable_components && !inherit_owner) {
3201 /* Nothing to inherit and not setting owner. */
3202 return NT_STATUS_OK;
3205 /* Create an inherited descriptor from the parent. */
3207 if (DEBUGLEVEL >= 10) {
3208 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3209 fsp_str_dbg(fsp) ));
3210 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3213 /* Inherit from parent descriptor if "inherit owner" set. */
3214 if (inherit_owner) {
3215 owner_sid = parent_desc->owner_sid;
3216 group_sid = parent_desc->group_sid;
3219 if (owner_sid == NULL) {
3220 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3222 if (group_sid == NULL) {
3223 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3226 status = se_create_child_secdesc(ctx,
3227 &psd,
3228 &size,
3229 parent_desc,
3230 owner_sid,
3231 group_sid,
3232 fsp->is_directory);
3233 if (!NT_STATUS_IS_OK(status)) {
3234 return status;
3237 /* If inheritable_components == false,
3238 se_create_child_secdesc()
3239 creates a security desriptor with a NULL dacl
3240 entry, but with SEC_DESC_DACL_PRESENT. We need
3241 to remove that flag. */
3243 if (!inheritable_components) {
3244 security_info_sent &= ~SECINFO_DACL;
3245 psd->type &= ~SEC_DESC_DACL_PRESENT;
3248 if (DEBUGLEVEL >= 10) {
3249 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3250 fsp_str_dbg(fsp) ));
3251 NDR_PRINT_DEBUG(security_descriptor, psd);
3254 if (inherit_owner) {
3255 /* We need to be root to force this. */
3256 become_root();
3258 status = SMB_VFS_FSET_NT_ACL(fsp,
3259 security_info_sent,
3260 psd);
3261 if (inherit_owner) {
3262 unbecome_root();
3264 return status;
3268 * Wrapper around open_file_ntcreate and open_directory
3271 static NTSTATUS create_file_unixpath(connection_struct *conn,
3272 struct smb_request *req,
3273 struct smb_filename *smb_fname,
3274 uint32_t access_mask,
3275 uint32_t share_access,
3276 uint32_t create_disposition,
3277 uint32_t create_options,
3278 uint32_t file_attributes,
3279 uint32_t oplock_request,
3280 uint64_t allocation_size,
3281 uint32_t private_flags,
3282 struct security_descriptor *sd,
3283 struct ea_list *ea_list,
3285 files_struct **result,
3286 int *pinfo)
3288 int info = FILE_WAS_OPENED;
3289 files_struct *base_fsp = NULL;
3290 files_struct *fsp = NULL;
3291 NTSTATUS status;
3293 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3294 "file_attributes = 0x%x, share_access = 0x%x, "
3295 "create_disposition = 0x%x create_options = 0x%x "
3296 "oplock_request = 0x%x private_flags = 0x%x "
3297 "ea_list = 0x%p, sd = 0x%p, "
3298 "fname = %s\n",
3299 (unsigned int)access_mask,
3300 (unsigned int)file_attributes,
3301 (unsigned int)share_access,
3302 (unsigned int)create_disposition,
3303 (unsigned int)create_options,
3304 (unsigned int)oplock_request,
3305 (unsigned int)private_flags,
3306 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3308 if (create_options & FILE_OPEN_BY_FILE_ID) {
3309 status = NT_STATUS_NOT_SUPPORTED;
3310 goto fail;
3313 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3314 status = NT_STATUS_INVALID_PARAMETER;
3315 goto fail;
3318 if (req == NULL) {
3319 oplock_request |= INTERNAL_OPEN_ONLY;
3322 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3323 && (access_mask & DELETE_ACCESS)
3324 && !is_ntfs_stream_smb_fname(smb_fname)) {
3326 * We can't open a file with DELETE access if any of the
3327 * streams is open without FILE_SHARE_DELETE
3329 status = open_streams_for_delete(conn, smb_fname->base_name);
3331 if (!NT_STATUS_IS_OK(status)) {
3332 goto fail;
3336 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3337 !security_token_has_privilege(get_current_nttok(conn),
3338 SEC_PRIV_SECURITY)) {
3339 DEBUG(10, ("create_file_unixpath: open on %s "
3340 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3341 smb_fname_str_dbg(smb_fname)));
3342 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3343 goto fail;
3346 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3347 && is_ntfs_stream_smb_fname(smb_fname)
3348 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3349 uint32 base_create_disposition;
3350 struct smb_filename *smb_fname_base = NULL;
3352 if (create_options & FILE_DIRECTORY_FILE) {
3353 status = NT_STATUS_NOT_A_DIRECTORY;
3354 goto fail;
3357 switch (create_disposition) {
3358 case FILE_OPEN:
3359 base_create_disposition = FILE_OPEN;
3360 break;
3361 default:
3362 base_create_disposition = FILE_OPEN_IF;
3363 break;
3366 /* Create an smb_filename with stream_name == NULL. */
3367 status = create_synthetic_smb_fname(talloc_tos(),
3368 smb_fname->base_name,
3369 NULL, NULL,
3370 &smb_fname_base);
3371 if (!NT_STATUS_IS_OK(status)) {
3372 goto fail;
3375 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3376 DEBUG(10, ("Unable to stat stream: %s\n",
3377 smb_fname_str_dbg(smb_fname_base)));
3380 /* Open the base file. */
3381 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3382 FILE_SHARE_READ
3383 | FILE_SHARE_WRITE
3384 | FILE_SHARE_DELETE,
3385 base_create_disposition,
3386 0, 0, 0, 0, 0, NULL, NULL,
3387 &base_fsp, NULL);
3388 TALLOC_FREE(smb_fname_base);
3390 if (!NT_STATUS_IS_OK(status)) {
3391 DEBUG(10, ("create_file_unixpath for base %s failed: "
3392 "%s\n", smb_fname->base_name,
3393 nt_errstr(status)));
3394 goto fail;
3396 /* we don't need to low level fd */
3397 fd_close(base_fsp);
3401 * If it's a request for a directory open, deal with it separately.
3404 if (create_options & FILE_DIRECTORY_FILE) {
3406 if (create_options & FILE_NON_DIRECTORY_FILE) {
3407 status = NT_STATUS_INVALID_PARAMETER;
3408 goto fail;
3411 /* Can't open a temp directory. IFS kit test. */
3412 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3413 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3414 status = NT_STATUS_INVALID_PARAMETER;
3415 goto fail;
3419 * We will get a create directory here if the Win32
3420 * app specified a security descriptor in the
3421 * CreateDirectory() call.
3424 oplock_request = 0;
3425 status = open_directory(
3426 conn, req, smb_fname, access_mask, share_access,
3427 create_disposition, create_options, file_attributes,
3428 &info, &fsp);
3429 } else {
3432 * Ordinary file case.
3435 status = file_new(req, conn, &fsp);
3436 if(!NT_STATUS_IS_OK(status)) {
3437 goto fail;
3440 status = fsp_set_smb_fname(fsp, smb_fname);
3441 if (!NT_STATUS_IS_OK(status)) {
3442 goto fail;
3446 * We're opening the stream element of a base_fsp
3447 * we already opened. Set up the base_fsp pointer.
3449 if (base_fsp) {
3450 fsp->base_fsp = base_fsp;
3453 status = open_file_ntcreate(conn,
3454 req,
3455 access_mask,
3456 share_access,
3457 create_disposition,
3458 create_options,
3459 file_attributes,
3460 oplock_request,
3461 private_flags,
3462 &info,
3463 fsp);
3465 if(!NT_STATUS_IS_OK(status)) {
3466 file_free(req, fsp);
3467 fsp = NULL;
3470 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3472 /* A stream open never opens a directory */
3474 if (base_fsp) {
3475 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3476 goto fail;
3480 * Fail the open if it was explicitly a non-directory
3481 * file.
3484 if (create_options & FILE_NON_DIRECTORY_FILE) {
3485 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3486 goto fail;
3489 oplock_request = 0;
3490 status = open_directory(
3491 conn, req, smb_fname, access_mask,
3492 share_access, create_disposition,
3493 create_options, file_attributes,
3494 &info, &fsp);
3498 if (!NT_STATUS_IS_OK(status)) {
3499 goto fail;
3502 fsp->base_fsp = base_fsp;
3504 if ((ea_list != NULL) &&
3505 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3506 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3507 if (!NT_STATUS_IS_OK(status)) {
3508 goto fail;
3512 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3513 status = NT_STATUS_ACCESS_DENIED;
3514 goto fail;
3517 /* Save the requested allocation size. */
3518 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3519 if (allocation_size
3520 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3521 fsp->initial_allocation_size = smb_roundup(
3522 fsp->conn, allocation_size);
3523 if (fsp->is_directory) {
3524 /* Can't set allocation size on a directory. */
3525 status = NT_STATUS_ACCESS_DENIED;
3526 goto fail;
3528 if (vfs_allocate_file_space(
3529 fsp, fsp->initial_allocation_size) == -1) {
3530 status = NT_STATUS_DISK_FULL;
3531 goto fail;
3533 } else {
3534 fsp->initial_allocation_size = smb_roundup(
3535 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3539 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3540 fsp->base_fsp == NULL) {
3541 if (sd != NULL) {
3543 * According to the MS documentation, the only time the security
3544 * descriptor is applied to the opened file is iff we *created* the
3545 * file; an existing file stays the same.
3547 * Also, it seems (from observation) that you can open the file with
3548 * any access mask but you can still write the sd. We need to override
3549 * the granted access before we call set_sd
3550 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3553 uint32_t sec_info_sent;
3554 uint32_t saved_access_mask = fsp->access_mask;
3556 sec_info_sent = get_sec_info(sd);
3558 fsp->access_mask = FILE_GENERIC_ALL;
3560 /* Convert all the generic bits. */
3561 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3562 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3564 if (sec_info_sent & (SECINFO_OWNER|
3565 SECINFO_GROUP|
3566 SECINFO_DACL|
3567 SECINFO_SACL)) {
3568 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3571 fsp->access_mask = saved_access_mask;
3573 if (!NT_STATUS_IS_OK(status)) {
3574 goto fail;
3576 } else if (lp_inherit_acls(SNUM(conn))) {
3577 /* Inherit from parent. Errors here are not fatal. */
3578 status = inherit_new_acl(fsp);
3579 if (!NT_STATUS_IS_OK(status)) {
3580 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3581 fsp_str_dbg(fsp),
3582 nt_errstr(status) ));
3587 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3589 *result = fsp;
3590 if (pinfo != NULL) {
3591 *pinfo = info;
3594 smb_fname->st = fsp->fsp_name->st;
3596 return NT_STATUS_OK;
3598 fail:
3599 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3601 if (fsp != NULL) {
3602 if (base_fsp && fsp->base_fsp == base_fsp) {
3604 * The close_file below will close
3605 * fsp->base_fsp.
3607 base_fsp = NULL;
3609 close_file(req, fsp, ERROR_CLOSE);
3610 fsp = NULL;
3612 if (base_fsp != NULL) {
3613 close_file(req, base_fsp, ERROR_CLOSE);
3614 base_fsp = NULL;
3616 return status;
3620 * Calculate the full path name given a relative fid.
3622 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3623 struct smb_request *req,
3624 uint16_t root_dir_fid,
3625 const struct smb_filename *smb_fname,
3626 struct smb_filename **smb_fname_out)
3628 files_struct *dir_fsp;
3629 char *parent_fname = NULL;
3630 char *new_base_name = NULL;
3631 NTSTATUS status;
3633 if (root_dir_fid == 0 || !smb_fname) {
3634 status = NT_STATUS_INTERNAL_ERROR;
3635 goto out;
3638 dir_fsp = file_fsp(req, root_dir_fid);
3640 if (dir_fsp == NULL) {
3641 status = NT_STATUS_INVALID_HANDLE;
3642 goto out;
3645 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3646 status = NT_STATUS_INVALID_HANDLE;
3647 goto out;
3650 if (!dir_fsp->is_directory) {
3653 * Check to see if this is a mac fork of some kind.
3656 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3657 is_ntfs_stream_smb_fname(smb_fname)) {
3658 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3659 goto out;
3663 we need to handle the case when we get a
3664 relative open relative to a file and the
3665 pathname is blank - this is a reopen!
3666 (hint from demyn plantenberg)
3669 status = NT_STATUS_INVALID_HANDLE;
3670 goto out;
3673 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3675 * We're at the toplevel dir, the final file name
3676 * must not contain ./, as this is filtered out
3677 * normally by srvstr_get_path and unix_convert
3678 * explicitly rejects paths containing ./.
3680 parent_fname = talloc_strdup(talloc_tos(), "");
3681 if (parent_fname == NULL) {
3682 status = NT_STATUS_NO_MEMORY;
3683 goto out;
3685 } else {
3686 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3689 * Copy in the base directory name.
3692 parent_fname = talloc_array(talloc_tos(), char,
3693 dir_name_len+2);
3694 if (parent_fname == NULL) {
3695 status = NT_STATUS_NO_MEMORY;
3696 goto out;
3698 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3699 dir_name_len+1);
3702 * Ensure it ends in a '/'.
3703 * We used TALLOC_SIZE +2 to add space for the '/'.
3706 if(dir_name_len
3707 && (parent_fname[dir_name_len-1] != '\\')
3708 && (parent_fname[dir_name_len-1] != '/')) {
3709 parent_fname[dir_name_len] = '/';
3710 parent_fname[dir_name_len+1] = '\0';
3714 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3715 smb_fname->base_name);
3716 if (new_base_name == NULL) {
3717 status = NT_STATUS_NO_MEMORY;
3718 goto out;
3721 status = filename_convert(req,
3722 conn,
3723 req->flags2 & FLAGS2_DFS_PATHNAMES,
3724 new_base_name,
3726 NULL,
3727 smb_fname_out);
3728 if (!NT_STATUS_IS_OK(status)) {
3729 goto out;
3732 out:
3733 TALLOC_FREE(parent_fname);
3734 TALLOC_FREE(new_base_name);
3735 return status;
3738 NTSTATUS create_file_default(connection_struct *conn,
3739 struct smb_request *req,
3740 uint16_t root_dir_fid,
3741 struct smb_filename *smb_fname,
3742 uint32_t access_mask,
3743 uint32_t share_access,
3744 uint32_t create_disposition,
3745 uint32_t create_options,
3746 uint32_t file_attributes,
3747 uint32_t oplock_request,
3748 uint64_t allocation_size,
3749 uint32_t private_flags,
3750 struct security_descriptor *sd,
3751 struct ea_list *ea_list,
3752 files_struct **result,
3753 int *pinfo)
3755 int info = FILE_WAS_OPENED;
3756 files_struct *fsp = NULL;
3757 NTSTATUS status;
3758 bool stream_name = false;
3760 DEBUG(10,("create_file: access_mask = 0x%x "
3761 "file_attributes = 0x%x, share_access = 0x%x, "
3762 "create_disposition = 0x%x create_options = 0x%x "
3763 "oplock_request = 0x%x "
3764 "private_flags = 0x%x "
3765 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3766 "fname = %s\n",
3767 (unsigned int)access_mask,
3768 (unsigned int)file_attributes,
3769 (unsigned int)share_access,
3770 (unsigned int)create_disposition,
3771 (unsigned int)create_options,
3772 (unsigned int)oplock_request,
3773 (unsigned int)private_flags,
3774 (unsigned int)root_dir_fid,
3775 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3778 * Calculate the filename from the root_dir_if if necessary.
3781 if (root_dir_fid != 0) {
3782 struct smb_filename *smb_fname_out = NULL;
3783 status = get_relative_fid_filename(conn, req, root_dir_fid,
3784 smb_fname, &smb_fname_out);
3785 if (!NT_STATUS_IS_OK(status)) {
3786 goto fail;
3788 smb_fname = smb_fname_out;
3792 * Check to see if this is a mac fork of some kind.
3795 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3796 if (stream_name) {
3797 enum FAKE_FILE_TYPE fake_file_type;
3799 fake_file_type = is_fake_file(smb_fname);
3801 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3804 * Here we go! support for changing the disk quotas
3805 * --metze
3807 * We need to fake up to open this MAGIC QUOTA file
3808 * and return a valid FID.
3810 * w2k close this file directly after openening xp
3811 * also tries a QUERY_FILE_INFO on the file and then
3812 * close it
3814 status = open_fake_file(req, conn, req->vuid,
3815 fake_file_type, smb_fname,
3816 access_mask, &fsp);
3817 if (!NT_STATUS_IS_OK(status)) {
3818 goto fail;
3821 ZERO_STRUCT(smb_fname->st);
3822 goto done;
3825 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3826 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3827 goto fail;
3831 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
3832 int ret;
3833 smb_fname->stream_name = NULL;
3834 /* We have to handle this error here. */
3835 if (create_options & FILE_DIRECTORY_FILE) {
3836 status = NT_STATUS_NOT_A_DIRECTORY;
3837 goto fail;
3839 if (lp_posix_pathnames()) {
3840 ret = SMB_VFS_LSTAT(conn, smb_fname);
3841 } else {
3842 ret = SMB_VFS_STAT(conn, smb_fname);
3845 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3846 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3847 goto fail;
3851 status = create_file_unixpath(
3852 conn, req, smb_fname, access_mask, share_access,
3853 create_disposition, create_options, file_attributes,
3854 oplock_request, allocation_size, private_flags,
3855 sd, ea_list,
3856 &fsp, &info);
3858 if (!NT_STATUS_IS_OK(status)) {
3859 goto fail;
3862 done:
3863 DEBUG(10, ("create_file: info=%d\n", info));
3865 *result = fsp;
3866 if (pinfo != NULL) {
3867 *pinfo = info;
3869 return NT_STATUS_OK;
3871 fail:
3872 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3874 if (fsp != NULL) {
3875 close_file(req, fsp, ERROR_CLOSE);
3876 fsp = NULL;
3878 return status;