s3: Remove an unused parameter from check_parent_access()
[Samba/gebeck_regimport.git] / source3 / smbd / open.c
blob543a6619e9dec881e3f028cfb233cd4dc1971406
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)) {
1027 return NT_STATUS_SHARING_VIOLATION;
1031 return NT_STATUS_OK;
1034 static bool is_delete_request(files_struct *fsp) {
1035 return ((fsp->access_mask == DELETE_ACCESS) &&
1036 (fsp->oplock_type == NO_OPLOCK));
1040 * Send a break message to the oplock holder and delay the open for
1041 * our client.
1044 static NTSTATUS send_break_message(files_struct *fsp,
1045 struct share_mode_entry *exclusive,
1046 uint64_t mid,
1047 int oplock_request)
1049 NTSTATUS status;
1050 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1052 DEBUG(10, ("Sending break request to PID %s\n",
1053 procid_str_static(&exclusive->pid)));
1054 exclusive->op_mid = mid;
1056 /* Create the message. */
1057 share_mode_entry_to_message(msg, exclusive);
1059 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1060 don't want this set in the share mode struct pointed to by lck. */
1062 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1063 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1064 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1067 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1068 MSG_SMB_BREAK_REQUEST,
1069 (uint8 *)msg,
1070 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1071 if (!NT_STATUS_IS_OK(status)) {
1072 DEBUG(3, ("Could not send oplock break message: %s\n",
1073 nt_errstr(status)));
1076 return status;
1080 * Return share_mode_entry pointers for :
1081 * 1). Batch oplock entry.
1082 * 2). Batch or exclusive oplock entry (may be identical to #1).
1083 * bool have_level2_oplock
1084 * bool have_no_oplock.
1085 * Do internal consistency checks on the share mode for a file.
1088 static void find_oplock_types(files_struct *fsp,
1089 int oplock_request,
1090 const struct share_mode_lock *lck,
1091 struct share_mode_entry **pp_batch,
1092 struct share_mode_entry **pp_ex_or_batch,
1093 bool *got_level2,
1094 bool *got_no_oplock)
1096 int i;
1098 *pp_batch = NULL;
1099 *pp_ex_or_batch = NULL;
1100 *got_level2 = false;
1101 *got_no_oplock = false;
1103 /* Ignore stat or internal opens, as is done in
1104 delay_for_batch_oplocks() and
1105 delay_for_exclusive_oplocks().
1107 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1108 return;
1111 for (i=0; i<lck->data->num_share_modes; i++) {
1112 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1113 continue;
1116 if (lck->data->share_modes[i].op_type == NO_OPLOCK &&
1117 is_stat_open(lck->data->share_modes[i].access_mask)) {
1118 /* We ignore stat opens in the table - they
1119 always have NO_OPLOCK and never get or
1120 cause breaks. JRA. */
1121 continue;
1124 if (BATCH_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1125 /* batch - can only be one. */
1126 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1127 smb_panic("Bad batch oplock entry.");
1129 *pp_batch = &lck->data->share_modes[i];
1132 if (EXCLUSIVE_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1133 /* Exclusive or batch - can only be one. */
1134 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1135 smb_panic("Bad exclusive or batch oplock entry.");
1137 *pp_ex_or_batch = &lck->data->share_modes[i];
1140 if (LEVEL_II_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1141 if (*pp_batch || *pp_ex_or_batch) {
1142 smb_panic("Bad levelII oplock entry.");
1144 *got_level2 = true;
1147 if (lck->data->share_modes[i].op_type == NO_OPLOCK) {
1148 if (*pp_batch || *pp_ex_or_batch) {
1149 smb_panic("Bad no oplock entry.");
1151 *got_no_oplock = true;
1156 static bool delay_for_batch_oplocks(files_struct *fsp,
1157 uint64_t mid,
1158 int oplock_request,
1159 struct share_mode_entry *batch_entry)
1161 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1162 return false;
1164 if (batch_entry == NULL) {
1165 return false;
1168 /* Found a batch oplock */
1169 send_break_message(fsp, batch_entry, mid, oplock_request);
1170 return true;
1173 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1174 uint64_t mid,
1175 int oplock_request,
1176 struct share_mode_entry *ex_entry)
1178 bool delay_it;
1180 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1181 return false;
1183 if (ex_entry == NULL) {
1184 return false;
1187 /* Found an exclusive or batch oplock */
1189 delay_it = is_delete_request(fsp) ?
1190 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1192 if (!delay_it) {
1193 return false;
1196 send_break_message(fsp, ex_entry, mid, oplock_request);
1197 return true;
1200 static bool file_has_brlocks(files_struct *fsp)
1202 struct byte_range_lock *br_lck;
1204 br_lck = brl_get_locks_readonly(fsp);
1205 if (!br_lck)
1206 return false;
1208 return br_lck->num_locks > 0 ? true : false;
1211 static void grant_fsp_oplock_type(files_struct *fsp,
1212 int oplock_request,
1213 bool got_level2_oplock,
1214 bool got_a_none_oplock)
1216 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1217 lp_level2_oplocks(SNUM(fsp->conn));
1219 /* Start by granting what the client asked for,
1220 but ensure no SAMBA_PRIVATE bits can be set. */
1221 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1223 if (oplock_request & INTERNAL_OPEN_ONLY) {
1224 /* No oplocks on internal open. */
1225 fsp->oplock_type = NO_OPLOCK;
1226 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1227 fsp->oplock_type, fsp_str_dbg(fsp)));
1228 return;
1229 } else if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1230 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1231 fsp_str_dbg(fsp)));
1232 fsp->oplock_type = NO_OPLOCK;
1235 if (is_stat_open(fsp->access_mask)) {
1236 /* Leave the value already set. */
1237 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1238 fsp->oplock_type, fsp_str_dbg(fsp)));
1239 return;
1243 * Match what was requested (fsp->oplock_type) with
1244 * what was found in the existing share modes.
1247 if (got_a_none_oplock) {
1248 fsp->oplock_type = NO_OPLOCK;
1249 } else if (got_level2_oplock) {
1250 if (fsp->oplock_type == NO_OPLOCK ||
1251 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1252 /* Store a level2 oplock, but don't tell the client */
1253 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1254 } else {
1255 fsp->oplock_type = LEVEL_II_OPLOCK;
1257 } else {
1258 /* All share_mode_entries are placeholders or deferred.
1259 * Silently upgrade to fake levelII if the client didn't
1260 * ask for an oplock. */
1261 if (fsp->oplock_type == NO_OPLOCK) {
1262 /* Store a level2 oplock, but don't tell the client */
1263 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1268 * Don't grant level2 to clients that don't want them
1269 * or if we've turned them off.
1271 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1272 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1275 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1276 fsp->oplock_type, fsp_str_dbg(fsp)));
1279 bool request_timed_out(struct timeval request_time,
1280 struct timeval timeout)
1282 struct timeval now, end_time;
1283 GetTimeOfDay(&now);
1284 end_time = timeval_sum(&request_time, &timeout);
1285 return (timeval_compare(&end_time, &now) < 0);
1288 /****************************************************************************
1289 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1290 ****************************************************************************/
1292 static void defer_open(struct share_mode_lock *lck,
1293 struct timeval request_time,
1294 struct timeval timeout,
1295 struct smb_request *req,
1296 struct deferred_open_record *state)
1298 int i;
1300 /* Paranoia check */
1302 for (i=0; i<lck->data->num_share_modes; i++) {
1303 struct share_mode_entry *e = &lck->data->share_modes[i];
1305 if (is_deferred_open_entry(e) &&
1306 procid_is_me(&e->pid) &&
1307 (e->op_mid == req->mid)) {
1308 DEBUG(0, ("Trying to defer an already deferred "
1309 "request: mid=%llu, exiting\n",
1310 (unsigned long long)req->mid));
1311 exit_server("attempt to defer a deferred request");
1315 /* End paranoia check */
1317 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1318 "open entry for mid %llu\n",
1319 (unsigned int)request_time.tv_sec,
1320 (unsigned int)request_time.tv_usec,
1321 (unsigned long long)req->mid));
1323 if (!push_deferred_open_message_smb(req, request_time, timeout,
1324 state->id, (char *)state, sizeof(*state))) {
1325 exit_server("push_deferred_open_message_smb failed");
1327 add_deferred_open(lck, req->mid, request_time,
1328 messaging_server_id(req->sconn->msg_ctx), state->id);
1332 /****************************************************************************
1333 On overwrite open ensure that the attributes match.
1334 ****************************************************************************/
1336 bool open_match_attributes(connection_struct *conn,
1337 uint32 old_dos_attr,
1338 uint32 new_dos_attr,
1339 mode_t existing_unx_mode,
1340 mode_t new_unx_mode,
1341 mode_t *returned_unx_mode)
1343 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1345 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1346 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1348 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1349 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1350 *returned_unx_mode = new_unx_mode;
1351 } else {
1352 *returned_unx_mode = (mode_t)0;
1355 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1356 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1357 "returned_unx_mode = 0%o\n",
1358 (unsigned int)old_dos_attr,
1359 (unsigned int)existing_unx_mode,
1360 (unsigned int)new_dos_attr,
1361 (unsigned int)*returned_unx_mode ));
1363 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1364 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1365 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1366 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1367 return False;
1370 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1371 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1372 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1373 return False;
1376 return True;
1379 /****************************************************************************
1380 Special FCB or DOS processing in the case of a sharing violation.
1381 Try and find a duplicated file handle.
1382 ****************************************************************************/
1384 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1385 connection_struct *conn,
1386 files_struct *fsp_to_dup_into,
1387 const struct smb_filename *smb_fname,
1388 struct file_id id,
1389 uint16 file_pid,
1390 uint16 vuid,
1391 uint32 access_mask,
1392 uint32 share_access,
1393 uint32 create_options)
1395 files_struct *fsp;
1397 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1398 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1400 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1401 fsp = file_find_di_next(fsp)) {
1403 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1404 "vuid = %u, file_pid = %u, private_options = 0x%x "
1405 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1406 fsp->fh->fd, (unsigned int)fsp->vuid,
1407 (unsigned int)fsp->file_pid,
1408 (unsigned int)fsp->fh->private_options,
1409 (unsigned int)fsp->access_mask ));
1411 if (fsp->fh->fd != -1 &&
1412 fsp->vuid == vuid &&
1413 fsp->file_pid == file_pid &&
1414 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1415 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1416 (fsp->access_mask & FILE_WRITE_DATA) &&
1417 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1418 strequal(fsp->fsp_name->stream_name,
1419 smb_fname->stream_name)) {
1420 DEBUG(10,("fcb_or_dos_open: file match\n"));
1421 break;
1425 if (!fsp) {
1426 return NT_STATUS_NOT_FOUND;
1429 /* quite an insane set of semantics ... */
1430 if (is_executable(smb_fname->base_name) &&
1431 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1432 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1433 return NT_STATUS_INVALID_PARAMETER;
1436 /* We need to duplicate this fsp. */
1437 return dup_file_fsp(req, fsp, access_mask, share_access,
1438 create_options, fsp_to_dup_into);
1441 static void schedule_defer_open(struct share_mode_lock *lck,
1442 struct timeval request_time,
1443 struct smb_request *req)
1445 struct deferred_open_record state;
1447 /* This is a relative time, added to the absolute
1448 request_time value to get the absolute timeout time.
1449 Note that if this is the second or greater time we enter
1450 this codepath for this particular request mid then
1451 request_time is left as the absolute time of the *first*
1452 time this request mid was processed. This is what allows
1453 the request to eventually time out. */
1455 struct timeval timeout;
1457 /* Normally the smbd we asked should respond within
1458 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1459 * the client did, give twice the timeout as a safety
1460 * measure here in case the other smbd is stuck
1461 * somewhere else. */
1463 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1465 /* Nothing actually uses state.delayed_for_oplocks
1466 but it's handy to differentiate in debug messages
1467 between a 30 second delay due to oplock break, and
1468 a 1 second delay for share mode conflicts. */
1470 state.delayed_for_oplocks = True;
1471 state.id = lck->data->id;
1473 if (!request_timed_out(request_time, timeout)) {
1474 defer_open(lck, request_time, timeout, req, &state);
1478 /****************************************************************************
1479 Work out what access_mask to use from what the client sent us.
1480 ****************************************************************************/
1482 static NTSTATUS smbd_calculate_maximum_allowed_access(
1483 connection_struct *conn,
1484 const struct smb_filename *smb_fname,
1485 uint32_t *p_access_mask)
1487 struct security_descriptor *sd;
1488 uint32_t access_granted;
1489 NTSTATUS status;
1491 if (get_current_uid(conn) == (uid_t)0) {
1492 *p_access_mask |= FILE_GENERIC_ALL;
1493 return NT_STATUS_OK;
1496 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1497 (SECINFO_OWNER |
1498 SECINFO_GROUP |
1499 SECINFO_DACL),&sd);
1501 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1503 * File did not exist
1505 *p_access_mask = FILE_GENERIC_ALL;
1506 return NT_STATUS_OK;
1508 if (!NT_STATUS_IS_OK(status)) {
1509 DEBUG(10,("smbd_calculate_access_mask: "
1510 "Could not get acl on file %s: %s\n",
1511 smb_fname_str_dbg(smb_fname),
1512 nt_errstr(status)));
1513 return NT_STATUS_ACCESS_DENIED;
1517 * Never test FILE_READ_ATTRIBUTES. se_access_check()
1518 * also takes care of owner WRITE_DAC and READ_CONTROL.
1520 status = se_access_check(sd,
1521 get_current_nttok(conn),
1522 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1523 &access_granted);
1525 TALLOC_FREE(sd);
1527 if (!NT_STATUS_IS_OK(status)) {
1528 DEBUG(10, ("smbd_calculate_access_mask: "
1529 "Access denied on file %s: "
1530 "when calculating maximum access\n",
1531 smb_fname_str_dbg(smb_fname)));
1532 return NT_STATUS_ACCESS_DENIED;
1534 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1535 return NT_STATUS_OK;
1538 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1539 const struct smb_filename *smb_fname,
1540 uint32_t access_mask,
1541 uint32_t *access_mask_out)
1543 NTSTATUS status;
1544 uint32_t orig_access_mask = access_mask;
1545 uint32_t rejected_share_access;
1548 * Convert GENERIC bits to specific bits.
1551 se_map_generic(&access_mask, &file_generic_mapping);
1553 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1554 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1556 status = smbd_calculate_maximum_allowed_access(
1557 conn, smb_fname, &access_mask);
1559 if (!NT_STATUS_IS_OK(status)) {
1560 return status;
1563 access_mask &= conn->share_access;
1566 rejected_share_access = access_mask & ~(conn->share_access);
1568 if (rejected_share_access) {
1569 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1570 "file %s: rejected by share access mask[0x%08X] "
1571 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1572 smb_fname_str_dbg(smb_fname),
1573 conn->share_access,
1574 orig_access_mask, access_mask,
1575 rejected_share_access));
1576 return NT_STATUS_ACCESS_DENIED;
1579 *access_mask_out = access_mask;
1580 return NT_STATUS_OK;
1583 /****************************************************************************
1584 Remove the deferred open entry under lock.
1585 ****************************************************************************/
1587 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1588 struct server_id pid)
1590 struct share_mode_lock *lck = get_existing_share_mode_lock(
1591 talloc_tos(), id);
1592 if (lck == NULL) {
1593 DEBUG(0, ("could not get share mode lock\n"));
1594 return;
1596 del_deferred_open_entry(lck, mid, pid);
1597 TALLOC_FREE(lck);
1600 /****************************************************************************
1601 Open a file with a share mode. Passed in an already created files_struct *.
1602 ****************************************************************************/
1604 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1605 struct smb_request *req,
1606 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1607 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1608 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1609 uint32 create_options, /* options such as delete on close. */
1610 uint32 new_dos_attributes, /* attributes used for new file. */
1611 int oplock_request, /* internal Samba oplock codes. */
1612 /* Information (FILE_EXISTS etc.) */
1613 uint32_t private_flags, /* Samba specific flags. */
1614 int *pinfo,
1615 files_struct *fsp)
1617 struct smb_filename *smb_fname = fsp->fsp_name;
1618 int flags=0;
1619 int flags2=0;
1620 bool file_existed = VALID_STAT(smb_fname->st);
1621 bool def_acl = False;
1622 bool posix_open = False;
1623 bool new_file_created = False;
1624 bool clear_ads = false;
1625 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1626 mode_t new_unx_mode = (mode_t)0;
1627 mode_t unx_mode = (mode_t)0;
1628 int info;
1629 uint32 existing_dos_attributes = 0;
1630 struct timeval request_time = timeval_zero();
1631 struct share_mode_lock *lck = NULL;
1632 uint32 open_access_mask = access_mask;
1633 NTSTATUS status;
1634 char *parent_dir;
1636 if (conn->printer) {
1638 * Printers are handled completely differently.
1639 * Most of the passed parameters are ignored.
1642 if (pinfo) {
1643 *pinfo = FILE_WAS_CREATED;
1646 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1647 smb_fname_str_dbg(smb_fname)));
1649 if (!req) {
1650 DEBUG(0,("open_file_ntcreate: printer open without "
1651 "an SMB request!\n"));
1652 return NT_STATUS_INTERNAL_ERROR;
1655 return print_spool_open(fsp, smb_fname->base_name,
1656 req->vuid);
1659 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1660 NULL)) {
1661 return NT_STATUS_NO_MEMORY;
1664 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1665 posix_open = True;
1666 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1667 new_dos_attributes = 0;
1668 } else {
1669 /* Windows allows a new file to be created and
1670 silently removes a FILE_ATTRIBUTE_DIRECTORY
1671 sent by the client. Do the same. */
1673 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1675 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1676 * created new. */
1677 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1678 smb_fname, parent_dir);
1681 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1682 "access_mask=0x%x share_access=0x%x "
1683 "create_disposition = 0x%x create_options=0x%x "
1684 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1685 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1686 access_mask, share_access, create_disposition,
1687 create_options, (unsigned int)unx_mode, oplock_request,
1688 (unsigned int)private_flags));
1690 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1691 DEBUG(0, ("No smb request but not an internal only open!\n"));
1692 return NT_STATUS_INTERNAL_ERROR;
1696 * Only non-internal opens can be deferred at all
1699 if (req) {
1700 void *ptr;
1701 if (get_deferred_open_message_state(req,
1702 &request_time,
1703 &ptr)) {
1705 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1706 /* Remember the absolute time of the original
1707 request with this mid. We'll use it later to
1708 see if this has timed out. */
1710 /* Remove the deferred open entry under lock. */
1711 remove_deferred_open_entry(
1712 state->id, req->mid,
1713 messaging_server_id(req->sconn->msg_ctx));
1715 /* Ensure we don't reprocess this message. */
1716 remove_deferred_open_message_smb(req->sconn, req->mid);
1720 if (!posix_open) {
1721 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1722 if (file_existed) {
1723 existing_dos_attributes = dos_mode(conn, smb_fname);
1727 /* ignore any oplock requests if oplocks are disabled */
1728 if (!lp_oplocks(SNUM(conn)) ||
1729 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1730 /* Mask off everything except the private Samba bits. */
1731 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1734 /* this is for OS/2 long file names - say we don't support them */
1735 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1736 /* OS/2 Workplace shell fix may be main code stream in a later
1737 * release. */
1738 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1739 "supported.\n"));
1740 if (use_nt_status()) {
1741 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1743 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1746 switch( create_disposition ) {
1748 * Currently we're using FILE_SUPERSEDE as the same as
1749 * FILE_OVERWRITE_IF but they really are
1750 * different. FILE_SUPERSEDE deletes an existing file
1751 * (requiring delete access) then recreates it.
1753 case FILE_SUPERSEDE:
1754 /* If file exists replace/overwrite. If file doesn't
1755 * exist create. */
1756 flags2 |= (O_CREAT | O_TRUNC);
1757 clear_ads = true;
1758 break;
1760 case FILE_OVERWRITE_IF:
1761 /* If file exists replace/overwrite. If file doesn't
1762 * exist create. */
1763 flags2 |= (O_CREAT | O_TRUNC);
1764 clear_ads = true;
1765 break;
1767 case FILE_OPEN:
1768 /* If file exists open. If file doesn't exist error. */
1769 if (!file_existed) {
1770 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1771 "requested for file %s and file "
1772 "doesn't exist.\n",
1773 smb_fname_str_dbg(smb_fname)));
1774 errno = ENOENT;
1775 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1777 break;
1779 case FILE_OVERWRITE:
1780 /* If file exists overwrite. If file doesn't exist
1781 * error. */
1782 if (!file_existed) {
1783 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1784 "requested for file %s and file "
1785 "doesn't exist.\n",
1786 smb_fname_str_dbg(smb_fname) ));
1787 errno = ENOENT;
1788 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1790 flags2 |= O_TRUNC;
1791 clear_ads = true;
1792 break;
1794 case FILE_CREATE:
1795 /* If file exists error. If file doesn't exist
1796 * create. */
1797 if (file_existed) {
1798 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1799 "requested for file %s and file "
1800 "already exists.\n",
1801 smb_fname_str_dbg(smb_fname)));
1802 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1803 errno = EISDIR;
1804 } else {
1805 errno = EEXIST;
1807 return map_nt_error_from_unix(errno);
1809 flags2 |= (O_CREAT|O_EXCL);
1810 break;
1812 case FILE_OPEN_IF:
1813 /* If file exists open. If file doesn't exist
1814 * create. */
1815 flags2 |= O_CREAT;
1816 break;
1818 default:
1819 return NT_STATUS_INVALID_PARAMETER;
1822 /* We only care about matching attributes on file exists and
1823 * overwrite. */
1825 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1826 (create_disposition == FILE_OVERWRITE_IF))) {
1827 if (!open_match_attributes(conn, existing_dos_attributes,
1828 new_dos_attributes,
1829 smb_fname->st.st_ex_mode,
1830 unx_mode, &new_unx_mode)) {
1831 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1832 "for file %s (%x %x) (0%o, 0%o)\n",
1833 smb_fname_str_dbg(smb_fname),
1834 existing_dos_attributes,
1835 new_dos_attributes,
1836 (unsigned int)smb_fname->st.st_ex_mode,
1837 (unsigned int)unx_mode ));
1838 errno = EACCES;
1839 return NT_STATUS_ACCESS_DENIED;
1843 status = smbd_calculate_access_mask(conn, smb_fname,
1844 access_mask,
1845 &access_mask);
1846 if (!NT_STATUS_IS_OK(status)) {
1847 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1848 "on file %s returned %s\n",
1849 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1850 return status;
1853 open_access_mask = access_mask;
1855 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1856 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1859 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1860 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1861 access_mask));
1864 * Note that we ignore the append flag as append does not
1865 * mean the same thing under DOS and Unix.
1868 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1869 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1870 /* DENY_DOS opens are always underlying read-write on the
1871 file handle, no matter what the requested access mask
1872 says. */
1873 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1874 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1875 flags = O_RDWR;
1876 } else {
1877 flags = O_WRONLY;
1879 } else {
1880 flags = O_RDONLY;
1884 * Currently we only look at FILE_WRITE_THROUGH for create options.
1887 #if defined(O_SYNC)
1888 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1889 flags2 |= O_SYNC;
1891 #endif /* O_SYNC */
1893 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1894 flags2 |= O_APPEND;
1897 if (!posix_open && !CAN_WRITE(conn)) {
1899 * We should really return a permission denied error if either
1900 * O_CREAT or O_TRUNC are set, but for compatibility with
1901 * older versions of Samba we just AND them out.
1903 flags2 &= ~(O_CREAT|O_TRUNC);
1907 * Ensure we can't write on a read-only share or file.
1910 if (flags != O_RDONLY && file_existed &&
1911 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1912 DEBUG(5,("open_file_ntcreate: write access requested for "
1913 "file %s on read only %s\n",
1914 smb_fname_str_dbg(smb_fname),
1915 !CAN_WRITE(conn) ? "share" : "file" ));
1916 errno = EACCES;
1917 return NT_STATUS_ACCESS_DENIED;
1920 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1921 fsp->share_access = share_access;
1922 fsp->fh->private_options = private_flags;
1923 fsp->access_mask = open_access_mask; /* We change this to the
1924 * requested access_mask after
1925 * the open is done. */
1926 fsp->posix_open = posix_open;
1928 /* Ensure no SAMBA_PRIVATE bits can be set. */
1929 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1931 if (timeval_is_zero(&request_time)) {
1932 request_time = fsp->open_time;
1935 if (file_existed) {
1936 struct share_mode_entry *batch_entry = NULL;
1937 struct share_mode_entry *exclusive_entry = NULL;
1938 bool got_level2_oplock = false;
1939 bool got_a_none_oplock = false;
1940 struct file_id id;
1942 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1943 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1945 lck = get_share_mode_lock(talloc_tos(), id,
1946 conn->connectpath,
1947 smb_fname, &old_write_time);
1948 if (lck == NULL) {
1949 DEBUG(0, ("Could not get share mode lock\n"));
1950 return NT_STATUS_SHARING_VIOLATION;
1953 /* Get the types we need to examine. */
1954 find_oplock_types(fsp,
1955 oplock_request,
1956 lck,
1957 &batch_entry,
1958 &exclusive_entry,
1959 &got_level2_oplock,
1960 &got_a_none_oplock);
1962 /* First pass - send break only on batch oplocks. */
1963 if ((req != NULL) &&
1964 delay_for_batch_oplocks(fsp,
1965 req->mid,
1966 oplock_request,
1967 batch_entry)) {
1968 schedule_defer_open(lck, request_time, req);
1969 TALLOC_FREE(lck);
1970 return NT_STATUS_SHARING_VIOLATION;
1973 /* Use the client requested access mask here, not the one we
1974 * open with. */
1975 status = open_mode_check(conn, lck, fsp->name_hash,
1976 access_mask, share_access,
1977 create_options, &file_existed);
1979 if (NT_STATUS_IS_OK(status)) {
1980 /* We might be going to allow this open. Check oplock
1981 * status again. */
1982 /* Second pass - send break for both batch or
1983 * exclusive oplocks. */
1984 if ((req != NULL) &&
1985 delay_for_exclusive_oplocks(
1986 fsp,
1987 req->mid,
1988 oplock_request,
1989 exclusive_entry)) {
1990 schedule_defer_open(lck, request_time, req);
1991 TALLOC_FREE(lck);
1992 return NT_STATUS_SHARING_VIOLATION;
1996 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1997 /* DELETE_PENDING is not deferred for a second */
1998 TALLOC_FREE(lck);
1999 return status;
2002 grant_fsp_oplock_type(fsp,
2003 oplock_request,
2004 got_level2_oplock,
2005 got_a_none_oplock);
2007 if (!NT_STATUS_IS_OK(status)) {
2008 uint32 can_access_mask;
2009 bool can_access = True;
2011 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2013 /* Check if this can be done with the deny_dos and fcb
2014 * calls. */
2015 if (private_flags &
2016 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2017 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2018 if (req == NULL) {
2019 DEBUG(0, ("DOS open without an SMB "
2020 "request!\n"));
2021 TALLOC_FREE(lck);
2022 return NT_STATUS_INTERNAL_ERROR;
2025 /* Use the client requested access mask here,
2026 * not the one we open with. */
2027 status = fcb_or_dos_open(req,
2028 conn,
2029 fsp,
2030 smb_fname,
2032 req->smbpid,
2033 req->vuid,
2034 access_mask,
2035 share_access,
2036 create_options);
2038 if (NT_STATUS_IS_OK(status)) {
2039 TALLOC_FREE(lck);
2040 if (pinfo) {
2041 *pinfo = FILE_WAS_OPENED;
2043 return NT_STATUS_OK;
2048 * This next line is a subtlety we need for
2049 * MS-Access. If a file open will fail due to share
2050 * permissions and also for security (access) reasons,
2051 * we need to return the access failed error, not the
2052 * share error. We can't open the file due to kernel
2053 * oplock deadlock (it's possible we failed above on
2054 * the open_mode_check()) so use a userspace check.
2057 if (flags & O_RDWR) {
2058 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2059 } else if (flags & O_WRONLY) {
2060 can_access_mask = FILE_WRITE_DATA;
2061 } else {
2062 can_access_mask = FILE_READ_DATA;
2065 if (((can_access_mask & FILE_WRITE_DATA) &&
2066 !CAN_WRITE(conn)) ||
2067 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2068 smb_fname, can_access_mask))) {
2069 can_access = False;
2073 * If we're returning a share violation, ensure we
2074 * cope with the braindead 1 second delay.
2077 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2078 lp_defer_sharing_violations()) {
2079 struct timeval timeout;
2080 struct deferred_open_record state;
2081 int timeout_usecs;
2083 /* this is a hack to speed up torture tests
2084 in 'make test' */
2085 timeout_usecs = lp_parm_int(SNUM(conn),
2086 "smbd","sharedelay",
2087 SHARING_VIOLATION_USEC_WAIT);
2089 /* This is a relative time, added to the absolute
2090 request_time value to get the absolute timeout time.
2091 Note that if this is the second or greater time we enter
2092 this codepath for this particular request mid then
2093 request_time is left as the absolute time of the *first*
2094 time this request mid was processed. This is what allows
2095 the request to eventually time out. */
2097 timeout = timeval_set(0, timeout_usecs);
2099 /* Nothing actually uses state.delayed_for_oplocks
2100 but it's handy to differentiate in debug messages
2101 between a 30 second delay due to oplock break, and
2102 a 1 second delay for share mode conflicts. */
2104 state.delayed_for_oplocks = False;
2105 state.id = id;
2107 if ((req != NULL)
2108 && !request_timed_out(request_time,
2109 timeout)) {
2110 defer_open(lck, request_time, timeout,
2111 req, &state);
2115 TALLOC_FREE(lck);
2116 if (can_access) {
2118 * We have detected a sharing violation here
2119 * so return the correct error code
2121 status = NT_STATUS_SHARING_VIOLATION;
2122 } else {
2123 status = NT_STATUS_ACCESS_DENIED;
2125 return status;
2129 * We exit this block with the share entry *locked*.....
2133 SMB_ASSERT(!file_existed || (lck != NULL));
2136 * Ensure we pay attention to default ACLs on directories if required.
2139 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2140 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2141 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2144 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2145 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2146 (unsigned int)flags, (unsigned int)flags2,
2147 (unsigned int)unx_mode, (unsigned int)access_mask,
2148 (unsigned int)open_access_mask));
2151 * open_file strips any O_TRUNC flags itself.
2154 fsp_open = open_file(fsp, conn, req, parent_dir,
2155 flags|flags2, unx_mode, access_mask,
2156 open_access_mask);
2158 if (!NT_STATUS_IS_OK(fsp_open)) {
2159 TALLOC_FREE(lck);
2160 return fsp_open;
2163 if (!file_existed) {
2164 struct share_mode_entry *batch_entry = NULL;
2165 struct share_mode_entry *exclusive_entry = NULL;
2166 bool got_level2_oplock = false;
2167 bool got_a_none_oplock = false;
2168 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2169 struct file_id id;
2171 * Deal with the race condition where two smbd's detect the
2172 * file doesn't exist and do the create at the same time. One
2173 * of them will win and set a share mode, the other (ie. this
2174 * one) should check if the requested share mode for this
2175 * create is allowed.
2179 * Now the file exists and fsp is successfully opened,
2180 * fsp->dev and fsp->inode are valid and should replace the
2181 * dev=0,inode=0 from a non existent file. Spotted by
2182 * Nadav Danieli <nadavd@exanet.com>. JRA.
2185 id = fsp->file_id;
2187 lck = get_share_mode_lock(talloc_tos(), id,
2188 conn->connectpath,
2189 smb_fname, &old_write_time);
2191 if (lck == NULL) {
2192 DEBUG(0, ("open_file_ntcreate: Could not get share "
2193 "mode lock for %s\n",
2194 smb_fname_str_dbg(smb_fname)));
2195 fd_close(fsp);
2196 return NT_STATUS_SHARING_VIOLATION;
2199 /* Get the types we need to examine. */
2200 find_oplock_types(fsp,
2201 oplock_request,
2202 lck,
2203 &batch_entry,
2204 &exclusive_entry,
2205 &got_level2_oplock,
2206 &got_a_none_oplock);
2208 /* First pass - send break only on batch oplocks. */
2209 if ((req != NULL) &&
2210 delay_for_batch_oplocks(fsp,
2211 req->mid,
2212 oplock_request,
2213 batch_entry)) {
2214 schedule_defer_open(lck, request_time, req);
2215 TALLOC_FREE(lck);
2216 fd_close(fsp);
2217 return NT_STATUS_SHARING_VIOLATION;
2220 status = open_mode_check(conn, lck, fsp->name_hash,
2221 access_mask, share_access,
2222 create_options, &file_existed);
2224 if (NT_STATUS_IS_OK(status)) {
2225 /* We might be going to allow this open. Check oplock
2226 * status again. */
2227 /* Second pass - send break for both batch or
2228 * exclusive oplocks. */
2229 if ((req != NULL) &&
2230 delay_for_exclusive_oplocks(
2231 fsp,
2232 req->mid,
2233 oplock_request,
2234 exclusive_entry)) {
2235 schedule_defer_open(lck, request_time, req);
2236 TALLOC_FREE(lck);
2237 fd_close(fsp);
2238 return NT_STATUS_SHARING_VIOLATION;
2242 if (!NT_STATUS_IS_OK(status)) {
2243 struct deferred_open_record state;
2245 state.delayed_for_oplocks = False;
2246 state.id = id;
2248 /* Do it all over again immediately. In the second
2249 * round we will find that the file existed and handle
2250 * the DELETE_PENDING and FCB cases correctly. No need
2251 * to duplicate the code here. Essentially this is a
2252 * "goto top of this function", but don't tell
2253 * anybody... */
2255 if (req != NULL) {
2256 defer_open(lck, request_time, timeval_zero(),
2257 req, &state);
2259 TALLOC_FREE(lck);
2260 fd_close(fsp);
2261 return status;
2264 grant_fsp_oplock_type(fsp,
2265 oplock_request,
2266 got_level2_oplock,
2267 got_a_none_oplock);
2270 * We exit this block with the share entry *locked*.....
2275 SMB_ASSERT(lck != NULL);
2277 /* Delete streams if create_disposition requires it */
2278 if (file_existed && clear_ads &&
2279 !is_ntfs_stream_smb_fname(smb_fname)) {
2280 status = delete_all_streams(conn, smb_fname->base_name);
2281 if (!NT_STATUS_IS_OK(status)) {
2282 TALLOC_FREE(lck);
2283 fd_close(fsp);
2284 return status;
2288 /* note that we ignore failure for the following. It is
2289 basically a hack for NFS, and NFS will never set one of
2290 these only read them. Nobody but Samba can ever set a deny
2291 mode and we have already checked our more authoritative
2292 locking database for permission to set this deny mode. If
2293 the kernel refuses the operations then the kernel is wrong.
2294 note that GPFS supports it as well - jmcd */
2296 if (fsp->fh->fd != -1) {
2297 int ret_flock;
2298 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2299 if(ret_flock == -1 ){
2301 TALLOC_FREE(lck);
2302 fd_close(fsp);
2304 return NT_STATUS_SHARING_VIOLATION;
2309 * At this point onwards, we can guarentee that the share entry
2310 * is locked, whether we created the file or not, and that the
2311 * deny mode is compatible with all current opens.
2315 * If requested, truncate the file.
2318 if (file_existed && (flags2&O_TRUNC)) {
2320 * We are modifing the file after open - update the stat
2321 * struct..
2323 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2324 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2325 status = map_nt_error_from_unix(errno);
2326 TALLOC_FREE(lck);
2327 fd_close(fsp);
2328 return status;
2333 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2334 * but we don't have to store this - just ignore it on access check.
2336 if (conn->sconn->using_smb2) {
2338 * SMB2 doesn't return it (according to Microsoft tests).
2339 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2340 * File created with access = 0x7 (Read, Write, Delete)
2341 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2343 fsp->access_mask = access_mask;
2344 } else {
2345 /* But SMB1 does. */
2346 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2349 if (file_existed) {
2350 /* stat opens on existing files don't get oplocks. */
2351 if (is_stat_open(open_access_mask)) {
2352 fsp->oplock_type = NO_OPLOCK;
2355 if (flags2 & O_TRUNC) {
2356 info = FILE_WAS_OVERWRITTEN;
2357 } else {
2358 info = FILE_WAS_OPENED;
2360 } else {
2361 info = FILE_WAS_CREATED;
2364 if (pinfo) {
2365 *pinfo = info;
2369 * Setup the oplock info in both the shared memory and
2370 * file structs.
2373 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2375 * Could not get the kernel oplock or there are byte-range
2376 * locks on the file.
2378 fsp->oplock_type = NO_OPLOCK;
2381 set_share_mode(lck, fsp, get_current_uid(conn),
2382 req ? req->mid : 0,
2383 fsp->oplock_type);
2385 /* Handle strange delete on close create semantics. */
2386 if (create_options & FILE_DELETE_ON_CLOSE) {
2388 status = can_set_delete_on_close(fsp, new_dos_attributes);
2390 if (!NT_STATUS_IS_OK(status)) {
2391 /* Remember to delete the mode we just added. */
2392 del_share_mode(lck, fsp);
2393 TALLOC_FREE(lck);
2394 fd_close(fsp);
2395 return status;
2397 /* Note that here we set the *inital* delete on close flag,
2398 not the regular one. The magic gets handled in close. */
2399 fsp->initial_delete_on_close = True;
2402 if (info == FILE_WAS_OVERWRITTEN
2403 || info == FILE_WAS_CREATED
2404 || info == FILE_WAS_SUPERSEDED) {
2405 new_file_created = True;
2408 if (new_file_created) {
2409 /* Files should be initially set as archive */
2410 if (lp_map_archive(SNUM(conn)) ||
2411 lp_store_dos_attributes(SNUM(conn))) {
2412 if (!posix_open) {
2413 if (file_set_dosmode(conn, smb_fname,
2414 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2415 parent_dir, true) == 0) {
2416 unx_mode = smb_fname->st.st_ex_mode;
2422 /* Determine sparse flag. */
2423 if (posix_open) {
2424 /* POSIX opens are sparse by default. */
2425 fsp->is_sparse = true;
2426 } else {
2427 fsp->is_sparse = (file_existed &&
2428 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2432 * Take care of inherited ACLs on created files - if default ACL not
2433 * selected.
2436 if (!posix_open && !file_existed && !def_acl) {
2438 int saved_errno = errno; /* We might get ENOSYS in the next
2439 * call.. */
2441 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2442 errno == ENOSYS) {
2443 errno = saved_errno; /* Ignore ENOSYS */
2446 } else if (new_unx_mode) {
2448 int ret = -1;
2450 /* Attributes need changing. File already existed. */
2453 int saved_errno = errno; /* We might get ENOSYS in the
2454 * next call.. */
2455 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2457 if (ret == -1 && errno == ENOSYS) {
2458 errno = saved_errno; /* Ignore ENOSYS */
2459 } else {
2460 DEBUG(5, ("open_file_ntcreate: reset "
2461 "attributes of file %s to 0%o\n",
2462 smb_fname_str_dbg(smb_fname),
2463 (unsigned int)new_unx_mode));
2464 ret = 0; /* Don't do the fchmod below. */
2468 if ((ret == -1) &&
2469 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2470 DEBUG(5, ("open_file_ntcreate: failed to reset "
2471 "attributes of file %s to 0%o\n",
2472 smb_fname_str_dbg(smb_fname),
2473 (unsigned int)new_unx_mode));
2476 /* If this is a successful open, we must remove any deferred open
2477 * records. */
2478 if (req != NULL) {
2479 del_deferred_open_entry(lck, req->mid,
2480 messaging_server_id(req->sconn->msg_ctx));
2482 TALLOC_FREE(lck);
2484 return NT_STATUS_OK;
2488 /****************************************************************************
2489 Open a file for for write to ensure that we can fchmod it.
2490 ****************************************************************************/
2492 NTSTATUS open_file_fchmod(connection_struct *conn,
2493 struct smb_filename *smb_fname,
2494 files_struct **result)
2496 if (!VALID_STAT(smb_fname->st)) {
2497 return NT_STATUS_INVALID_PARAMETER;
2500 return SMB_VFS_CREATE_FILE(
2501 conn, /* conn */
2502 NULL, /* req */
2503 0, /* root_dir_fid */
2504 smb_fname, /* fname */
2505 FILE_WRITE_DATA, /* access_mask */
2506 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2507 FILE_SHARE_DELETE),
2508 FILE_OPEN, /* create_disposition*/
2509 0, /* create_options */
2510 0, /* file_attributes */
2511 INTERNAL_OPEN_ONLY, /* oplock_request */
2512 0, /* allocation_size */
2513 0, /* private_flags */
2514 NULL, /* sd */
2515 NULL, /* ea_list */
2516 result, /* result */
2517 NULL); /* pinfo */
2520 static NTSTATUS mkdir_internal(connection_struct *conn,
2521 struct smb_filename *smb_dname,
2522 uint32 file_attributes)
2524 mode_t mode;
2525 char *parent_dir = NULL;
2526 NTSTATUS status;
2527 bool posix_open = false;
2528 bool need_re_stat = false;
2529 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2531 if(access_mask & ~(conn->share_access)) {
2532 DEBUG(5,("mkdir_internal: failing share access "
2533 "%s\n", lp_servicename(SNUM(conn))));
2534 return NT_STATUS_ACCESS_DENIED;
2537 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2538 NULL)) {
2539 return NT_STATUS_NO_MEMORY;
2542 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2543 posix_open = true;
2544 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2545 } else {
2546 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2549 status = check_parent_access(conn,
2550 smb_dname,
2551 access_mask);
2552 if(!NT_STATUS_IS_OK(status)) {
2553 DEBUG(5,("mkdir_internal: check_parent_access "
2554 "on directory %s for path %s returned %s\n",
2555 parent_dir,
2556 smb_dname->base_name,
2557 nt_errstr(status) ));
2558 return status;
2561 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2562 return map_nt_error_from_unix(errno);
2565 /* Ensure we're checking for a symlink here.... */
2566 /* We don't want to get caught by a symlink racer. */
2568 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2569 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2570 smb_fname_str_dbg(smb_dname), strerror(errno)));
2571 return map_nt_error_from_unix(errno);
2574 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2575 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2576 smb_fname_str_dbg(smb_dname)));
2577 return NT_STATUS_NOT_A_DIRECTORY;
2580 if (lp_store_dos_attributes(SNUM(conn))) {
2581 if (!posix_open) {
2582 file_set_dosmode(conn, smb_dname,
2583 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2584 parent_dir, true);
2588 if (lp_inherit_perms(SNUM(conn))) {
2589 inherit_access_posix_acl(conn, parent_dir,
2590 smb_dname->base_name, mode);
2591 need_re_stat = true;
2594 if (!posix_open) {
2596 * Check if high bits should have been set,
2597 * then (if bits are missing): add them.
2598 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2599 * dir.
2601 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2602 (mode & ~smb_dname->st.st_ex_mode)) {
2603 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2604 (smb_dname->st.st_ex_mode |
2605 (mode & ~smb_dname->st.st_ex_mode)));
2606 need_re_stat = true;
2610 /* Change the owner if required. */
2611 if (lp_inherit_owner(SNUM(conn))) {
2612 change_dir_owner_to_parent(conn, parent_dir,
2613 smb_dname->base_name,
2614 &smb_dname->st);
2615 need_re_stat = true;
2618 if (need_re_stat) {
2619 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2620 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2621 smb_fname_str_dbg(smb_dname), strerror(errno)));
2622 return map_nt_error_from_unix(errno);
2626 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2627 smb_dname->base_name);
2629 return NT_STATUS_OK;
2632 /****************************************************************************
2633 Ensure we didn't get symlink raced on opening a directory.
2634 ****************************************************************************/
2636 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2637 const SMB_STRUCT_STAT *sbuf2)
2639 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2640 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2641 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2642 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2643 return false;
2645 return true;
2648 /****************************************************************************
2649 Open a directory from an NT SMB call.
2650 ****************************************************************************/
2652 static NTSTATUS open_directory(connection_struct *conn,
2653 struct smb_request *req,
2654 struct smb_filename *smb_dname,
2655 uint32 access_mask,
2656 uint32 share_access,
2657 uint32 create_disposition,
2658 uint32 create_options,
2659 uint32 file_attributes,
2660 int *pinfo,
2661 files_struct **result)
2663 files_struct *fsp = NULL;
2664 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2665 struct share_mode_lock *lck = NULL;
2666 NTSTATUS status;
2667 struct timespec mtimespec;
2668 int info = 0;
2670 if (is_ntfs_stream_smb_fname(smb_dname)) {
2671 DEBUG(2, ("open_directory: %s is a stream name!\n",
2672 smb_fname_str_dbg(smb_dname)));
2673 return NT_STATUS_NOT_A_DIRECTORY;
2676 /* Ensure we have a directory attribute. */
2677 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2679 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2680 "share_access = 0x%x create_options = 0x%x, "
2681 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2682 smb_fname_str_dbg(smb_dname),
2683 (unsigned int)access_mask,
2684 (unsigned int)share_access,
2685 (unsigned int)create_options,
2686 (unsigned int)create_disposition,
2687 (unsigned int)file_attributes));
2689 status = smbd_calculate_access_mask(conn, smb_dname,
2690 access_mask, &access_mask);
2691 if (!NT_STATUS_IS_OK(status)) {
2692 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2693 "on file %s returned %s\n",
2694 smb_fname_str_dbg(smb_dname),
2695 nt_errstr(status)));
2696 return status;
2699 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2700 !security_token_has_privilege(get_current_nttok(conn),
2701 SEC_PRIV_SECURITY)) {
2702 DEBUG(10, ("open_directory: open on %s "
2703 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2704 smb_fname_str_dbg(smb_dname)));
2705 return NT_STATUS_PRIVILEGE_NOT_HELD;
2708 switch( create_disposition ) {
2709 case FILE_OPEN:
2711 if (!dir_existed) {
2712 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2715 info = FILE_WAS_OPENED;
2716 break;
2718 case FILE_CREATE:
2720 /* If directory exists error. If directory doesn't
2721 * exist create. */
2723 if (dir_existed) {
2724 status = NT_STATUS_OBJECT_NAME_COLLISION;
2725 DEBUG(2, ("open_directory: unable to create "
2726 "%s. Error was %s\n",
2727 smb_fname_str_dbg(smb_dname),
2728 nt_errstr(status)));
2729 return status;
2732 status = mkdir_internal(conn, smb_dname,
2733 file_attributes);
2735 if (!NT_STATUS_IS_OK(status)) {
2736 DEBUG(2, ("open_directory: unable to create "
2737 "%s. Error was %s\n",
2738 smb_fname_str_dbg(smb_dname),
2739 nt_errstr(status)));
2740 return status;
2743 info = FILE_WAS_CREATED;
2744 break;
2746 case FILE_OPEN_IF:
2748 * If directory exists open. If directory doesn't
2749 * exist create.
2752 if (dir_existed) {
2753 status = NT_STATUS_OK;
2754 info = FILE_WAS_OPENED;
2755 } else {
2756 status = mkdir_internal(conn, smb_dname,
2757 file_attributes);
2759 if (NT_STATUS_IS_OK(status)) {
2760 info = FILE_WAS_CREATED;
2761 } else {
2762 /* Cope with create race. */
2763 if (!NT_STATUS_EQUAL(status,
2764 NT_STATUS_OBJECT_NAME_COLLISION)) {
2765 DEBUG(2, ("open_directory: unable to create "
2766 "%s. Error was %s\n",
2767 smb_fname_str_dbg(smb_dname),
2768 nt_errstr(status)));
2769 return status;
2771 info = FILE_WAS_OPENED;
2775 break;
2777 case FILE_SUPERSEDE:
2778 case FILE_OVERWRITE:
2779 case FILE_OVERWRITE_IF:
2780 default:
2781 DEBUG(5,("open_directory: invalid create_disposition "
2782 "0x%x for directory %s\n",
2783 (unsigned int)create_disposition,
2784 smb_fname_str_dbg(smb_dname)));
2785 return NT_STATUS_INVALID_PARAMETER;
2788 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2789 DEBUG(5,("open_directory: %s is not a directory !\n",
2790 smb_fname_str_dbg(smb_dname)));
2791 return NT_STATUS_NOT_A_DIRECTORY;
2794 if (info == FILE_WAS_OPENED) {
2795 status = smbd_check_access_rights(conn, smb_dname, access_mask);
2796 if (!NT_STATUS_IS_OK(status)) {
2797 DEBUG(10, ("open_directory: smbd_check_access_rights on "
2798 "file %s failed with %s\n",
2799 smb_fname_str_dbg(smb_dname),
2800 nt_errstr(status)));
2801 return status;
2805 status = file_new(req, conn, &fsp);
2806 if(!NT_STATUS_IS_OK(status)) {
2807 return status;
2811 * Setup the files_struct for it.
2814 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2815 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2816 fsp->file_pid = req ? req->smbpid : 0;
2817 fsp->can_lock = False;
2818 fsp->can_read = False;
2819 fsp->can_write = False;
2821 fsp->share_access = share_access;
2822 fsp->fh->private_options = 0;
2824 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2826 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2827 fsp->print_file = NULL;
2828 fsp->modified = False;
2829 fsp->oplock_type = NO_OPLOCK;
2830 fsp->sent_oplock_break = NO_BREAK_SENT;
2831 fsp->is_directory = True;
2832 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2833 status = fsp_set_smb_fname(fsp, smb_dname);
2834 if (!NT_STATUS_IS_OK(status)) {
2835 file_free(req, fsp);
2836 return status;
2839 mtimespec = smb_dname->st.st_ex_mtime;
2841 #ifdef O_DIRECTORY
2842 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2843 #else
2844 /* POSIX allows us to open a directory with O_RDONLY. */
2845 status = fd_open(conn, fsp, O_RDONLY, 0);
2846 #endif
2847 if (!NT_STATUS_IS_OK(status)) {
2848 DEBUG(5, ("open_directory: Could not open fd for "
2849 "%s (%s)\n",
2850 smb_fname_str_dbg(smb_dname),
2851 nt_errstr(status)));
2852 file_free(req, fsp);
2853 return status;
2856 status = vfs_stat_fsp(fsp);
2857 if (!NT_STATUS_IS_OK(status)) {
2858 fd_close(fsp);
2859 file_free(req, fsp);
2860 return status;
2863 /* Ensure there was no race condition. */
2864 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2865 DEBUG(5,("open_directory: stat struct differs for "
2866 "directory %s.\n",
2867 smb_fname_str_dbg(smb_dname)));
2868 fd_close(fsp);
2869 file_free(req, fsp);
2870 return NT_STATUS_ACCESS_DENIED;
2873 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2874 conn->connectpath, smb_dname,
2875 &mtimespec);
2877 if (lck == NULL) {
2878 DEBUG(0, ("open_directory: Could not get share mode lock for "
2879 "%s\n", smb_fname_str_dbg(smb_dname)));
2880 fd_close(fsp);
2881 file_free(req, fsp);
2882 return NT_STATUS_SHARING_VIOLATION;
2885 status = open_mode_check(conn, lck, fsp->name_hash,
2886 access_mask, share_access,
2887 create_options, &dir_existed);
2889 if (!NT_STATUS_IS_OK(status)) {
2890 TALLOC_FREE(lck);
2891 fd_close(fsp);
2892 file_free(req, fsp);
2893 return status;
2896 set_share_mode(lck, fsp, get_current_uid(conn),
2897 req ? req->mid : 0, NO_OPLOCK);
2899 /* For directories the delete on close bit at open time seems
2900 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2901 if (create_options & FILE_DELETE_ON_CLOSE) {
2902 status = can_set_delete_on_close(fsp, 0);
2903 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2904 TALLOC_FREE(lck);
2905 fd_close(fsp);
2906 file_free(req, fsp);
2907 return status;
2910 if (NT_STATUS_IS_OK(status)) {
2911 /* Note that here we set the *inital* delete on close flag,
2912 not the regular one. The magic gets handled in close. */
2913 fsp->initial_delete_on_close = True;
2917 TALLOC_FREE(lck);
2919 if (pinfo) {
2920 *pinfo = info;
2923 *result = fsp;
2924 return NT_STATUS_OK;
2927 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2928 struct smb_filename *smb_dname)
2930 NTSTATUS status;
2931 files_struct *fsp;
2933 status = SMB_VFS_CREATE_FILE(
2934 conn, /* conn */
2935 req, /* req */
2936 0, /* root_dir_fid */
2937 smb_dname, /* fname */
2938 FILE_READ_ATTRIBUTES, /* access_mask */
2939 FILE_SHARE_NONE, /* share_access */
2940 FILE_CREATE, /* create_disposition*/
2941 FILE_DIRECTORY_FILE, /* create_options */
2942 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2943 0, /* oplock_request */
2944 0, /* allocation_size */
2945 0, /* private_flags */
2946 NULL, /* sd */
2947 NULL, /* ea_list */
2948 &fsp, /* result */
2949 NULL); /* pinfo */
2951 if (NT_STATUS_IS_OK(status)) {
2952 close_file(req, fsp, NORMAL_CLOSE);
2955 return status;
2958 /****************************************************************************
2959 Receive notification that one of our open files has been renamed by another
2960 smbd process.
2961 ****************************************************************************/
2963 void msg_file_was_renamed(struct messaging_context *msg,
2964 void *private_data,
2965 uint32_t msg_type,
2966 struct server_id server_id,
2967 DATA_BLOB *data)
2969 files_struct *fsp;
2970 char *frm = (char *)data->data;
2971 struct file_id id;
2972 const char *sharepath;
2973 const char *base_name;
2974 const char *stream_name;
2975 struct smb_filename *smb_fname = NULL;
2976 size_t sp_len, bn_len;
2977 NTSTATUS status;
2978 struct smbd_server_connection *sconn =
2979 talloc_get_type_abort(private_data,
2980 struct smbd_server_connection);
2982 if (data->data == NULL
2983 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2984 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2985 (int)data->length));
2986 return;
2989 /* Unpack the message. */
2990 pull_file_id_24(frm, &id);
2991 sharepath = &frm[24];
2992 sp_len = strlen(sharepath);
2993 base_name = sharepath + sp_len + 1;
2994 bn_len = strlen(base_name);
2995 stream_name = sharepath + sp_len + 1 + bn_len + 1;
2997 /* stream_name must always be NULL if there is no stream. */
2998 if (stream_name[0] == '\0') {
2999 stream_name = NULL;
3002 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3003 stream_name, NULL, &smb_fname);
3004 if (!NT_STATUS_IS_OK(status)) {
3005 return;
3008 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3009 "file_id %s\n",
3010 sharepath, smb_fname_str_dbg(smb_fname),
3011 file_id_string_tos(&id)));
3013 for(fsp = file_find_di_first(sconn, id); fsp;
3014 fsp = file_find_di_next(fsp)) {
3015 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3017 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3018 fsp->fnum, fsp_str_dbg(fsp),
3019 smb_fname_str_dbg(smb_fname)));
3020 status = fsp_set_smb_fname(fsp, smb_fname);
3021 if (!NT_STATUS_IS_OK(status)) {
3022 goto out;
3024 } else {
3025 /* TODO. JRA. */
3026 /* Now we have the complete path we can work out if this is
3027 actually within this share and adjust newname accordingly. */
3028 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3029 "not sharepath %s) "
3030 "fnum %d from %s -> %s\n",
3031 fsp->conn->connectpath,
3032 sharepath,
3033 fsp->fnum,
3034 fsp_str_dbg(fsp),
3035 smb_fname_str_dbg(smb_fname)));
3038 out:
3039 TALLOC_FREE(smb_fname);
3040 return;
3044 * If a main file is opened for delete, all streams need to be checked for
3045 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3046 * If that works, delete them all by setting the delete on close and close.
3049 NTSTATUS open_streams_for_delete(connection_struct *conn,
3050 const char *fname)
3052 struct stream_struct *stream_info = NULL;
3053 files_struct **streams = NULL;
3054 int i;
3055 unsigned int num_streams = 0;
3056 TALLOC_CTX *frame = talloc_stackframe();
3057 NTSTATUS status;
3059 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3060 &num_streams, &stream_info);
3062 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3063 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3064 DEBUG(10, ("no streams around\n"));
3065 TALLOC_FREE(frame);
3066 return NT_STATUS_OK;
3069 if (!NT_STATUS_IS_OK(status)) {
3070 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3071 nt_errstr(status)));
3072 goto fail;
3075 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3076 num_streams));
3078 if (num_streams == 0) {
3079 TALLOC_FREE(frame);
3080 return NT_STATUS_OK;
3083 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3084 if (streams == NULL) {
3085 DEBUG(0, ("talloc failed\n"));
3086 status = NT_STATUS_NO_MEMORY;
3087 goto fail;
3090 for (i=0; i<num_streams; i++) {
3091 struct smb_filename *smb_fname = NULL;
3093 if (strequal(stream_info[i].name, "::$DATA")) {
3094 streams[i] = NULL;
3095 continue;
3098 status = create_synthetic_smb_fname(talloc_tos(), fname,
3099 stream_info[i].name,
3100 NULL, &smb_fname);
3101 if (!NT_STATUS_IS_OK(status)) {
3102 goto fail;
3105 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3106 DEBUG(10, ("Unable to stat stream: %s\n",
3107 smb_fname_str_dbg(smb_fname)));
3110 status = SMB_VFS_CREATE_FILE(
3111 conn, /* conn */
3112 NULL, /* req */
3113 0, /* root_dir_fid */
3114 smb_fname, /* fname */
3115 DELETE_ACCESS, /* access_mask */
3116 (FILE_SHARE_READ | /* share_access */
3117 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3118 FILE_OPEN, /* create_disposition*/
3119 0, /* create_options */
3120 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3121 0, /* oplock_request */
3122 0, /* allocation_size */
3123 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3124 NULL, /* sd */
3125 NULL, /* ea_list */
3126 &streams[i], /* result */
3127 NULL); /* pinfo */
3129 if (!NT_STATUS_IS_OK(status)) {
3130 DEBUG(10, ("Could not open stream %s: %s\n",
3131 smb_fname_str_dbg(smb_fname),
3132 nt_errstr(status)));
3134 TALLOC_FREE(smb_fname);
3135 break;
3137 TALLOC_FREE(smb_fname);
3141 * don't touch the variable "status" beyond this point :-)
3144 for (i -= 1 ; i >= 0; i--) {
3145 if (streams[i] == NULL) {
3146 continue;
3149 DEBUG(10, ("Closing stream # %d, %s\n", i,
3150 fsp_str_dbg(streams[i])));
3151 close_file(NULL, streams[i], NORMAL_CLOSE);
3154 fail:
3155 TALLOC_FREE(frame);
3156 return status;
3159 /*********************************************************************
3160 Create a default ACL by inheriting from the parent. If no inheritance
3161 from the parent available, don't set anything. This will leave the actual
3162 permissions the new file or directory already got from the filesystem
3163 as the NT ACL when read.
3164 *********************************************************************/
3166 static NTSTATUS inherit_new_acl(files_struct *fsp)
3168 TALLOC_CTX *ctx = talloc_tos();
3169 char *parent_name = NULL;
3170 struct security_descriptor *parent_desc = NULL;
3171 NTSTATUS status = NT_STATUS_OK;
3172 struct security_descriptor *psd = NULL;
3173 struct dom_sid *owner_sid = NULL;
3174 struct dom_sid *group_sid = NULL;
3175 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3176 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3177 bool inheritable_components = false;
3178 size_t size = 0;
3180 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3181 return NT_STATUS_NO_MEMORY;
3184 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3185 parent_name,
3186 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3187 &parent_desc);
3188 if (!NT_STATUS_IS_OK(status)) {
3189 return status;
3192 inheritable_components = sd_has_inheritable_components(parent_desc,
3193 fsp->is_directory);
3195 if (!inheritable_components && !inherit_owner) {
3196 /* Nothing to inherit and not setting owner. */
3197 return NT_STATUS_OK;
3200 /* Create an inherited descriptor from the parent. */
3202 if (DEBUGLEVEL >= 10) {
3203 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3204 fsp_str_dbg(fsp) ));
3205 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3208 /* Inherit from parent descriptor if "inherit owner" set. */
3209 if (inherit_owner) {
3210 owner_sid = parent_desc->owner_sid;
3211 group_sid = parent_desc->group_sid;
3214 if (owner_sid == NULL) {
3215 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3217 if (group_sid == NULL) {
3218 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3221 status = se_create_child_secdesc(ctx,
3222 &psd,
3223 &size,
3224 parent_desc,
3225 owner_sid,
3226 group_sid,
3227 fsp->is_directory);
3228 if (!NT_STATUS_IS_OK(status)) {
3229 return status;
3232 /* If inheritable_components == false,
3233 se_create_child_secdesc()
3234 creates a security desriptor with a NULL dacl
3235 entry, but with SEC_DESC_DACL_PRESENT. We need
3236 to remove that flag. */
3238 if (!inheritable_components) {
3239 security_info_sent &= ~SECINFO_DACL;
3240 psd->type &= ~SEC_DESC_DACL_PRESENT;
3243 if (DEBUGLEVEL >= 10) {
3244 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3245 fsp_str_dbg(fsp) ));
3246 NDR_PRINT_DEBUG(security_descriptor, psd);
3249 if (inherit_owner) {
3250 /* We need to be root to force this. */
3251 become_root();
3253 status = SMB_VFS_FSET_NT_ACL(fsp,
3254 security_info_sent,
3255 psd);
3256 if (inherit_owner) {
3257 unbecome_root();
3259 return status;
3263 * Wrapper around open_file_ntcreate and open_directory
3266 static NTSTATUS create_file_unixpath(connection_struct *conn,
3267 struct smb_request *req,
3268 struct smb_filename *smb_fname,
3269 uint32_t access_mask,
3270 uint32_t share_access,
3271 uint32_t create_disposition,
3272 uint32_t create_options,
3273 uint32_t file_attributes,
3274 uint32_t oplock_request,
3275 uint64_t allocation_size,
3276 uint32_t private_flags,
3277 struct security_descriptor *sd,
3278 struct ea_list *ea_list,
3280 files_struct **result,
3281 int *pinfo)
3283 int info = FILE_WAS_OPENED;
3284 files_struct *base_fsp = NULL;
3285 files_struct *fsp = NULL;
3286 NTSTATUS status;
3288 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3289 "file_attributes = 0x%x, share_access = 0x%x, "
3290 "create_disposition = 0x%x create_options = 0x%x "
3291 "oplock_request = 0x%x private_flags = 0x%x "
3292 "ea_list = 0x%p, sd = 0x%p, "
3293 "fname = %s\n",
3294 (unsigned int)access_mask,
3295 (unsigned int)file_attributes,
3296 (unsigned int)share_access,
3297 (unsigned int)create_disposition,
3298 (unsigned int)create_options,
3299 (unsigned int)oplock_request,
3300 (unsigned int)private_flags,
3301 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3303 if (create_options & FILE_OPEN_BY_FILE_ID) {
3304 status = NT_STATUS_NOT_SUPPORTED;
3305 goto fail;
3308 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3309 status = NT_STATUS_INVALID_PARAMETER;
3310 goto fail;
3313 if (req == NULL) {
3314 oplock_request |= INTERNAL_OPEN_ONLY;
3317 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3318 && (access_mask & DELETE_ACCESS)
3319 && !is_ntfs_stream_smb_fname(smb_fname)) {
3321 * We can't open a file with DELETE access if any of the
3322 * streams is open without FILE_SHARE_DELETE
3324 status = open_streams_for_delete(conn, smb_fname->base_name);
3326 if (!NT_STATUS_IS_OK(status)) {
3327 goto fail;
3331 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3332 !security_token_has_privilege(get_current_nttok(conn),
3333 SEC_PRIV_SECURITY)) {
3334 DEBUG(10, ("create_file_unixpath: open on %s "
3335 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3336 smb_fname_str_dbg(smb_fname)));
3337 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3338 goto fail;
3341 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3342 && is_ntfs_stream_smb_fname(smb_fname)
3343 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3344 uint32 base_create_disposition;
3345 struct smb_filename *smb_fname_base = NULL;
3347 if (create_options & FILE_DIRECTORY_FILE) {
3348 status = NT_STATUS_NOT_A_DIRECTORY;
3349 goto fail;
3352 switch (create_disposition) {
3353 case FILE_OPEN:
3354 base_create_disposition = FILE_OPEN;
3355 break;
3356 default:
3357 base_create_disposition = FILE_OPEN_IF;
3358 break;
3361 /* Create an smb_filename with stream_name == NULL. */
3362 status = create_synthetic_smb_fname(talloc_tos(),
3363 smb_fname->base_name,
3364 NULL, NULL,
3365 &smb_fname_base);
3366 if (!NT_STATUS_IS_OK(status)) {
3367 goto fail;
3370 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3371 DEBUG(10, ("Unable to stat stream: %s\n",
3372 smb_fname_str_dbg(smb_fname_base)));
3375 /* Open the base file. */
3376 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3377 FILE_SHARE_READ
3378 | FILE_SHARE_WRITE
3379 | FILE_SHARE_DELETE,
3380 base_create_disposition,
3381 0, 0, 0, 0, 0, NULL, NULL,
3382 &base_fsp, NULL);
3383 TALLOC_FREE(smb_fname_base);
3385 if (!NT_STATUS_IS_OK(status)) {
3386 DEBUG(10, ("create_file_unixpath for base %s failed: "
3387 "%s\n", smb_fname->base_name,
3388 nt_errstr(status)));
3389 goto fail;
3391 /* we don't need to low level fd */
3392 fd_close(base_fsp);
3396 * If it's a request for a directory open, deal with it separately.
3399 if (create_options & FILE_DIRECTORY_FILE) {
3401 if (create_options & FILE_NON_DIRECTORY_FILE) {
3402 status = NT_STATUS_INVALID_PARAMETER;
3403 goto fail;
3406 /* Can't open a temp directory. IFS kit test. */
3407 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3408 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3409 status = NT_STATUS_INVALID_PARAMETER;
3410 goto fail;
3414 * We will get a create directory here if the Win32
3415 * app specified a security descriptor in the
3416 * CreateDirectory() call.
3419 oplock_request = 0;
3420 status = open_directory(
3421 conn, req, smb_fname, access_mask, share_access,
3422 create_disposition, create_options, file_attributes,
3423 &info, &fsp);
3424 } else {
3427 * Ordinary file case.
3430 status = file_new(req, conn, &fsp);
3431 if(!NT_STATUS_IS_OK(status)) {
3432 goto fail;
3435 status = fsp_set_smb_fname(fsp, smb_fname);
3436 if (!NT_STATUS_IS_OK(status)) {
3437 goto fail;
3441 * We're opening the stream element of a base_fsp
3442 * we already opened. Set up the base_fsp pointer.
3444 if (base_fsp) {
3445 fsp->base_fsp = base_fsp;
3448 status = open_file_ntcreate(conn,
3449 req,
3450 access_mask,
3451 share_access,
3452 create_disposition,
3453 create_options,
3454 file_attributes,
3455 oplock_request,
3456 private_flags,
3457 &info,
3458 fsp);
3460 if(!NT_STATUS_IS_OK(status)) {
3461 file_free(req, fsp);
3462 fsp = NULL;
3465 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3467 /* A stream open never opens a directory */
3469 if (base_fsp) {
3470 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3471 goto fail;
3475 * Fail the open if it was explicitly a non-directory
3476 * file.
3479 if (create_options & FILE_NON_DIRECTORY_FILE) {
3480 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3481 goto fail;
3484 oplock_request = 0;
3485 status = open_directory(
3486 conn, req, smb_fname, access_mask,
3487 share_access, create_disposition,
3488 create_options, file_attributes,
3489 &info, &fsp);
3493 if (!NT_STATUS_IS_OK(status)) {
3494 goto fail;
3497 fsp->base_fsp = base_fsp;
3499 if ((ea_list != NULL) &&
3500 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3501 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3502 if (!NT_STATUS_IS_OK(status)) {
3503 goto fail;
3507 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3508 status = NT_STATUS_ACCESS_DENIED;
3509 goto fail;
3512 /* Save the requested allocation size. */
3513 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3514 if (allocation_size
3515 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3516 fsp->initial_allocation_size = smb_roundup(
3517 fsp->conn, allocation_size);
3518 if (fsp->is_directory) {
3519 /* Can't set allocation size on a directory. */
3520 status = NT_STATUS_ACCESS_DENIED;
3521 goto fail;
3523 if (vfs_allocate_file_space(
3524 fsp, fsp->initial_allocation_size) == -1) {
3525 status = NT_STATUS_DISK_FULL;
3526 goto fail;
3528 } else {
3529 fsp->initial_allocation_size = smb_roundup(
3530 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3534 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3535 fsp->base_fsp == NULL) {
3536 if (sd != NULL) {
3538 * According to the MS documentation, the only time the security
3539 * descriptor is applied to the opened file is iff we *created* the
3540 * file; an existing file stays the same.
3542 * Also, it seems (from observation) that you can open the file with
3543 * any access mask but you can still write the sd. We need to override
3544 * the granted access before we call set_sd
3545 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3548 uint32_t sec_info_sent;
3549 uint32_t saved_access_mask = fsp->access_mask;
3551 sec_info_sent = get_sec_info(sd);
3553 fsp->access_mask = FILE_GENERIC_ALL;
3555 /* Convert all the generic bits. */
3556 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3557 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3559 if (sec_info_sent & (SECINFO_OWNER|
3560 SECINFO_GROUP|
3561 SECINFO_DACL|
3562 SECINFO_SACL)) {
3563 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3566 fsp->access_mask = saved_access_mask;
3568 if (!NT_STATUS_IS_OK(status)) {
3569 goto fail;
3571 } else if (lp_inherit_acls(SNUM(conn))) {
3572 /* Inherit from parent. Errors here are not fatal. */
3573 status = inherit_new_acl(fsp);
3574 if (!NT_STATUS_IS_OK(status)) {
3575 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3576 fsp_str_dbg(fsp),
3577 nt_errstr(status) ));
3582 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3584 *result = fsp;
3585 if (pinfo != NULL) {
3586 *pinfo = info;
3589 smb_fname->st = fsp->fsp_name->st;
3591 return NT_STATUS_OK;
3593 fail:
3594 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3596 if (fsp != NULL) {
3597 if (base_fsp && fsp->base_fsp == base_fsp) {
3599 * The close_file below will close
3600 * fsp->base_fsp.
3602 base_fsp = NULL;
3604 close_file(req, fsp, ERROR_CLOSE);
3605 fsp = NULL;
3607 if (base_fsp != NULL) {
3608 close_file(req, base_fsp, ERROR_CLOSE);
3609 base_fsp = NULL;
3611 return status;
3615 * Calculate the full path name given a relative fid.
3617 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3618 struct smb_request *req,
3619 uint16_t root_dir_fid,
3620 const struct smb_filename *smb_fname,
3621 struct smb_filename **smb_fname_out)
3623 files_struct *dir_fsp;
3624 char *parent_fname = NULL;
3625 char *new_base_name = NULL;
3626 NTSTATUS status;
3628 if (root_dir_fid == 0 || !smb_fname) {
3629 status = NT_STATUS_INTERNAL_ERROR;
3630 goto out;
3633 dir_fsp = file_fsp(req, root_dir_fid);
3635 if (dir_fsp == NULL) {
3636 status = NT_STATUS_INVALID_HANDLE;
3637 goto out;
3640 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3641 status = NT_STATUS_INVALID_HANDLE;
3642 goto out;
3645 if (!dir_fsp->is_directory) {
3648 * Check to see if this is a mac fork of some kind.
3651 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3652 is_ntfs_stream_smb_fname(smb_fname)) {
3653 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3654 goto out;
3658 we need to handle the case when we get a
3659 relative open relative to a file and the
3660 pathname is blank - this is a reopen!
3661 (hint from demyn plantenberg)
3664 status = NT_STATUS_INVALID_HANDLE;
3665 goto out;
3668 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3670 * We're at the toplevel dir, the final file name
3671 * must not contain ./, as this is filtered out
3672 * normally by srvstr_get_path and unix_convert
3673 * explicitly rejects paths containing ./.
3675 parent_fname = talloc_strdup(talloc_tos(), "");
3676 if (parent_fname == NULL) {
3677 status = NT_STATUS_NO_MEMORY;
3678 goto out;
3680 } else {
3681 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3684 * Copy in the base directory name.
3687 parent_fname = talloc_array(talloc_tos(), char,
3688 dir_name_len+2);
3689 if (parent_fname == NULL) {
3690 status = NT_STATUS_NO_MEMORY;
3691 goto out;
3693 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3694 dir_name_len+1);
3697 * Ensure it ends in a '/'.
3698 * We used TALLOC_SIZE +2 to add space for the '/'.
3701 if(dir_name_len
3702 && (parent_fname[dir_name_len-1] != '\\')
3703 && (parent_fname[dir_name_len-1] != '/')) {
3704 parent_fname[dir_name_len] = '/';
3705 parent_fname[dir_name_len+1] = '\0';
3709 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3710 smb_fname->base_name);
3711 if (new_base_name == NULL) {
3712 status = NT_STATUS_NO_MEMORY;
3713 goto out;
3716 status = filename_convert(req,
3717 conn,
3718 req->flags2 & FLAGS2_DFS_PATHNAMES,
3719 new_base_name,
3721 NULL,
3722 smb_fname_out);
3723 if (!NT_STATUS_IS_OK(status)) {
3724 goto out;
3727 out:
3728 TALLOC_FREE(parent_fname);
3729 TALLOC_FREE(new_base_name);
3730 return status;
3733 NTSTATUS create_file_default(connection_struct *conn,
3734 struct smb_request *req,
3735 uint16_t root_dir_fid,
3736 struct smb_filename *smb_fname,
3737 uint32_t access_mask,
3738 uint32_t share_access,
3739 uint32_t create_disposition,
3740 uint32_t create_options,
3741 uint32_t file_attributes,
3742 uint32_t oplock_request,
3743 uint64_t allocation_size,
3744 uint32_t private_flags,
3745 struct security_descriptor *sd,
3746 struct ea_list *ea_list,
3747 files_struct **result,
3748 int *pinfo)
3750 int info = FILE_WAS_OPENED;
3751 files_struct *fsp = NULL;
3752 NTSTATUS status;
3753 bool stream_name = false;
3755 DEBUG(10,("create_file: access_mask = 0x%x "
3756 "file_attributes = 0x%x, share_access = 0x%x, "
3757 "create_disposition = 0x%x create_options = 0x%x "
3758 "oplock_request = 0x%x "
3759 "private_flags = 0x%x "
3760 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3761 "fname = %s\n",
3762 (unsigned int)access_mask,
3763 (unsigned int)file_attributes,
3764 (unsigned int)share_access,
3765 (unsigned int)create_disposition,
3766 (unsigned int)create_options,
3767 (unsigned int)oplock_request,
3768 (unsigned int)private_flags,
3769 (unsigned int)root_dir_fid,
3770 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3773 * Calculate the filename from the root_dir_if if necessary.
3776 if (root_dir_fid != 0) {
3777 struct smb_filename *smb_fname_out = NULL;
3778 status = get_relative_fid_filename(conn, req, root_dir_fid,
3779 smb_fname, &smb_fname_out);
3780 if (!NT_STATUS_IS_OK(status)) {
3781 goto fail;
3783 smb_fname = smb_fname_out;
3787 * Check to see if this is a mac fork of some kind.
3790 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3791 if (stream_name) {
3792 enum FAKE_FILE_TYPE fake_file_type;
3794 fake_file_type = is_fake_file(smb_fname);
3796 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3799 * Here we go! support for changing the disk quotas
3800 * --metze
3802 * We need to fake up to open this MAGIC QUOTA file
3803 * and return a valid FID.
3805 * w2k close this file directly after openening xp
3806 * also tries a QUERY_FILE_INFO on the file and then
3807 * close it
3809 status = open_fake_file(req, conn, req->vuid,
3810 fake_file_type, smb_fname,
3811 access_mask, &fsp);
3812 if (!NT_STATUS_IS_OK(status)) {
3813 goto fail;
3816 ZERO_STRUCT(smb_fname->st);
3817 goto done;
3820 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3821 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3822 goto fail;
3826 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
3827 int ret;
3828 smb_fname->stream_name = NULL;
3829 /* We have to handle this error here. */
3830 if (create_options & FILE_DIRECTORY_FILE) {
3831 status = NT_STATUS_NOT_A_DIRECTORY;
3832 goto fail;
3834 if (lp_posix_pathnames()) {
3835 ret = SMB_VFS_LSTAT(conn, smb_fname);
3836 } else {
3837 ret = SMB_VFS_STAT(conn, smb_fname);
3840 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3841 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3842 goto fail;
3846 status = create_file_unixpath(
3847 conn, req, smb_fname, access_mask, share_access,
3848 create_disposition, create_options, file_attributes,
3849 oplock_request, allocation_size, private_flags,
3850 sd, ea_list,
3851 &fsp, &info);
3853 if (!NT_STATUS_IS_OK(status)) {
3854 goto fail;
3857 done:
3858 DEBUG(10, ("create_file: info=%d\n", info));
3860 *result = fsp;
3861 if (pinfo != NULL) {
3862 *pinfo = info;
3864 return NT_STATUS_OK;
3866 fail:
3867 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3869 if (fsp != NULL) {
3870 close_file(req, fsp, ERROR_CLOSE);
3871 fsp = NULL;
3873 return status;