s3: Replace an if with a boolean short circuit
[Samba.git] / source3 / smbd / open.c
blob7708833f97d9749c29f2d037a3c807add5969f40
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) != 0);
780 fsp->can_write =
781 CAN_WRITE(conn) &&
782 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
783 fsp->print_file = NULL;
784 fsp->modified = False;
785 fsp->sent_oplock_break = NO_BREAK_SENT;
786 fsp->is_directory = False;
787 if (conn->aio_write_behind_list &&
788 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
789 conn->case_sensitive)) {
790 fsp->aio_write_behind = True;
793 fsp->wcp = NULL; /* Write cache pointer. */
795 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
796 conn->session_info->unix_info->unix_name,
797 smb_fname_str_dbg(smb_fname),
798 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
799 conn->num_files_open));
801 errno = 0;
802 return NT_STATUS_OK;
805 /****************************************************************************
806 Check if we can open a file with a share mode.
807 Returns True if conflict, False if not.
808 ****************************************************************************/
810 static bool share_conflict(struct share_mode_entry *entry,
811 uint32 access_mask,
812 uint32 share_access)
814 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
815 "entry->share_access = 0x%x, "
816 "entry->private_options = 0x%x\n",
817 (unsigned int)entry->access_mask,
818 (unsigned int)entry->share_access,
819 (unsigned int)entry->private_options));
821 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
822 (unsigned int)access_mask, (unsigned int)share_access));
824 if ((entry->access_mask & (FILE_WRITE_DATA|
825 FILE_APPEND_DATA|
826 FILE_READ_DATA|
827 FILE_EXECUTE|
828 DELETE_ACCESS)) == 0) {
829 DEBUG(10,("share_conflict: No conflict due to "
830 "entry->access_mask = 0x%x\n",
831 (unsigned int)entry->access_mask ));
832 return False;
835 if ((access_mask & (FILE_WRITE_DATA|
836 FILE_APPEND_DATA|
837 FILE_READ_DATA|
838 FILE_EXECUTE|
839 DELETE_ACCESS)) == 0) {
840 DEBUG(10,("share_conflict: No conflict due to "
841 "access_mask = 0x%x\n",
842 (unsigned int)access_mask ));
843 return False;
846 #if 1 /* JRA TEST - Superdebug. */
847 #define CHECK_MASK(num, am, right, sa, share) \
848 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
849 (unsigned int)(num), (unsigned int)(am), \
850 (unsigned int)(right), (unsigned int)(am)&(right) )); \
851 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
852 (unsigned int)(num), (unsigned int)(sa), \
853 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
854 if (((am) & (right)) && !((sa) & (share))) { \
855 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
856 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
857 (unsigned int)(share) )); \
858 return True; \
860 #else
861 #define CHECK_MASK(num, am, right, sa, share) \
862 if (((am) & (right)) && !((sa) & (share))) { \
863 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
864 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
865 (unsigned int)(share) )); \
866 return True; \
868 #endif
870 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
871 share_access, FILE_SHARE_WRITE);
872 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
873 entry->share_access, FILE_SHARE_WRITE);
875 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
876 share_access, FILE_SHARE_READ);
877 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
878 entry->share_access, FILE_SHARE_READ);
880 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
881 share_access, FILE_SHARE_DELETE);
882 CHECK_MASK(6, access_mask, DELETE_ACCESS,
883 entry->share_access, FILE_SHARE_DELETE);
885 DEBUG(10,("share_conflict: No conflict.\n"));
886 return False;
889 #if defined(DEVELOPER)
890 static void validate_my_share_entries(struct smbd_server_connection *sconn,
891 int num,
892 struct share_mode_entry *share_entry)
894 struct server_id self = messaging_server_id(sconn->msg_ctx);
895 files_struct *fsp;
897 if (!procid_equal(&self, &share_entry->pid)) {
898 return;
901 if (is_deferred_open_entry(share_entry) &&
902 !open_was_deferred(sconn, share_entry->op_mid)) {
903 char *str = talloc_asprintf(talloc_tos(),
904 "Got a deferred entry without a request: "
905 "PANIC: %s\n",
906 share_mode_str(talloc_tos(), num, share_entry));
907 smb_panic(str);
910 if (!is_valid_share_mode_entry(share_entry)) {
911 return;
914 fsp = file_find_dif(sconn, share_entry->id,
915 share_entry->share_file_id);
916 if (!fsp) {
917 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
918 share_mode_str(talloc_tos(), num, share_entry) ));
919 smb_panic("validate_my_share_entries: Cannot match a "
920 "share entry with an open file\n");
923 if (is_deferred_open_entry(share_entry)) {
924 goto panic;
927 if ((share_entry->op_type == NO_OPLOCK) &&
928 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
929 /* Someone has already written to it, but I haven't yet
930 * noticed */
931 return;
934 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
935 goto panic;
938 return;
940 panic:
942 char *str;
943 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
944 share_mode_str(talloc_tos(), num, share_entry) ));
945 str = talloc_asprintf(talloc_tos(),
946 "validate_my_share_entries: "
947 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
948 fsp->fsp_name->base_name,
949 (unsigned int)fsp->oplock_type,
950 (unsigned int)share_entry->op_type );
951 smb_panic(str);
954 #endif
956 bool is_stat_open(uint32 access_mask)
958 return (access_mask &&
959 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
960 FILE_WRITE_ATTRIBUTES))==0) &&
961 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
962 FILE_WRITE_ATTRIBUTES)) != 0));
965 /****************************************************************************
966 Deal with share modes
967 Invarient: Share mode must be locked on entry and exit.
968 Returns -1 on error, or number of share modes on success (may be zero).
969 ****************************************************************************/
971 static NTSTATUS open_mode_check(connection_struct *conn,
972 struct share_mode_lock *lck,
973 uint32_t name_hash,
974 uint32 access_mask,
975 uint32 share_access,
976 uint32 create_options,
977 bool *file_existed)
979 int i;
981 if(lck->data->num_share_modes == 0) {
982 return NT_STATUS_OK;
985 /* A delete on close prohibits everything */
987 if (is_delete_on_close_set(lck, name_hash)) {
989 * Check the delete on close token
990 * is valid. It could have been left
991 * after a server crash.
993 for(i = 0; i < lck->data->num_share_modes; i++) {
994 if (!share_mode_stale_pid(lck->data, i)) {
996 *file_existed = true;
998 return NT_STATUS_DELETE_PENDING;
1001 return NT_STATUS_OK;
1004 if (is_stat_open(access_mask)) {
1005 /* Stat open that doesn't trigger oplock breaks or share mode
1006 * checks... ! JRA. */
1007 return NT_STATUS_OK;
1011 * Check if the share modes will give us access.
1014 #if defined(DEVELOPER)
1015 for(i = 0; i < lck->data->num_share_modes; i++) {
1016 validate_my_share_entries(conn->sconn, i,
1017 &lck->data->share_modes[i]);
1019 #endif
1021 if (!lp_share_modes(SNUM(conn))) {
1022 return NT_STATUS_OK;
1025 /* Now we check the share modes, after any oplock breaks. */
1026 for(i = 0; i < lck->data->num_share_modes; i++) {
1028 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1029 continue;
1032 /* someone else has a share lock on it, check to see if we can
1033 * too */
1034 if (share_conflict(&lck->data->share_modes[i],
1035 access_mask, share_access)) {
1037 if (share_mode_stale_pid(lck->data, i)) {
1038 continue;
1041 *file_existed = true;
1043 return NT_STATUS_SHARING_VIOLATION;
1047 if (lck->data->num_share_modes != 0) {
1048 *file_existed = true;
1051 return NT_STATUS_OK;
1054 static bool is_delete_request(files_struct *fsp) {
1055 return ((fsp->access_mask == DELETE_ACCESS) &&
1056 (fsp->oplock_type == NO_OPLOCK));
1060 * Send a break message to the oplock holder and delay the open for
1061 * our client.
1064 static NTSTATUS send_break_message(files_struct *fsp,
1065 struct share_mode_entry *exclusive,
1066 uint64_t mid,
1067 int oplock_request)
1069 NTSTATUS status;
1070 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1072 DEBUG(10, ("Sending break request to PID %s\n",
1073 procid_str_static(&exclusive->pid)));
1074 exclusive->op_mid = mid;
1076 /* Create the message. */
1077 share_mode_entry_to_message(msg, exclusive);
1079 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1080 don't want this set in the share mode struct pointed to by lck. */
1082 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1083 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1084 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1087 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1088 MSG_SMB_BREAK_REQUEST,
1089 (uint8 *)msg,
1090 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1091 if (!NT_STATUS_IS_OK(status)) {
1092 DEBUG(3, ("Could not send oplock break message: %s\n",
1093 nt_errstr(status)));
1096 return status;
1100 * Return share_mode_entry pointers for :
1101 * 1). Batch oplock entry.
1102 * 2). Batch or exclusive oplock entry (may be identical to #1).
1103 * bool have_level2_oplock
1104 * bool have_no_oplock.
1105 * Do internal consistency checks on the share mode for a file.
1108 static void find_oplock_types(files_struct *fsp,
1109 int oplock_request,
1110 const struct share_mode_lock *lck,
1111 struct share_mode_entry **pp_batch,
1112 struct share_mode_entry **pp_ex_or_batch,
1113 bool *got_level2,
1114 bool *got_no_oplock)
1116 int i;
1118 *pp_batch = NULL;
1119 *pp_ex_or_batch = NULL;
1120 *got_level2 = false;
1121 *got_no_oplock = false;
1123 /* Ignore stat or internal opens, as is done in
1124 delay_for_batch_oplocks() and
1125 delay_for_exclusive_oplocks().
1127 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1128 return;
1131 for (i=0; i<lck->data->num_share_modes; i++) {
1132 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1133 continue;
1136 if (lck->data->share_modes[i].op_type == NO_OPLOCK &&
1137 is_stat_open(lck->data->share_modes[i].access_mask)) {
1138 /* We ignore stat opens in the table - they
1139 always have NO_OPLOCK and never get or
1140 cause breaks. JRA. */
1141 continue;
1144 if (BATCH_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1145 /* batch - can only be one. */
1146 if (share_mode_stale_pid(lck->data, i)) {
1147 DEBUG(10, ("Found stale batch oplock\n"));
1148 continue;
1150 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1151 smb_panic("Bad batch oplock entry.");
1153 *pp_batch = &lck->data->share_modes[i];
1156 if (EXCLUSIVE_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1157 if (share_mode_stale_pid(lck->data, i)) {
1158 DEBUG(10, ("Found stale duplicate oplock\n"));
1159 continue;
1161 /* Exclusive or batch - can only be one. */
1162 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1163 smb_panic("Bad exclusive or batch oplock entry.");
1165 *pp_ex_or_batch = &lck->data->share_modes[i];
1168 if (LEVEL_II_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1169 if (*pp_batch || *pp_ex_or_batch) {
1170 if (share_mode_stale_pid(lck->data, i)) {
1171 DEBUG(10, ("Found stale LevelII "
1172 "oplock\n"));
1173 continue;
1175 smb_panic("Bad levelII oplock entry.");
1177 *got_level2 = true;
1180 if (lck->data->share_modes[i].op_type == NO_OPLOCK) {
1181 if (*pp_batch || *pp_ex_or_batch) {
1182 if (share_mode_stale_pid(lck->data, i)) {
1183 DEBUG(10, ("Found stale NO_OPLOCK "
1184 "entry\n"));
1185 continue;
1187 smb_panic("Bad no oplock entry.");
1189 *got_no_oplock = true;
1194 static bool delay_for_batch_oplocks(files_struct *fsp,
1195 uint64_t mid,
1196 int oplock_request,
1197 struct share_mode_entry *batch_entry)
1199 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1200 return false;
1202 if (batch_entry == NULL) {
1203 return false;
1206 /* Found a batch oplock */
1207 send_break_message(fsp, batch_entry, mid, oplock_request);
1208 return true;
1211 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1212 uint64_t mid,
1213 int oplock_request,
1214 struct share_mode_entry *ex_entry)
1216 bool delay_it;
1218 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1219 return false;
1221 if (ex_entry == NULL) {
1222 return false;
1225 /* Found an exclusive or batch oplock */
1227 delay_it = is_delete_request(fsp) ?
1228 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1230 if (!delay_it) {
1231 return false;
1234 send_break_message(fsp, ex_entry, mid, oplock_request);
1235 return true;
1238 static bool file_has_brlocks(files_struct *fsp)
1240 struct byte_range_lock *br_lck;
1242 br_lck = brl_get_locks_readonly(fsp);
1243 if (!br_lck)
1244 return false;
1246 return br_lck->num_locks > 0 ? true : false;
1249 static void grant_fsp_oplock_type(files_struct *fsp,
1250 int oplock_request,
1251 bool got_level2_oplock,
1252 bool got_a_none_oplock)
1254 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1255 lp_level2_oplocks(SNUM(fsp->conn));
1257 /* Start by granting what the client asked for,
1258 but ensure no SAMBA_PRIVATE bits can be set. */
1259 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1261 if (oplock_request & INTERNAL_OPEN_ONLY) {
1262 /* No oplocks on internal open. */
1263 fsp->oplock_type = NO_OPLOCK;
1264 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1265 fsp->oplock_type, fsp_str_dbg(fsp)));
1266 return;
1267 } else if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1268 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1269 fsp_str_dbg(fsp)));
1270 fsp->oplock_type = NO_OPLOCK;
1273 if (is_stat_open(fsp->access_mask)) {
1274 /* Leave the value already set. */
1275 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1276 fsp->oplock_type, fsp_str_dbg(fsp)));
1277 return;
1281 * Match what was requested (fsp->oplock_type) with
1282 * what was found in the existing share modes.
1285 if (got_a_none_oplock) {
1286 fsp->oplock_type = NO_OPLOCK;
1287 } else if (got_level2_oplock) {
1288 if (fsp->oplock_type == NO_OPLOCK ||
1289 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1290 /* Store a level2 oplock, but don't tell the client */
1291 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1292 } else {
1293 fsp->oplock_type = LEVEL_II_OPLOCK;
1295 } else {
1296 /* All share_mode_entries are placeholders or deferred.
1297 * Silently upgrade to fake levelII if the client didn't
1298 * ask for an oplock. */
1299 if (fsp->oplock_type == NO_OPLOCK) {
1300 /* Store a level2 oplock, but don't tell the client */
1301 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1306 * Don't grant level2 to clients that don't want them
1307 * or if we've turned them off.
1309 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1310 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1313 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1314 fsp->oplock_type, fsp_str_dbg(fsp)));
1317 bool request_timed_out(struct timeval request_time,
1318 struct timeval timeout)
1320 struct timeval now, end_time;
1321 GetTimeOfDay(&now);
1322 end_time = timeval_sum(&request_time, &timeout);
1323 return (timeval_compare(&end_time, &now) < 0);
1326 /****************************************************************************
1327 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1328 ****************************************************************************/
1330 static void defer_open(struct share_mode_lock *lck,
1331 struct timeval request_time,
1332 struct timeval timeout,
1333 struct smb_request *req,
1334 struct deferred_open_record *state)
1336 struct server_id self = messaging_server_id(req->sconn->msg_ctx);
1337 int i;
1339 /* Paranoia check */
1341 for (i=0; i<lck->data->num_share_modes; i++) {
1342 struct share_mode_entry *e = &lck->data->share_modes[i];
1344 if (is_deferred_open_entry(e) &&
1345 procid_equal(&self, &e->pid) &&
1346 (e->op_mid == req->mid)) {
1347 DEBUG(0, ("Trying to defer an already deferred "
1348 "request: mid=%llu, exiting\n",
1349 (unsigned long long)req->mid));
1350 exit_server("attempt to defer a deferred request");
1354 /* End paranoia check */
1356 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1357 "open entry for mid %llu\n",
1358 (unsigned int)request_time.tv_sec,
1359 (unsigned int)request_time.tv_usec,
1360 (unsigned long long)req->mid));
1362 if (!push_deferred_open_message_smb(req, request_time, timeout,
1363 state->id, (char *)state, sizeof(*state))) {
1364 exit_server("push_deferred_open_message_smb failed");
1366 add_deferred_open(lck, req->mid, request_time, self, state->id);
1370 /****************************************************************************
1371 On overwrite open ensure that the attributes match.
1372 ****************************************************************************/
1374 bool open_match_attributes(connection_struct *conn,
1375 uint32 old_dos_attr,
1376 uint32 new_dos_attr,
1377 mode_t existing_unx_mode,
1378 mode_t new_unx_mode,
1379 mode_t *returned_unx_mode)
1381 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1383 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1384 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1386 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1387 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1388 *returned_unx_mode = new_unx_mode;
1389 } else {
1390 *returned_unx_mode = (mode_t)0;
1393 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1394 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1395 "returned_unx_mode = 0%o\n",
1396 (unsigned int)old_dos_attr,
1397 (unsigned int)existing_unx_mode,
1398 (unsigned int)new_dos_attr,
1399 (unsigned int)*returned_unx_mode ));
1401 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1402 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1403 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1404 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1405 return False;
1408 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1409 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1410 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1411 return False;
1414 return True;
1417 /****************************************************************************
1418 Special FCB or DOS processing in the case of a sharing violation.
1419 Try and find a duplicated file handle.
1420 ****************************************************************************/
1422 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1423 connection_struct *conn,
1424 files_struct *fsp_to_dup_into,
1425 const struct smb_filename *smb_fname,
1426 struct file_id id,
1427 uint16 file_pid,
1428 uint64_t vuid,
1429 uint32 access_mask,
1430 uint32 share_access,
1431 uint32 create_options)
1433 files_struct *fsp;
1435 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1436 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1438 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1439 fsp = file_find_di_next(fsp)) {
1441 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1442 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1443 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1444 fsp->fh->fd, (unsigned long long)fsp->vuid,
1445 (unsigned int)fsp->file_pid,
1446 (unsigned int)fsp->fh->private_options,
1447 (unsigned int)fsp->access_mask ));
1449 if (fsp->fh->fd != -1 &&
1450 fsp->vuid == vuid &&
1451 fsp->file_pid == file_pid &&
1452 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1453 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1454 (fsp->access_mask & FILE_WRITE_DATA) &&
1455 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1456 strequal(fsp->fsp_name->stream_name,
1457 smb_fname->stream_name)) {
1458 DEBUG(10,("fcb_or_dos_open: file match\n"));
1459 break;
1463 if (!fsp) {
1464 return NT_STATUS_NOT_FOUND;
1467 /* quite an insane set of semantics ... */
1468 if (is_executable(smb_fname->base_name) &&
1469 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1470 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1471 return NT_STATUS_INVALID_PARAMETER;
1474 /* We need to duplicate this fsp. */
1475 return dup_file_fsp(req, fsp, access_mask, share_access,
1476 create_options, fsp_to_dup_into);
1479 static void schedule_defer_open(struct share_mode_lock *lck,
1480 struct timeval request_time,
1481 struct smb_request *req)
1483 struct deferred_open_record state;
1485 /* This is a relative time, added to the absolute
1486 request_time value to get the absolute timeout time.
1487 Note that if this is the second or greater time we enter
1488 this codepath for this particular request mid then
1489 request_time is left as the absolute time of the *first*
1490 time this request mid was processed. This is what allows
1491 the request to eventually time out. */
1493 struct timeval timeout;
1495 /* Normally the smbd we asked should respond within
1496 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1497 * the client did, give twice the timeout as a safety
1498 * measure here in case the other smbd is stuck
1499 * somewhere else. */
1501 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1503 /* Nothing actually uses state.delayed_for_oplocks
1504 but it's handy to differentiate in debug messages
1505 between a 30 second delay due to oplock break, and
1506 a 1 second delay for share mode conflicts. */
1508 state.delayed_for_oplocks = True;
1509 state.id = lck->data->id;
1511 if (!request_timed_out(request_time, timeout)) {
1512 defer_open(lck, request_time, timeout, req, &state);
1516 /****************************************************************************
1517 Work out what access_mask to use from what the client sent us.
1518 ****************************************************************************/
1520 static NTSTATUS smbd_calculate_maximum_allowed_access(
1521 connection_struct *conn,
1522 const struct smb_filename *smb_fname,
1523 uint32_t *p_access_mask)
1525 struct security_descriptor *sd;
1526 uint32_t access_granted;
1527 NTSTATUS status;
1529 if (get_current_uid(conn) == (uid_t)0) {
1530 *p_access_mask |= FILE_GENERIC_ALL;
1531 return NT_STATUS_OK;
1534 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1535 (SECINFO_OWNER |
1536 SECINFO_GROUP |
1537 SECINFO_DACL),&sd);
1539 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1541 * File did not exist
1543 *p_access_mask = FILE_GENERIC_ALL;
1544 return NT_STATUS_OK;
1546 if (!NT_STATUS_IS_OK(status)) {
1547 DEBUG(10,("smbd_calculate_access_mask: "
1548 "Could not get acl on file %s: %s\n",
1549 smb_fname_str_dbg(smb_fname),
1550 nt_errstr(status)));
1551 return NT_STATUS_ACCESS_DENIED;
1555 * Never test FILE_READ_ATTRIBUTES. se_access_check()
1556 * also takes care of owner WRITE_DAC and READ_CONTROL.
1558 status = se_access_check(sd,
1559 get_current_nttok(conn),
1560 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1561 &access_granted);
1563 TALLOC_FREE(sd);
1565 if (!NT_STATUS_IS_OK(status)) {
1566 DEBUG(10, ("smbd_calculate_access_mask: "
1567 "Access denied on file %s: "
1568 "when calculating maximum access\n",
1569 smb_fname_str_dbg(smb_fname)));
1570 return NT_STATUS_ACCESS_DENIED;
1572 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1573 return NT_STATUS_OK;
1576 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1577 const struct smb_filename *smb_fname,
1578 uint32_t access_mask,
1579 uint32_t *access_mask_out)
1581 NTSTATUS status;
1582 uint32_t orig_access_mask = access_mask;
1583 uint32_t rejected_share_access;
1586 * Convert GENERIC bits to specific bits.
1589 se_map_generic(&access_mask, &file_generic_mapping);
1591 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1592 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1594 status = smbd_calculate_maximum_allowed_access(
1595 conn, smb_fname, &access_mask);
1597 if (!NT_STATUS_IS_OK(status)) {
1598 return status;
1601 access_mask &= conn->share_access;
1604 rejected_share_access = access_mask & ~(conn->share_access);
1606 if (rejected_share_access) {
1607 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1608 "file %s: rejected by share access mask[0x%08X] "
1609 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1610 smb_fname_str_dbg(smb_fname),
1611 conn->share_access,
1612 orig_access_mask, access_mask,
1613 rejected_share_access));
1614 return NT_STATUS_ACCESS_DENIED;
1617 *access_mask_out = access_mask;
1618 return NT_STATUS_OK;
1621 /****************************************************************************
1622 Remove the deferred open entry under lock.
1623 ****************************************************************************/
1625 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1626 struct server_id pid)
1628 struct share_mode_lock *lck = get_existing_share_mode_lock(
1629 talloc_tos(), id);
1630 if (lck == NULL) {
1631 DEBUG(0, ("could not get share mode lock\n"));
1632 return;
1634 del_deferred_open_entry(lck, mid, pid);
1635 TALLOC_FREE(lck);
1638 /****************************************************************************
1639 Open a file with a share mode. Passed in an already created files_struct *.
1640 ****************************************************************************/
1642 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1643 struct smb_request *req,
1644 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1645 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1646 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1647 uint32 create_options, /* options such as delete on close. */
1648 uint32 new_dos_attributes, /* attributes used for new file. */
1649 int oplock_request, /* internal Samba oplock codes. */
1650 /* Information (FILE_EXISTS etc.) */
1651 uint32_t private_flags, /* Samba specific flags. */
1652 int *pinfo,
1653 files_struct *fsp)
1655 struct smb_filename *smb_fname = fsp->fsp_name;
1656 int flags=0;
1657 int flags2=0;
1658 bool file_existed = VALID_STAT(smb_fname->st);
1659 bool def_acl = False;
1660 bool posix_open = False;
1661 bool new_file_created = False;
1662 bool clear_ads = false;
1663 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1664 mode_t new_unx_mode = (mode_t)0;
1665 mode_t unx_mode = (mode_t)0;
1666 int info;
1667 uint32 existing_dos_attributes = 0;
1668 struct timeval request_time = timeval_zero();
1669 struct share_mode_lock *lck = NULL;
1670 uint32 open_access_mask = access_mask;
1671 NTSTATUS status;
1672 char *parent_dir;
1674 if (conn->printer) {
1676 * Printers are handled completely differently.
1677 * Most of the passed parameters are ignored.
1680 if (pinfo) {
1681 *pinfo = FILE_WAS_CREATED;
1684 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1685 smb_fname_str_dbg(smb_fname)));
1687 if (!req) {
1688 DEBUG(0,("open_file_ntcreate: printer open without "
1689 "an SMB request!\n"));
1690 return NT_STATUS_INTERNAL_ERROR;
1693 return print_spool_open(fsp, smb_fname->base_name,
1694 req->vuid);
1697 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1698 NULL)) {
1699 return NT_STATUS_NO_MEMORY;
1702 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1703 posix_open = True;
1704 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1705 new_dos_attributes = 0;
1706 } else {
1707 /* Windows allows a new file to be created and
1708 silently removes a FILE_ATTRIBUTE_DIRECTORY
1709 sent by the client. Do the same. */
1711 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1713 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1714 * created new. */
1715 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1716 smb_fname, parent_dir);
1719 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1720 "access_mask=0x%x share_access=0x%x "
1721 "create_disposition = 0x%x create_options=0x%x "
1722 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1723 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1724 access_mask, share_access, create_disposition,
1725 create_options, (unsigned int)unx_mode, oplock_request,
1726 (unsigned int)private_flags));
1728 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1729 DEBUG(0, ("No smb request but not an internal only open!\n"));
1730 return NT_STATUS_INTERNAL_ERROR;
1734 * Only non-internal opens can be deferred at all
1737 if (req) {
1738 void *ptr;
1739 if (get_deferred_open_message_state(req,
1740 &request_time,
1741 &ptr)) {
1743 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1744 /* Remember the absolute time of the original
1745 request with this mid. We'll use it later to
1746 see if this has timed out. */
1748 /* Remove the deferred open entry under lock. */
1749 remove_deferred_open_entry(
1750 state->id, req->mid,
1751 messaging_server_id(req->sconn->msg_ctx));
1753 /* Ensure we don't reprocess this message. */
1754 remove_deferred_open_message_smb(req->sconn, req->mid);
1758 if (!posix_open) {
1759 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1760 if (file_existed) {
1761 existing_dos_attributes = dos_mode(conn, smb_fname);
1765 /* ignore any oplock requests if oplocks are disabled */
1766 if (!lp_oplocks(SNUM(conn)) ||
1767 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1768 /* Mask off everything except the private Samba bits. */
1769 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1772 /* this is for OS/2 long file names - say we don't support them */
1773 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1774 /* OS/2 Workplace shell fix may be main code stream in a later
1775 * release. */
1776 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1777 "supported.\n"));
1778 if (use_nt_status()) {
1779 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1781 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1784 switch( create_disposition ) {
1786 * Currently we're using FILE_SUPERSEDE as the same as
1787 * FILE_OVERWRITE_IF but they really are
1788 * different. FILE_SUPERSEDE deletes an existing file
1789 * (requiring delete access) then recreates it.
1791 case FILE_SUPERSEDE:
1792 /* If file exists replace/overwrite. If file doesn't
1793 * exist create. */
1794 flags2 |= (O_CREAT | O_TRUNC);
1795 clear_ads = true;
1796 break;
1798 case FILE_OVERWRITE_IF:
1799 /* If file exists replace/overwrite. If file doesn't
1800 * exist create. */
1801 flags2 |= (O_CREAT | O_TRUNC);
1802 clear_ads = true;
1803 break;
1805 case FILE_OPEN:
1806 /* If file exists open. If file doesn't exist error. */
1807 if (!file_existed) {
1808 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1809 "requested for file %s and file "
1810 "doesn't exist.\n",
1811 smb_fname_str_dbg(smb_fname)));
1812 errno = ENOENT;
1813 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1815 break;
1817 case FILE_OVERWRITE:
1818 /* If file exists overwrite. If file doesn't exist
1819 * error. */
1820 if (!file_existed) {
1821 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1822 "requested for file %s and file "
1823 "doesn't exist.\n",
1824 smb_fname_str_dbg(smb_fname) ));
1825 errno = ENOENT;
1826 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1828 flags2 |= O_TRUNC;
1829 clear_ads = true;
1830 break;
1832 case FILE_CREATE:
1833 /* If file exists error. If file doesn't exist
1834 * create. */
1835 if (file_existed) {
1836 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1837 "requested for file %s and file "
1838 "already exists.\n",
1839 smb_fname_str_dbg(smb_fname)));
1840 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1841 errno = EISDIR;
1842 } else {
1843 errno = EEXIST;
1845 return map_nt_error_from_unix(errno);
1847 flags2 |= (O_CREAT|O_EXCL);
1848 break;
1850 case FILE_OPEN_IF:
1851 /* If file exists open. If file doesn't exist
1852 * create. */
1853 flags2 |= O_CREAT;
1854 break;
1856 default:
1857 return NT_STATUS_INVALID_PARAMETER;
1860 /* We only care about matching attributes on file exists and
1861 * overwrite. */
1863 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1864 (create_disposition == FILE_OVERWRITE_IF))) {
1865 if (!open_match_attributes(conn, existing_dos_attributes,
1866 new_dos_attributes,
1867 smb_fname->st.st_ex_mode,
1868 unx_mode, &new_unx_mode)) {
1869 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1870 "for file %s (%x %x) (0%o, 0%o)\n",
1871 smb_fname_str_dbg(smb_fname),
1872 existing_dos_attributes,
1873 new_dos_attributes,
1874 (unsigned int)smb_fname->st.st_ex_mode,
1875 (unsigned int)unx_mode ));
1876 errno = EACCES;
1877 return NT_STATUS_ACCESS_DENIED;
1881 status = smbd_calculate_access_mask(conn, smb_fname,
1882 access_mask,
1883 &access_mask);
1884 if (!NT_STATUS_IS_OK(status)) {
1885 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1886 "on file %s returned %s\n",
1887 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1888 return status;
1891 open_access_mask = access_mask;
1893 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1894 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1897 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1898 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1899 access_mask));
1902 * Note that we ignore the append flag as append does not
1903 * mean the same thing under DOS and Unix.
1906 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1907 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1908 /* DENY_DOS opens are always underlying read-write on the
1909 file handle, no matter what the requested access mask
1910 says. */
1911 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1912 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1913 flags = O_RDWR;
1914 } else {
1915 flags = O_WRONLY;
1917 } else {
1918 flags = O_RDONLY;
1922 * Currently we only look at FILE_WRITE_THROUGH for create options.
1925 #if defined(O_SYNC)
1926 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1927 flags2 |= O_SYNC;
1929 #endif /* O_SYNC */
1931 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1932 flags2 |= O_APPEND;
1935 if (!posix_open && !CAN_WRITE(conn)) {
1937 * We should really return a permission denied error if either
1938 * O_CREAT or O_TRUNC are set, but for compatibility with
1939 * older versions of Samba we just AND them out.
1941 flags2 &= ~(O_CREAT|O_TRUNC);
1945 * Ensure we can't write on a read-only share or file.
1948 if (flags != O_RDONLY && file_existed &&
1949 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1950 DEBUG(5,("open_file_ntcreate: write access requested for "
1951 "file %s on read only %s\n",
1952 smb_fname_str_dbg(smb_fname),
1953 !CAN_WRITE(conn) ? "share" : "file" ));
1954 errno = EACCES;
1955 return NT_STATUS_ACCESS_DENIED;
1958 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1959 fsp->share_access = share_access;
1960 fsp->fh->private_options = private_flags;
1961 fsp->access_mask = open_access_mask; /* We change this to the
1962 * requested access_mask after
1963 * the open is done. */
1964 fsp->posix_open = posix_open;
1966 /* Ensure no SAMBA_PRIVATE bits can be set. */
1967 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1969 if (timeval_is_zero(&request_time)) {
1970 request_time = fsp->open_time;
1973 if (file_existed) {
1974 struct share_mode_entry *batch_entry = NULL;
1975 struct share_mode_entry *exclusive_entry = NULL;
1976 bool got_level2_oplock = false;
1977 bool got_a_none_oplock = false;
1978 struct file_id id;
1980 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1981 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1983 lck = get_share_mode_lock(talloc_tos(), id,
1984 conn->connectpath,
1985 smb_fname, &old_write_time);
1986 if (lck == NULL) {
1987 DEBUG(0, ("Could not get share mode lock\n"));
1988 return NT_STATUS_SHARING_VIOLATION;
1991 /* Get the types we need to examine. */
1992 find_oplock_types(fsp,
1993 oplock_request,
1994 lck,
1995 &batch_entry,
1996 &exclusive_entry,
1997 &got_level2_oplock,
1998 &got_a_none_oplock);
2000 /* First pass - send break only on batch oplocks. */
2001 if ((req != NULL) &&
2002 delay_for_batch_oplocks(fsp,
2003 req->mid,
2004 oplock_request,
2005 batch_entry)) {
2006 schedule_defer_open(lck, request_time, req);
2007 TALLOC_FREE(lck);
2008 return NT_STATUS_SHARING_VIOLATION;
2011 /* Use the client requested access mask here, not the one we
2012 * open with. */
2013 status = open_mode_check(conn, lck, fsp->name_hash,
2014 access_mask, share_access,
2015 create_options, &file_existed);
2017 if (NT_STATUS_IS_OK(status)) {
2018 /* We might be going to allow this open. Check oplock
2019 * status again. */
2020 /* Second pass - send break for both batch or
2021 * exclusive oplocks. */
2022 if ((req != NULL) &&
2023 delay_for_exclusive_oplocks(
2024 fsp,
2025 req->mid,
2026 oplock_request,
2027 exclusive_entry)) {
2028 schedule_defer_open(lck, request_time, req);
2029 TALLOC_FREE(lck);
2030 return NT_STATUS_SHARING_VIOLATION;
2034 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2035 /* DELETE_PENDING is not deferred for a second */
2036 TALLOC_FREE(lck);
2037 return status;
2040 grant_fsp_oplock_type(fsp,
2041 oplock_request,
2042 got_level2_oplock,
2043 got_a_none_oplock);
2045 if (!NT_STATUS_IS_OK(status)) {
2046 uint32 can_access_mask;
2047 bool can_access = True;
2049 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2051 /* Check if this can be done with the deny_dos and fcb
2052 * calls. */
2053 if (private_flags &
2054 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2055 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2056 if (req == NULL) {
2057 DEBUG(0, ("DOS open without an SMB "
2058 "request!\n"));
2059 TALLOC_FREE(lck);
2060 return NT_STATUS_INTERNAL_ERROR;
2063 /* Use the client requested access mask here,
2064 * not the one we open with. */
2065 status = fcb_or_dos_open(req,
2066 conn,
2067 fsp,
2068 smb_fname,
2070 req->smbpid,
2071 req->vuid,
2072 access_mask,
2073 share_access,
2074 create_options);
2076 if (NT_STATUS_IS_OK(status)) {
2077 TALLOC_FREE(lck);
2078 if (pinfo) {
2079 *pinfo = FILE_WAS_OPENED;
2081 return NT_STATUS_OK;
2086 * This next line is a subtlety we need for
2087 * MS-Access. If a file open will fail due to share
2088 * permissions and also for security (access) reasons,
2089 * we need to return the access failed error, not the
2090 * share error. We can't open the file due to kernel
2091 * oplock deadlock (it's possible we failed above on
2092 * the open_mode_check()) so use a userspace check.
2095 if (flags & O_RDWR) {
2096 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2097 } else if (flags & O_WRONLY) {
2098 can_access_mask = FILE_WRITE_DATA;
2099 } else {
2100 can_access_mask = FILE_READ_DATA;
2103 if (((can_access_mask & FILE_WRITE_DATA) &&
2104 !CAN_WRITE(conn)) ||
2105 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2106 smb_fname, can_access_mask))) {
2107 can_access = False;
2111 * If we're returning a share violation, ensure we
2112 * cope with the braindead 1 second delay.
2115 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2116 lp_defer_sharing_violations()) {
2117 struct timeval timeout;
2118 struct deferred_open_record state;
2119 int timeout_usecs;
2121 /* this is a hack to speed up torture tests
2122 in 'make test' */
2123 timeout_usecs = lp_parm_int(SNUM(conn),
2124 "smbd","sharedelay",
2125 SHARING_VIOLATION_USEC_WAIT);
2127 /* This is a relative time, added to the absolute
2128 request_time value to get the absolute timeout time.
2129 Note that if this is the second or greater time we enter
2130 this codepath for this particular request mid then
2131 request_time is left as the absolute time of the *first*
2132 time this request mid was processed. This is what allows
2133 the request to eventually time out. */
2135 timeout = timeval_set(0, timeout_usecs);
2137 /* Nothing actually uses state.delayed_for_oplocks
2138 but it's handy to differentiate in debug messages
2139 between a 30 second delay due to oplock break, and
2140 a 1 second delay for share mode conflicts. */
2142 state.delayed_for_oplocks = False;
2143 state.id = id;
2145 if ((req != NULL)
2146 && !request_timed_out(request_time,
2147 timeout)) {
2148 defer_open(lck, request_time, timeout,
2149 req, &state);
2153 TALLOC_FREE(lck);
2154 if (can_access) {
2156 * We have detected a sharing violation here
2157 * so return the correct error code
2159 status = NT_STATUS_SHARING_VIOLATION;
2160 } else {
2161 status = NT_STATUS_ACCESS_DENIED;
2163 return status;
2167 * We exit this block with the share entry *locked*.....
2171 SMB_ASSERT(!file_existed || (lck != NULL));
2174 * Ensure we pay attention to default ACLs on directories if required.
2177 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2178 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2179 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2182 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2183 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2184 (unsigned int)flags, (unsigned int)flags2,
2185 (unsigned int)unx_mode, (unsigned int)access_mask,
2186 (unsigned int)open_access_mask));
2189 * open_file strips any O_TRUNC flags itself.
2192 fsp_open = open_file(fsp, conn, req, parent_dir,
2193 flags|flags2, unx_mode, access_mask,
2194 open_access_mask);
2196 if (!NT_STATUS_IS_OK(fsp_open)) {
2197 TALLOC_FREE(lck);
2198 return fsp_open;
2201 if (!file_existed) {
2202 struct share_mode_entry *batch_entry = NULL;
2203 struct share_mode_entry *exclusive_entry = NULL;
2204 bool got_level2_oplock = false;
2205 bool got_a_none_oplock = false;
2206 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2207 struct file_id id;
2209 * Deal with the race condition where two smbd's detect the
2210 * file doesn't exist and do the create at the same time. One
2211 * of them will win and set a share mode, the other (ie. this
2212 * one) should check if the requested share mode for this
2213 * create is allowed.
2217 * Now the file exists and fsp is successfully opened,
2218 * fsp->dev and fsp->inode are valid and should replace the
2219 * dev=0,inode=0 from a non existent file. Spotted by
2220 * Nadav Danieli <nadavd@exanet.com>. JRA.
2223 id = fsp->file_id;
2225 lck = get_share_mode_lock(talloc_tos(), id,
2226 conn->connectpath,
2227 smb_fname, &old_write_time);
2229 if (lck == NULL) {
2230 DEBUG(0, ("open_file_ntcreate: Could not get share "
2231 "mode lock for %s\n",
2232 smb_fname_str_dbg(smb_fname)));
2233 fd_close(fsp);
2234 return NT_STATUS_SHARING_VIOLATION;
2237 /* Get the types we need to examine. */
2238 find_oplock_types(fsp,
2239 oplock_request,
2240 lck,
2241 &batch_entry,
2242 &exclusive_entry,
2243 &got_level2_oplock,
2244 &got_a_none_oplock);
2246 /* First pass - send break only on batch oplocks. */
2247 if ((req != NULL) &&
2248 delay_for_batch_oplocks(fsp,
2249 req->mid,
2250 oplock_request,
2251 batch_entry)) {
2252 schedule_defer_open(lck, request_time, req);
2253 TALLOC_FREE(lck);
2254 fd_close(fsp);
2255 return NT_STATUS_SHARING_VIOLATION;
2258 status = open_mode_check(conn, lck, fsp->name_hash,
2259 access_mask, share_access,
2260 create_options, &file_existed);
2262 if (NT_STATUS_IS_OK(status)) {
2263 /* We might be going to allow this open. Check oplock
2264 * status again. */
2265 /* Second pass - send break for both batch or
2266 * exclusive oplocks. */
2267 if ((req != NULL) &&
2268 delay_for_exclusive_oplocks(
2269 fsp,
2270 req->mid,
2271 oplock_request,
2272 exclusive_entry)) {
2273 schedule_defer_open(lck, request_time, req);
2274 TALLOC_FREE(lck);
2275 fd_close(fsp);
2276 return NT_STATUS_SHARING_VIOLATION;
2280 if (!NT_STATUS_IS_OK(status)) {
2281 struct deferred_open_record state;
2283 state.delayed_for_oplocks = False;
2284 state.id = id;
2286 /* Do it all over again immediately. In the second
2287 * round we will find that the file existed and handle
2288 * the DELETE_PENDING and FCB cases correctly. No need
2289 * to duplicate the code here. Essentially this is a
2290 * "goto top of this function", but don't tell
2291 * anybody... */
2293 if (req != NULL) {
2294 defer_open(lck, request_time, timeval_zero(),
2295 req, &state);
2297 TALLOC_FREE(lck);
2298 fd_close(fsp);
2299 return status;
2302 grant_fsp_oplock_type(fsp,
2303 oplock_request,
2304 got_level2_oplock,
2305 got_a_none_oplock);
2308 * We exit this block with the share entry *locked*.....
2313 SMB_ASSERT(lck != NULL);
2315 /* Delete streams if create_disposition requires it */
2316 if (file_existed && clear_ads &&
2317 !is_ntfs_stream_smb_fname(smb_fname)) {
2318 status = delete_all_streams(conn, smb_fname->base_name);
2319 if (!NT_STATUS_IS_OK(status)) {
2320 TALLOC_FREE(lck);
2321 fd_close(fsp);
2322 return status;
2326 /* note that we ignore failure for the following. It is
2327 basically a hack for NFS, and NFS will never set one of
2328 these only read them. Nobody but Samba can ever set a deny
2329 mode and we have already checked our more authoritative
2330 locking database for permission to set this deny mode. If
2331 the kernel refuses the operations then the kernel is wrong.
2332 note that GPFS supports it as well - jmcd */
2334 if (fsp->fh->fd != -1) {
2335 int ret_flock;
2336 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2337 if(ret_flock == -1 ){
2339 TALLOC_FREE(lck);
2340 fd_close(fsp);
2342 return NT_STATUS_SHARING_VIOLATION;
2347 * At this point onwards, we can guarentee that the share entry
2348 * is locked, whether we created the file or not, and that the
2349 * deny mode is compatible with all current opens.
2353 * If requested, truncate the file.
2356 if (file_existed && (flags2&O_TRUNC)) {
2358 * We are modifing the file after open - update the stat
2359 * struct..
2361 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2362 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2363 status = map_nt_error_from_unix(errno);
2364 TALLOC_FREE(lck);
2365 fd_close(fsp);
2366 return status;
2371 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2372 * but we don't have to store this - just ignore it on access check.
2374 if (conn->sconn->using_smb2) {
2376 * SMB2 doesn't return it (according to Microsoft tests).
2377 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2378 * File created with access = 0x7 (Read, Write, Delete)
2379 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2381 fsp->access_mask = access_mask;
2382 } else {
2383 /* But SMB1 does. */
2384 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2387 if (file_existed) {
2388 /* stat opens on existing files don't get oplocks. */
2389 if (is_stat_open(open_access_mask)) {
2390 fsp->oplock_type = NO_OPLOCK;
2393 if (flags2 & O_TRUNC) {
2394 info = FILE_WAS_OVERWRITTEN;
2395 } else {
2396 info = FILE_WAS_OPENED;
2398 } else {
2399 info = FILE_WAS_CREATED;
2402 if (pinfo) {
2403 *pinfo = info;
2407 * Setup the oplock info in both the shared memory and
2408 * file structs.
2411 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2413 * Could not get the kernel oplock or there are byte-range
2414 * locks on the file.
2416 fsp->oplock_type = NO_OPLOCK;
2419 set_share_mode(lck, fsp, get_current_uid(conn),
2420 req ? req->mid : 0,
2421 fsp->oplock_type);
2423 /* Handle strange delete on close create semantics. */
2424 if (create_options & FILE_DELETE_ON_CLOSE) {
2426 status = can_set_delete_on_close(fsp, new_dos_attributes);
2428 if (!NT_STATUS_IS_OK(status)) {
2429 /* Remember to delete the mode we just added. */
2430 del_share_mode(lck, fsp);
2431 TALLOC_FREE(lck);
2432 fd_close(fsp);
2433 return status;
2435 /* Note that here we set the *inital* delete on close flag,
2436 not the regular one. The magic gets handled in close. */
2437 fsp->initial_delete_on_close = True;
2440 if (info == FILE_WAS_OVERWRITTEN
2441 || info == FILE_WAS_CREATED
2442 || info == FILE_WAS_SUPERSEDED) {
2443 new_file_created = True;
2446 if (new_file_created) {
2447 /* Files should be initially set as archive */
2448 if (lp_map_archive(SNUM(conn)) ||
2449 lp_store_dos_attributes(SNUM(conn))) {
2450 if (!posix_open) {
2451 if (file_set_dosmode(conn, smb_fname,
2452 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2453 parent_dir, true) == 0) {
2454 unx_mode = smb_fname->st.st_ex_mode;
2460 /* Determine sparse flag. */
2461 if (posix_open) {
2462 /* POSIX opens are sparse by default. */
2463 fsp->is_sparse = true;
2464 } else {
2465 fsp->is_sparse = (file_existed &&
2466 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2470 * Take care of inherited ACLs on created files - if default ACL not
2471 * selected.
2474 if (!posix_open && !file_existed && !def_acl) {
2476 int saved_errno = errno; /* We might get ENOSYS in the next
2477 * call.. */
2479 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2480 errno == ENOSYS) {
2481 errno = saved_errno; /* Ignore ENOSYS */
2484 } else if (new_unx_mode) {
2486 int ret = -1;
2488 /* Attributes need changing. File already existed. */
2491 int saved_errno = errno; /* We might get ENOSYS in the
2492 * next call.. */
2493 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2495 if (ret == -1 && errno == ENOSYS) {
2496 errno = saved_errno; /* Ignore ENOSYS */
2497 } else {
2498 DEBUG(5, ("open_file_ntcreate: reset "
2499 "attributes of file %s to 0%o\n",
2500 smb_fname_str_dbg(smb_fname),
2501 (unsigned int)new_unx_mode));
2502 ret = 0; /* Don't do the fchmod below. */
2506 if ((ret == -1) &&
2507 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2508 DEBUG(5, ("open_file_ntcreate: failed to reset "
2509 "attributes of file %s to 0%o\n",
2510 smb_fname_str_dbg(smb_fname),
2511 (unsigned int)new_unx_mode));
2514 /* If this is a successful open, we must remove any deferred open
2515 * records. */
2516 if (req != NULL) {
2517 del_deferred_open_entry(lck, req->mid,
2518 messaging_server_id(req->sconn->msg_ctx));
2520 TALLOC_FREE(lck);
2522 return NT_STATUS_OK;
2526 /****************************************************************************
2527 Open a file for for write to ensure that we can fchmod it.
2528 ****************************************************************************/
2530 NTSTATUS open_file_fchmod(connection_struct *conn,
2531 struct smb_filename *smb_fname,
2532 files_struct **result)
2534 if (!VALID_STAT(smb_fname->st)) {
2535 return NT_STATUS_INVALID_PARAMETER;
2538 return SMB_VFS_CREATE_FILE(
2539 conn, /* conn */
2540 NULL, /* req */
2541 0, /* root_dir_fid */
2542 smb_fname, /* fname */
2543 FILE_WRITE_DATA, /* access_mask */
2544 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2545 FILE_SHARE_DELETE),
2546 FILE_OPEN, /* create_disposition*/
2547 0, /* create_options */
2548 0, /* file_attributes */
2549 INTERNAL_OPEN_ONLY, /* oplock_request */
2550 0, /* allocation_size */
2551 0, /* private_flags */
2552 NULL, /* sd */
2553 NULL, /* ea_list */
2554 result, /* result */
2555 NULL); /* pinfo */
2558 static NTSTATUS mkdir_internal(connection_struct *conn,
2559 struct smb_filename *smb_dname,
2560 uint32 file_attributes)
2562 mode_t mode;
2563 char *parent_dir = NULL;
2564 NTSTATUS status;
2565 bool posix_open = false;
2566 bool need_re_stat = false;
2567 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2569 if(access_mask & ~(conn->share_access)) {
2570 DEBUG(5,("mkdir_internal: failing share access "
2571 "%s\n", lp_servicename(SNUM(conn))));
2572 return NT_STATUS_ACCESS_DENIED;
2575 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2576 NULL)) {
2577 return NT_STATUS_NO_MEMORY;
2580 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2581 posix_open = true;
2582 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2583 } else {
2584 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2587 status = check_parent_access(conn,
2588 smb_dname,
2589 access_mask);
2590 if(!NT_STATUS_IS_OK(status)) {
2591 DEBUG(5,("mkdir_internal: check_parent_access "
2592 "on directory %s for path %s returned %s\n",
2593 parent_dir,
2594 smb_dname->base_name,
2595 nt_errstr(status) ));
2596 return status;
2599 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2600 return map_nt_error_from_unix(errno);
2603 /* Ensure we're checking for a symlink here.... */
2604 /* We don't want to get caught by a symlink racer. */
2606 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2607 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2608 smb_fname_str_dbg(smb_dname), strerror(errno)));
2609 return map_nt_error_from_unix(errno);
2612 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2613 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2614 smb_fname_str_dbg(smb_dname)));
2615 return NT_STATUS_NOT_A_DIRECTORY;
2618 if (lp_store_dos_attributes(SNUM(conn))) {
2619 if (!posix_open) {
2620 file_set_dosmode(conn, smb_dname,
2621 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2622 parent_dir, true);
2626 if (lp_inherit_perms(SNUM(conn))) {
2627 inherit_access_posix_acl(conn, parent_dir,
2628 smb_dname->base_name, mode);
2629 need_re_stat = true;
2632 if (!posix_open) {
2634 * Check if high bits should have been set,
2635 * then (if bits are missing): add them.
2636 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2637 * dir.
2639 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2640 (mode & ~smb_dname->st.st_ex_mode)) {
2641 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2642 (smb_dname->st.st_ex_mode |
2643 (mode & ~smb_dname->st.st_ex_mode)));
2644 need_re_stat = true;
2648 /* Change the owner if required. */
2649 if (lp_inherit_owner(SNUM(conn))) {
2650 change_dir_owner_to_parent(conn, parent_dir,
2651 smb_dname->base_name,
2652 &smb_dname->st);
2653 need_re_stat = true;
2656 if (need_re_stat) {
2657 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2658 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2659 smb_fname_str_dbg(smb_dname), strerror(errno)));
2660 return map_nt_error_from_unix(errno);
2664 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2665 smb_dname->base_name);
2667 return NT_STATUS_OK;
2670 /****************************************************************************
2671 Ensure we didn't get symlink raced on opening a directory.
2672 ****************************************************************************/
2674 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2675 const SMB_STRUCT_STAT *sbuf2)
2677 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2678 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2679 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2680 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2681 return false;
2683 return true;
2686 /****************************************************************************
2687 Open a directory from an NT SMB call.
2688 ****************************************************************************/
2690 static NTSTATUS open_directory(connection_struct *conn,
2691 struct smb_request *req,
2692 struct smb_filename *smb_dname,
2693 uint32 access_mask,
2694 uint32 share_access,
2695 uint32 create_disposition,
2696 uint32 create_options,
2697 uint32 file_attributes,
2698 int *pinfo,
2699 files_struct **result)
2701 files_struct *fsp = NULL;
2702 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2703 struct share_mode_lock *lck = NULL;
2704 NTSTATUS status;
2705 struct timespec mtimespec;
2706 int info = 0;
2708 if (is_ntfs_stream_smb_fname(smb_dname)) {
2709 DEBUG(2, ("open_directory: %s is a stream name!\n",
2710 smb_fname_str_dbg(smb_dname)));
2711 return NT_STATUS_NOT_A_DIRECTORY;
2714 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2715 /* Ensure we have a directory attribute. */
2716 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2719 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2720 "share_access = 0x%x create_options = 0x%x, "
2721 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2722 smb_fname_str_dbg(smb_dname),
2723 (unsigned int)access_mask,
2724 (unsigned int)share_access,
2725 (unsigned int)create_options,
2726 (unsigned int)create_disposition,
2727 (unsigned int)file_attributes));
2729 status = smbd_calculate_access_mask(conn, smb_dname,
2730 access_mask, &access_mask);
2731 if (!NT_STATUS_IS_OK(status)) {
2732 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2733 "on file %s returned %s\n",
2734 smb_fname_str_dbg(smb_dname),
2735 nt_errstr(status)));
2736 return status;
2739 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2740 !security_token_has_privilege(get_current_nttok(conn),
2741 SEC_PRIV_SECURITY)) {
2742 DEBUG(10, ("open_directory: open on %s "
2743 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2744 smb_fname_str_dbg(smb_dname)));
2745 return NT_STATUS_PRIVILEGE_NOT_HELD;
2748 switch( create_disposition ) {
2749 case FILE_OPEN:
2751 if (!dir_existed) {
2752 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2755 info = FILE_WAS_OPENED;
2756 break;
2758 case FILE_CREATE:
2760 /* If directory exists error. If directory doesn't
2761 * exist create. */
2763 if (dir_existed) {
2764 status = 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;
2772 status = mkdir_internal(conn, smb_dname,
2773 file_attributes);
2775 if (!NT_STATUS_IS_OK(status)) {
2776 DEBUG(2, ("open_directory: unable to create "
2777 "%s. Error was %s\n",
2778 smb_fname_str_dbg(smb_dname),
2779 nt_errstr(status)));
2780 return status;
2783 info = FILE_WAS_CREATED;
2784 break;
2786 case FILE_OPEN_IF:
2788 * If directory exists open. If directory doesn't
2789 * exist create.
2792 if (dir_existed) {
2793 status = NT_STATUS_OK;
2794 info = FILE_WAS_OPENED;
2795 } else {
2796 status = mkdir_internal(conn, smb_dname,
2797 file_attributes);
2799 if (NT_STATUS_IS_OK(status)) {
2800 info = FILE_WAS_CREATED;
2801 } else {
2802 /* Cope with create race. */
2803 if (!NT_STATUS_EQUAL(status,
2804 NT_STATUS_OBJECT_NAME_COLLISION)) {
2805 DEBUG(2, ("open_directory: unable to create "
2806 "%s. Error was %s\n",
2807 smb_fname_str_dbg(smb_dname),
2808 nt_errstr(status)));
2809 return status;
2811 info = FILE_WAS_OPENED;
2815 break;
2817 case FILE_SUPERSEDE:
2818 case FILE_OVERWRITE:
2819 case FILE_OVERWRITE_IF:
2820 default:
2821 DEBUG(5,("open_directory: invalid create_disposition "
2822 "0x%x for directory %s\n",
2823 (unsigned int)create_disposition,
2824 smb_fname_str_dbg(smb_dname)));
2825 return NT_STATUS_INVALID_PARAMETER;
2828 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2829 DEBUG(5,("open_directory: %s is not a directory !\n",
2830 smb_fname_str_dbg(smb_dname)));
2831 return NT_STATUS_NOT_A_DIRECTORY;
2834 if (info == FILE_WAS_OPENED) {
2835 status = smbd_check_access_rights(conn, smb_dname, access_mask);
2836 if (!NT_STATUS_IS_OK(status)) {
2837 DEBUG(10, ("open_directory: smbd_check_access_rights on "
2838 "file %s failed with %s\n",
2839 smb_fname_str_dbg(smb_dname),
2840 nt_errstr(status)));
2841 return status;
2845 status = file_new(req, conn, &fsp);
2846 if(!NT_STATUS_IS_OK(status)) {
2847 return status;
2851 * Setup the files_struct for it.
2854 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2855 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2856 fsp->file_pid = req ? req->smbpid : 0;
2857 fsp->can_lock = False;
2858 fsp->can_read = False;
2859 fsp->can_write = False;
2861 fsp->share_access = share_access;
2862 fsp->fh->private_options = 0;
2864 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2866 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2867 fsp->print_file = NULL;
2868 fsp->modified = False;
2869 fsp->oplock_type = NO_OPLOCK;
2870 fsp->sent_oplock_break = NO_BREAK_SENT;
2871 fsp->is_directory = True;
2872 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2873 status = fsp_set_smb_fname(fsp, smb_dname);
2874 if (!NT_STATUS_IS_OK(status)) {
2875 file_free(req, fsp);
2876 return status;
2879 mtimespec = smb_dname->st.st_ex_mtime;
2881 #ifdef O_DIRECTORY
2882 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2883 #else
2884 /* POSIX allows us to open a directory with O_RDONLY. */
2885 status = fd_open(conn, fsp, O_RDONLY, 0);
2886 #endif
2887 if (!NT_STATUS_IS_OK(status)) {
2888 DEBUG(5, ("open_directory: Could not open fd for "
2889 "%s (%s)\n",
2890 smb_fname_str_dbg(smb_dname),
2891 nt_errstr(status)));
2892 file_free(req, fsp);
2893 return status;
2896 status = vfs_stat_fsp(fsp);
2897 if (!NT_STATUS_IS_OK(status)) {
2898 fd_close(fsp);
2899 file_free(req, fsp);
2900 return status;
2903 /* Ensure there was no race condition. */
2904 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2905 DEBUG(5,("open_directory: stat struct differs for "
2906 "directory %s.\n",
2907 smb_fname_str_dbg(smb_dname)));
2908 fd_close(fsp);
2909 file_free(req, fsp);
2910 return NT_STATUS_ACCESS_DENIED;
2913 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2914 conn->connectpath, smb_dname,
2915 &mtimespec);
2917 if (lck == NULL) {
2918 DEBUG(0, ("open_directory: Could not get share mode lock for "
2919 "%s\n", smb_fname_str_dbg(smb_dname)));
2920 fd_close(fsp);
2921 file_free(req, fsp);
2922 return NT_STATUS_SHARING_VIOLATION;
2925 status = open_mode_check(conn, lck, fsp->name_hash,
2926 access_mask, share_access,
2927 create_options, &dir_existed);
2929 if (!NT_STATUS_IS_OK(status)) {
2930 TALLOC_FREE(lck);
2931 fd_close(fsp);
2932 file_free(req, fsp);
2933 return status;
2936 set_share_mode(lck, fsp, get_current_uid(conn),
2937 req ? req->mid : 0, NO_OPLOCK);
2939 /* For directories the delete on close bit at open time seems
2940 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2941 if (create_options & FILE_DELETE_ON_CLOSE) {
2942 status = can_set_delete_on_close(fsp, 0);
2943 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2944 TALLOC_FREE(lck);
2945 fd_close(fsp);
2946 file_free(req, fsp);
2947 return status;
2950 if (NT_STATUS_IS_OK(status)) {
2951 /* Note that here we set the *inital* delete on close flag,
2952 not the regular one. The magic gets handled in close. */
2953 fsp->initial_delete_on_close = True;
2957 TALLOC_FREE(lck);
2959 if (pinfo) {
2960 *pinfo = info;
2963 *result = fsp;
2964 return NT_STATUS_OK;
2967 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2968 struct smb_filename *smb_dname)
2970 NTSTATUS status;
2971 files_struct *fsp;
2973 status = SMB_VFS_CREATE_FILE(
2974 conn, /* conn */
2975 req, /* req */
2976 0, /* root_dir_fid */
2977 smb_dname, /* fname */
2978 FILE_READ_ATTRIBUTES, /* access_mask */
2979 FILE_SHARE_NONE, /* share_access */
2980 FILE_CREATE, /* create_disposition*/
2981 FILE_DIRECTORY_FILE, /* create_options */
2982 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2983 0, /* oplock_request */
2984 0, /* allocation_size */
2985 0, /* private_flags */
2986 NULL, /* sd */
2987 NULL, /* ea_list */
2988 &fsp, /* result */
2989 NULL); /* pinfo */
2991 if (NT_STATUS_IS_OK(status)) {
2992 close_file(req, fsp, NORMAL_CLOSE);
2995 return status;
2998 /****************************************************************************
2999 Receive notification that one of our open files has been renamed by another
3000 smbd process.
3001 ****************************************************************************/
3003 void msg_file_was_renamed(struct messaging_context *msg,
3004 void *private_data,
3005 uint32_t msg_type,
3006 struct server_id server_id,
3007 DATA_BLOB *data)
3009 files_struct *fsp;
3010 char *frm = (char *)data->data;
3011 struct file_id id;
3012 const char *sharepath;
3013 const char *base_name;
3014 const char *stream_name;
3015 struct smb_filename *smb_fname = NULL;
3016 size_t sp_len, bn_len;
3017 NTSTATUS status;
3018 struct smbd_server_connection *sconn =
3019 talloc_get_type_abort(private_data,
3020 struct smbd_server_connection);
3022 if (data->data == NULL
3023 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3024 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3025 (int)data->length));
3026 return;
3029 /* Unpack the message. */
3030 pull_file_id_24(frm, &id);
3031 sharepath = &frm[24];
3032 sp_len = strlen(sharepath);
3033 base_name = sharepath + sp_len + 1;
3034 bn_len = strlen(base_name);
3035 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3037 /* stream_name must always be NULL if there is no stream. */
3038 if (stream_name[0] == '\0') {
3039 stream_name = NULL;
3042 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3043 stream_name, NULL, &smb_fname);
3044 if (!NT_STATUS_IS_OK(status)) {
3045 return;
3048 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3049 "file_id %s\n",
3050 sharepath, smb_fname_str_dbg(smb_fname),
3051 file_id_string_tos(&id)));
3053 for(fsp = file_find_di_first(sconn, id); fsp;
3054 fsp = file_find_di_next(fsp)) {
3055 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3057 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3058 fsp->fnum, fsp_str_dbg(fsp),
3059 smb_fname_str_dbg(smb_fname)));
3060 status = fsp_set_smb_fname(fsp, smb_fname);
3061 if (!NT_STATUS_IS_OK(status)) {
3062 goto out;
3064 } else {
3065 /* TODO. JRA. */
3066 /* Now we have the complete path we can work out if this is
3067 actually within this share and adjust newname accordingly. */
3068 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3069 "not sharepath %s) "
3070 "fnum %d from %s -> %s\n",
3071 fsp->conn->connectpath,
3072 sharepath,
3073 fsp->fnum,
3074 fsp_str_dbg(fsp),
3075 smb_fname_str_dbg(smb_fname)));
3078 out:
3079 TALLOC_FREE(smb_fname);
3080 return;
3084 * If a main file is opened for delete, all streams need to be checked for
3085 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3086 * If that works, delete them all by setting the delete on close and close.
3089 NTSTATUS open_streams_for_delete(connection_struct *conn,
3090 const char *fname)
3092 struct stream_struct *stream_info = NULL;
3093 files_struct **streams = NULL;
3094 int i;
3095 unsigned int num_streams = 0;
3096 TALLOC_CTX *frame = talloc_stackframe();
3097 NTSTATUS status;
3099 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3100 &num_streams, &stream_info);
3102 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3103 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3104 DEBUG(10, ("no streams around\n"));
3105 TALLOC_FREE(frame);
3106 return NT_STATUS_OK;
3109 if (!NT_STATUS_IS_OK(status)) {
3110 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3111 nt_errstr(status)));
3112 goto fail;
3115 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3116 num_streams));
3118 if (num_streams == 0) {
3119 TALLOC_FREE(frame);
3120 return NT_STATUS_OK;
3123 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3124 if (streams == NULL) {
3125 DEBUG(0, ("talloc failed\n"));
3126 status = NT_STATUS_NO_MEMORY;
3127 goto fail;
3130 for (i=0; i<num_streams; i++) {
3131 struct smb_filename *smb_fname = NULL;
3133 if (strequal(stream_info[i].name, "::$DATA")) {
3134 streams[i] = NULL;
3135 continue;
3138 status = create_synthetic_smb_fname(talloc_tos(), fname,
3139 stream_info[i].name,
3140 NULL, &smb_fname);
3141 if (!NT_STATUS_IS_OK(status)) {
3142 goto fail;
3145 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3146 DEBUG(10, ("Unable to stat stream: %s\n",
3147 smb_fname_str_dbg(smb_fname)));
3150 status = SMB_VFS_CREATE_FILE(
3151 conn, /* conn */
3152 NULL, /* req */
3153 0, /* root_dir_fid */
3154 smb_fname, /* fname */
3155 DELETE_ACCESS, /* access_mask */
3156 (FILE_SHARE_READ | /* share_access */
3157 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3158 FILE_OPEN, /* create_disposition*/
3159 0, /* create_options */
3160 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3161 0, /* oplock_request */
3162 0, /* allocation_size */
3163 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3164 NULL, /* sd */
3165 NULL, /* ea_list */
3166 &streams[i], /* result */
3167 NULL); /* pinfo */
3169 if (!NT_STATUS_IS_OK(status)) {
3170 DEBUG(10, ("Could not open stream %s: %s\n",
3171 smb_fname_str_dbg(smb_fname),
3172 nt_errstr(status)));
3174 TALLOC_FREE(smb_fname);
3175 break;
3177 TALLOC_FREE(smb_fname);
3181 * don't touch the variable "status" beyond this point :-)
3184 for (i -= 1 ; i >= 0; i--) {
3185 if (streams[i] == NULL) {
3186 continue;
3189 DEBUG(10, ("Closing stream # %d, %s\n", i,
3190 fsp_str_dbg(streams[i])));
3191 close_file(NULL, streams[i], NORMAL_CLOSE);
3194 fail:
3195 TALLOC_FREE(frame);
3196 return status;
3199 /*********************************************************************
3200 Create a default ACL by inheriting from the parent. If no inheritance
3201 from the parent available, don't set anything. This will leave the actual
3202 permissions the new file or directory already got from the filesystem
3203 as the NT ACL when read.
3204 *********************************************************************/
3206 static NTSTATUS inherit_new_acl(files_struct *fsp)
3208 TALLOC_CTX *ctx = talloc_tos();
3209 char *parent_name = NULL;
3210 struct security_descriptor *parent_desc = NULL;
3211 NTSTATUS status = NT_STATUS_OK;
3212 struct security_descriptor *psd = NULL;
3213 struct dom_sid *owner_sid = NULL;
3214 struct dom_sid *group_sid = NULL;
3215 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3216 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3217 bool inheritable_components = false;
3218 size_t size = 0;
3220 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3221 return NT_STATUS_NO_MEMORY;
3224 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3225 parent_name,
3226 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3227 &parent_desc);
3228 if (!NT_STATUS_IS_OK(status)) {
3229 return status;
3232 inheritable_components = sd_has_inheritable_components(parent_desc,
3233 fsp->is_directory);
3235 if (!inheritable_components && !inherit_owner) {
3236 /* Nothing to inherit and not setting owner. */
3237 return NT_STATUS_OK;
3240 /* Create an inherited descriptor from the parent. */
3242 if (DEBUGLEVEL >= 10) {
3243 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3244 fsp_str_dbg(fsp) ));
3245 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3248 /* Inherit from parent descriptor if "inherit owner" set. */
3249 if (inherit_owner) {
3250 owner_sid = parent_desc->owner_sid;
3251 group_sid = parent_desc->group_sid;
3254 if (owner_sid == NULL) {
3255 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3257 if (group_sid == NULL) {
3258 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3261 status = se_create_child_secdesc(ctx,
3262 &psd,
3263 &size,
3264 parent_desc,
3265 owner_sid,
3266 group_sid,
3267 fsp->is_directory);
3268 if (!NT_STATUS_IS_OK(status)) {
3269 return status;
3272 /* If inheritable_components == false,
3273 se_create_child_secdesc()
3274 creates a security desriptor with a NULL dacl
3275 entry, but with SEC_DESC_DACL_PRESENT. We need
3276 to remove that flag. */
3278 if (!inheritable_components) {
3279 security_info_sent &= ~SECINFO_DACL;
3280 psd->type &= ~SEC_DESC_DACL_PRESENT;
3283 if (DEBUGLEVEL >= 10) {
3284 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3285 fsp_str_dbg(fsp) ));
3286 NDR_PRINT_DEBUG(security_descriptor, psd);
3289 if (inherit_owner) {
3290 /* We need to be root to force this. */
3291 become_root();
3293 status = SMB_VFS_FSET_NT_ACL(fsp,
3294 security_info_sent,
3295 psd);
3296 if (inherit_owner) {
3297 unbecome_root();
3299 return status;
3303 * Wrapper around open_file_ntcreate and open_directory
3306 static NTSTATUS create_file_unixpath(connection_struct *conn,
3307 struct smb_request *req,
3308 struct smb_filename *smb_fname,
3309 uint32_t access_mask,
3310 uint32_t share_access,
3311 uint32_t create_disposition,
3312 uint32_t create_options,
3313 uint32_t file_attributes,
3314 uint32_t oplock_request,
3315 uint64_t allocation_size,
3316 uint32_t private_flags,
3317 struct security_descriptor *sd,
3318 struct ea_list *ea_list,
3320 files_struct **result,
3321 int *pinfo)
3323 int info = FILE_WAS_OPENED;
3324 files_struct *base_fsp = NULL;
3325 files_struct *fsp = NULL;
3326 NTSTATUS status;
3328 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3329 "file_attributes = 0x%x, share_access = 0x%x, "
3330 "create_disposition = 0x%x create_options = 0x%x "
3331 "oplock_request = 0x%x private_flags = 0x%x "
3332 "ea_list = 0x%p, sd = 0x%p, "
3333 "fname = %s\n",
3334 (unsigned int)access_mask,
3335 (unsigned int)file_attributes,
3336 (unsigned int)share_access,
3337 (unsigned int)create_disposition,
3338 (unsigned int)create_options,
3339 (unsigned int)oplock_request,
3340 (unsigned int)private_flags,
3341 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3343 if (create_options & FILE_OPEN_BY_FILE_ID) {
3344 status = NT_STATUS_NOT_SUPPORTED;
3345 goto fail;
3348 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3349 status = NT_STATUS_INVALID_PARAMETER;
3350 goto fail;
3353 if (req == NULL) {
3354 oplock_request |= INTERNAL_OPEN_ONLY;
3357 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3358 && (access_mask & DELETE_ACCESS)
3359 && !is_ntfs_stream_smb_fname(smb_fname)) {
3361 * We can't open a file with DELETE access if any of the
3362 * streams is open without FILE_SHARE_DELETE
3364 status = open_streams_for_delete(conn, smb_fname->base_name);
3366 if (!NT_STATUS_IS_OK(status)) {
3367 goto fail;
3371 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3372 !security_token_has_privilege(get_current_nttok(conn),
3373 SEC_PRIV_SECURITY)) {
3374 DEBUG(10, ("create_file_unixpath: open on %s "
3375 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3376 smb_fname_str_dbg(smb_fname)));
3377 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3378 goto fail;
3381 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3382 && is_ntfs_stream_smb_fname(smb_fname)
3383 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3384 uint32 base_create_disposition;
3385 struct smb_filename *smb_fname_base = NULL;
3387 if (create_options & FILE_DIRECTORY_FILE) {
3388 status = NT_STATUS_NOT_A_DIRECTORY;
3389 goto fail;
3392 switch (create_disposition) {
3393 case FILE_OPEN:
3394 base_create_disposition = FILE_OPEN;
3395 break;
3396 default:
3397 base_create_disposition = FILE_OPEN_IF;
3398 break;
3401 /* Create an smb_filename with stream_name == NULL. */
3402 status = create_synthetic_smb_fname(talloc_tos(),
3403 smb_fname->base_name,
3404 NULL, NULL,
3405 &smb_fname_base);
3406 if (!NT_STATUS_IS_OK(status)) {
3407 goto fail;
3410 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3411 DEBUG(10, ("Unable to stat stream: %s\n",
3412 smb_fname_str_dbg(smb_fname_base)));
3415 /* Open the base file. */
3416 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3417 FILE_SHARE_READ
3418 | FILE_SHARE_WRITE
3419 | FILE_SHARE_DELETE,
3420 base_create_disposition,
3421 0, 0, 0, 0, 0, NULL, NULL,
3422 &base_fsp, NULL);
3423 TALLOC_FREE(smb_fname_base);
3425 if (!NT_STATUS_IS_OK(status)) {
3426 DEBUG(10, ("create_file_unixpath for base %s failed: "
3427 "%s\n", smb_fname->base_name,
3428 nt_errstr(status)));
3429 goto fail;
3431 /* we don't need to low level fd */
3432 fd_close(base_fsp);
3436 * If it's a request for a directory open, deal with it separately.
3439 if (create_options & FILE_DIRECTORY_FILE) {
3441 if (create_options & FILE_NON_DIRECTORY_FILE) {
3442 status = NT_STATUS_INVALID_PARAMETER;
3443 goto fail;
3446 /* Can't open a temp directory. IFS kit test. */
3447 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3448 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3449 status = NT_STATUS_INVALID_PARAMETER;
3450 goto fail;
3454 * We will get a create directory here if the Win32
3455 * app specified a security descriptor in the
3456 * CreateDirectory() call.
3459 oplock_request = 0;
3460 status = open_directory(
3461 conn, req, smb_fname, access_mask, share_access,
3462 create_disposition, create_options, file_attributes,
3463 &info, &fsp);
3464 } else {
3467 * Ordinary file case.
3470 status = file_new(req, conn, &fsp);
3471 if(!NT_STATUS_IS_OK(status)) {
3472 goto fail;
3475 status = fsp_set_smb_fname(fsp, smb_fname);
3476 if (!NT_STATUS_IS_OK(status)) {
3477 goto fail;
3481 * We're opening the stream element of a base_fsp
3482 * we already opened. Set up the base_fsp pointer.
3484 if (base_fsp) {
3485 fsp->base_fsp = base_fsp;
3488 status = open_file_ntcreate(conn,
3489 req,
3490 access_mask,
3491 share_access,
3492 create_disposition,
3493 create_options,
3494 file_attributes,
3495 oplock_request,
3496 private_flags,
3497 &info,
3498 fsp);
3500 if(!NT_STATUS_IS_OK(status)) {
3501 file_free(req, fsp);
3502 fsp = NULL;
3505 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3507 /* A stream open never opens a directory */
3509 if (base_fsp) {
3510 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3511 goto fail;
3515 * Fail the open if it was explicitly a non-directory
3516 * file.
3519 if (create_options & FILE_NON_DIRECTORY_FILE) {
3520 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3521 goto fail;
3524 oplock_request = 0;
3525 status = open_directory(
3526 conn, req, smb_fname, access_mask,
3527 share_access, create_disposition,
3528 create_options, file_attributes,
3529 &info, &fsp);
3533 if (!NT_STATUS_IS_OK(status)) {
3534 goto fail;
3537 fsp->base_fsp = base_fsp;
3539 if ((ea_list != NULL) &&
3540 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3541 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3542 if (!NT_STATUS_IS_OK(status)) {
3543 goto fail;
3547 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3548 status = NT_STATUS_ACCESS_DENIED;
3549 goto fail;
3552 /* Save the requested allocation size. */
3553 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3554 if (allocation_size
3555 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3556 fsp->initial_allocation_size = smb_roundup(
3557 fsp->conn, allocation_size);
3558 if (fsp->is_directory) {
3559 /* Can't set allocation size on a directory. */
3560 status = NT_STATUS_ACCESS_DENIED;
3561 goto fail;
3563 if (vfs_allocate_file_space(
3564 fsp, fsp->initial_allocation_size) == -1) {
3565 status = NT_STATUS_DISK_FULL;
3566 goto fail;
3568 } else {
3569 fsp->initial_allocation_size = smb_roundup(
3570 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3574 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3575 fsp->base_fsp == NULL) {
3576 if (sd != NULL) {
3578 * According to the MS documentation, the only time the security
3579 * descriptor is applied to the opened file is iff we *created* the
3580 * file; an existing file stays the same.
3582 * Also, it seems (from observation) that you can open the file with
3583 * any access mask but you can still write the sd. We need to override
3584 * the granted access before we call set_sd
3585 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3588 uint32_t sec_info_sent;
3589 uint32_t saved_access_mask = fsp->access_mask;
3591 sec_info_sent = get_sec_info(sd);
3593 fsp->access_mask = FILE_GENERIC_ALL;
3595 /* Convert all the generic bits. */
3596 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3597 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3599 if (sec_info_sent & (SECINFO_OWNER|
3600 SECINFO_GROUP|
3601 SECINFO_DACL|
3602 SECINFO_SACL)) {
3603 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3606 fsp->access_mask = saved_access_mask;
3608 if (!NT_STATUS_IS_OK(status)) {
3609 goto fail;
3611 } else if (lp_inherit_acls(SNUM(conn))) {
3612 /* Inherit from parent. Errors here are not fatal. */
3613 status = inherit_new_acl(fsp);
3614 if (!NT_STATUS_IS_OK(status)) {
3615 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3616 fsp_str_dbg(fsp),
3617 nt_errstr(status) ));
3622 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3624 *result = fsp;
3625 if (pinfo != NULL) {
3626 *pinfo = info;
3629 smb_fname->st = fsp->fsp_name->st;
3631 return NT_STATUS_OK;
3633 fail:
3634 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3636 if (fsp != NULL) {
3637 if (base_fsp && fsp->base_fsp == base_fsp) {
3639 * The close_file below will close
3640 * fsp->base_fsp.
3642 base_fsp = NULL;
3644 close_file(req, fsp, ERROR_CLOSE);
3645 fsp = NULL;
3647 if (base_fsp != NULL) {
3648 close_file(req, base_fsp, ERROR_CLOSE);
3649 base_fsp = NULL;
3651 return status;
3655 * Calculate the full path name given a relative fid.
3657 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3658 struct smb_request *req,
3659 uint16_t root_dir_fid,
3660 const struct smb_filename *smb_fname,
3661 struct smb_filename **smb_fname_out)
3663 files_struct *dir_fsp;
3664 char *parent_fname = NULL;
3665 char *new_base_name = NULL;
3666 NTSTATUS status;
3668 if (root_dir_fid == 0 || !smb_fname) {
3669 status = NT_STATUS_INTERNAL_ERROR;
3670 goto out;
3673 dir_fsp = file_fsp(req, root_dir_fid);
3675 if (dir_fsp == NULL) {
3676 status = NT_STATUS_INVALID_HANDLE;
3677 goto out;
3680 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3681 status = NT_STATUS_INVALID_HANDLE;
3682 goto out;
3685 if (!dir_fsp->is_directory) {
3688 * Check to see if this is a mac fork of some kind.
3691 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3692 is_ntfs_stream_smb_fname(smb_fname)) {
3693 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3694 goto out;
3698 we need to handle the case when we get a
3699 relative open relative to a file and the
3700 pathname is blank - this is a reopen!
3701 (hint from demyn plantenberg)
3704 status = NT_STATUS_INVALID_HANDLE;
3705 goto out;
3708 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3710 * We're at the toplevel dir, the final file name
3711 * must not contain ./, as this is filtered out
3712 * normally by srvstr_get_path and unix_convert
3713 * explicitly rejects paths containing ./.
3715 parent_fname = talloc_strdup(talloc_tos(), "");
3716 if (parent_fname == NULL) {
3717 status = NT_STATUS_NO_MEMORY;
3718 goto out;
3720 } else {
3721 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3724 * Copy in the base directory name.
3727 parent_fname = talloc_array(talloc_tos(), char,
3728 dir_name_len+2);
3729 if (parent_fname == NULL) {
3730 status = NT_STATUS_NO_MEMORY;
3731 goto out;
3733 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3734 dir_name_len+1);
3737 * Ensure it ends in a '/'.
3738 * We used TALLOC_SIZE +2 to add space for the '/'.
3741 if(dir_name_len
3742 && (parent_fname[dir_name_len-1] != '\\')
3743 && (parent_fname[dir_name_len-1] != '/')) {
3744 parent_fname[dir_name_len] = '/';
3745 parent_fname[dir_name_len+1] = '\0';
3749 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3750 smb_fname->base_name);
3751 if (new_base_name == NULL) {
3752 status = NT_STATUS_NO_MEMORY;
3753 goto out;
3756 status = filename_convert(req,
3757 conn,
3758 req->flags2 & FLAGS2_DFS_PATHNAMES,
3759 new_base_name,
3761 NULL,
3762 smb_fname_out);
3763 if (!NT_STATUS_IS_OK(status)) {
3764 goto out;
3767 out:
3768 TALLOC_FREE(parent_fname);
3769 TALLOC_FREE(new_base_name);
3770 return status;
3773 NTSTATUS create_file_default(connection_struct *conn,
3774 struct smb_request *req,
3775 uint16_t root_dir_fid,
3776 struct smb_filename *smb_fname,
3777 uint32_t access_mask,
3778 uint32_t share_access,
3779 uint32_t create_disposition,
3780 uint32_t create_options,
3781 uint32_t file_attributes,
3782 uint32_t oplock_request,
3783 uint64_t allocation_size,
3784 uint32_t private_flags,
3785 struct security_descriptor *sd,
3786 struct ea_list *ea_list,
3787 files_struct **result,
3788 int *pinfo)
3790 int info = FILE_WAS_OPENED;
3791 files_struct *fsp = NULL;
3792 NTSTATUS status;
3793 bool stream_name = false;
3795 DEBUG(10,("create_file: access_mask = 0x%x "
3796 "file_attributes = 0x%x, share_access = 0x%x, "
3797 "create_disposition = 0x%x create_options = 0x%x "
3798 "oplock_request = 0x%x "
3799 "private_flags = 0x%x "
3800 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3801 "fname = %s\n",
3802 (unsigned int)access_mask,
3803 (unsigned int)file_attributes,
3804 (unsigned int)share_access,
3805 (unsigned int)create_disposition,
3806 (unsigned int)create_options,
3807 (unsigned int)oplock_request,
3808 (unsigned int)private_flags,
3809 (unsigned int)root_dir_fid,
3810 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3813 * Calculate the filename from the root_dir_if if necessary.
3816 if (root_dir_fid != 0) {
3817 struct smb_filename *smb_fname_out = NULL;
3818 status = get_relative_fid_filename(conn, req, root_dir_fid,
3819 smb_fname, &smb_fname_out);
3820 if (!NT_STATUS_IS_OK(status)) {
3821 goto fail;
3823 smb_fname = smb_fname_out;
3827 * Check to see if this is a mac fork of some kind.
3830 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3831 if (stream_name) {
3832 enum FAKE_FILE_TYPE fake_file_type;
3834 fake_file_type = is_fake_file(smb_fname);
3836 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3839 * Here we go! support for changing the disk quotas
3840 * --metze
3842 * We need to fake up to open this MAGIC QUOTA file
3843 * and return a valid FID.
3845 * w2k close this file directly after openening xp
3846 * also tries a QUERY_FILE_INFO on the file and then
3847 * close it
3849 status = open_fake_file(req, conn, req->vuid,
3850 fake_file_type, smb_fname,
3851 access_mask, &fsp);
3852 if (!NT_STATUS_IS_OK(status)) {
3853 goto fail;
3856 ZERO_STRUCT(smb_fname->st);
3857 goto done;
3860 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3861 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3862 goto fail;
3866 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
3867 int ret;
3868 smb_fname->stream_name = NULL;
3869 /* We have to handle this error here. */
3870 if (create_options & FILE_DIRECTORY_FILE) {
3871 status = NT_STATUS_NOT_A_DIRECTORY;
3872 goto fail;
3874 if (lp_posix_pathnames()) {
3875 ret = SMB_VFS_LSTAT(conn, smb_fname);
3876 } else {
3877 ret = SMB_VFS_STAT(conn, smb_fname);
3880 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3881 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3882 goto fail;
3886 status = create_file_unixpath(
3887 conn, req, smb_fname, access_mask, share_access,
3888 create_disposition, create_options, file_attributes,
3889 oplock_request, allocation_size, private_flags,
3890 sd, ea_list,
3891 &fsp, &info);
3893 if (!NT_STATUS_IS_OK(status)) {
3894 goto fail;
3897 done:
3898 DEBUG(10, ("create_file: info=%d\n", info));
3900 *result = fsp;
3901 if (pinfo != NULL) {
3902 *pinfo = info;
3904 return NT_STATUS_OK;
3906 fail:
3907 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3909 if (fsp != NULL) {
3910 close_file(req, fsp, ERROR_CLOSE);
3911 fsp = NULL;
3913 return status;