s3: Factor out disposition_to_open_flags
[Samba/gebeck_regimport.git] / source3 / smbd / open.c
blob27e0128582b7d9075bb1bd92df90ccbca0c796e4
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 bool async_open;
39 struct file_id id;
42 /****************************************************************************
43 If the requester wanted DELETE_ACCESS and was rejected because
44 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
45 overrides this.
46 ****************************************************************************/
48 static bool parent_override_delete(connection_struct *conn,
49 const struct smb_filename *smb_fname,
50 uint32_t access_mask,
51 uint32_t rejected_mask)
53 if ((access_mask & DELETE_ACCESS) &&
54 (rejected_mask & DELETE_ACCESS) &&
55 can_delete_file_in_directory(conn, smb_fname)) {
56 return true;
58 return false;
61 /****************************************************************************
62 Check if we have open rights.
63 ****************************************************************************/
65 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
66 const struct smb_filename *smb_fname,
67 uint32_t access_mask)
69 /* Check if we have rights to open. */
70 NTSTATUS status;
71 struct security_descriptor *sd = NULL;
72 uint32_t rejected_share_access;
73 uint32_t rejected_mask = access_mask;
75 rejected_share_access = access_mask & ~(conn->share_access);
77 if (rejected_share_access) {
78 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
79 "on %s (0x%x)\n",
80 (unsigned int)access_mask,
81 smb_fname_str_dbg(smb_fname),
82 (unsigned int)rejected_share_access ));
83 return NT_STATUS_ACCESS_DENIED;
86 if (get_current_uid(conn) == (uid_t)0) {
87 /* I'm sorry sir, I didn't know you were root... */
88 DEBUG(10,("smbd_check_access_rights: root override "
89 "on %s. Granting 0x%x\n",
90 smb_fname_str_dbg(smb_fname),
91 (unsigned int)access_mask ));
92 return NT_STATUS_OK;
95 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
96 DEBUG(10,("smbd_check_access_rights: not checking ACL "
97 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
98 smb_fname_str_dbg(smb_fname),
99 (unsigned int)access_mask ));
100 return NT_STATUS_OK;
103 if (access_mask == DELETE_ACCESS &&
104 VALID_STAT(smb_fname->st) &&
105 S_ISLNK(smb_fname->st.st_ex_mode)) {
106 /* We can always delete a symlink. */
107 DEBUG(10,("smbd_check_access_rights: not checking ACL "
108 "on DELETE_ACCESS on symlink %s.\n",
109 smb_fname_str_dbg(smb_fname) ));
110 return NT_STATUS_OK;
113 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
114 (SECINFO_OWNER |
115 SECINFO_GROUP |
116 SECINFO_DACL),&sd);
118 if (!NT_STATUS_IS_OK(status)) {
119 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
120 "on %s: %s\n",
121 smb_fname_str_dbg(smb_fname),
122 nt_errstr(status)));
124 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
125 goto access_denied;
128 return status;
132 * Never test FILE_READ_ATTRIBUTES. se_file_access_check() also takes care of
133 * owner WRITE_DAC and READ_CONTROL.
135 status = se_file_access_check(sd,
136 get_current_nttok(conn),
137 false,
138 (access_mask & ~FILE_READ_ATTRIBUTES),
139 &rejected_mask);
141 DEBUG(10,("smbd_check_access_rights: file %s requesting "
142 "0x%x returning 0x%x (%s)\n",
143 smb_fname_str_dbg(smb_fname),
144 (unsigned int)access_mask,
145 (unsigned int)rejected_mask,
146 nt_errstr(status) ));
148 if (!NT_STATUS_IS_OK(status)) {
149 if (DEBUGLEVEL >= 10) {
150 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
151 smb_fname_str_dbg(smb_fname) ));
152 NDR_PRINT_DEBUG(security_descriptor, sd);
156 TALLOC_FREE(sd);
158 if (NT_STATUS_IS_OK(status) ||
159 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
160 return status;
163 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
165 access_denied:
167 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
168 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
169 !lp_store_dos_attributes(SNUM(conn)) &&
170 (lp_map_readonly(SNUM(conn)) ||
171 lp_map_archive(SNUM(conn)) ||
172 lp_map_hidden(SNUM(conn)) ||
173 lp_map_system(SNUM(conn)))) {
174 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
176 DEBUG(10,("smbd_check_access_rights: "
177 "overrode "
178 "FILE_WRITE_ATTRIBUTES "
179 "on file %s\n",
180 smb_fname_str_dbg(smb_fname)));
183 if (parent_override_delete(conn,
184 smb_fname,
185 access_mask,
186 rejected_mask)) {
187 /* Were we trying to do an open
188 * for delete and didn't get DELETE
189 * access (only) ? Check if the
190 * directory allows DELETE_CHILD.
191 * See here:
192 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
193 * for details. */
195 rejected_mask &= ~DELETE_ACCESS;
197 DEBUG(10,("smbd_check_access_rights: "
198 "overrode "
199 "DELETE_ACCESS on "
200 "file %s\n",
201 smb_fname_str_dbg(smb_fname)));
204 if (rejected_mask != 0) {
205 return NT_STATUS_ACCESS_DENIED;
207 return NT_STATUS_OK;
210 static NTSTATUS check_parent_access(struct connection_struct *conn,
211 struct smb_filename *smb_fname,
212 uint32_t access_mask)
214 NTSTATUS status;
215 char *parent_dir = NULL;
216 struct security_descriptor *parent_sd = NULL;
217 uint32_t access_granted = 0;
219 if (!parent_dirname(talloc_tos(),
220 smb_fname->base_name,
221 &parent_dir,
222 NULL)) {
223 return NT_STATUS_NO_MEMORY;
226 if (get_current_uid(conn) == (uid_t)0) {
227 /* I'm sorry sir, I didn't know you were root... */
228 DEBUG(10,("check_parent_access: root override "
229 "on %s. Granting 0x%x\n",
230 smb_fname_str_dbg(smb_fname),
231 (unsigned int)access_mask ));
232 return NT_STATUS_OK;
235 status = SMB_VFS_GET_NT_ACL(conn,
236 parent_dir,
237 SECINFO_DACL,
238 &parent_sd);
240 if (!NT_STATUS_IS_OK(status)) {
241 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
242 "%s with error %s\n",
243 parent_dir,
244 nt_errstr(status)));
245 return status;
249 * Never test FILE_READ_ATTRIBUTES. se_file_access_check() also takes care of
250 * owner WRITE_DAC and READ_CONTROL.
252 status = se_file_access_check(parent_sd,
253 get_current_nttok(conn),
254 false,
255 (access_mask & ~FILE_READ_ATTRIBUTES),
256 &access_granted);
257 if(!NT_STATUS_IS_OK(status)) {
258 DEBUG(5,("check_parent_access: access check "
259 "on directory %s for "
260 "path %s for mask 0x%x returned (0x%x) %s\n",
261 parent_dir,
262 smb_fname->base_name,
263 access_mask,
264 access_granted,
265 nt_errstr(status) ));
266 return status;
269 return NT_STATUS_OK;
272 /****************************************************************************
273 fd support routines - attempt to do a dos_open.
274 ****************************************************************************/
276 static NTSTATUS fd_open(struct connection_struct *conn,
277 files_struct *fsp,
278 int flags,
279 mode_t mode)
281 struct smb_filename *smb_fname = fsp->fsp_name;
282 NTSTATUS status = NT_STATUS_OK;
284 #ifdef O_NOFOLLOW
286 * Never follow symlinks on a POSIX client. The
287 * client should be doing this.
290 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
291 flags |= O_NOFOLLOW;
293 #endif
295 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
296 if (fsp->fh->fd == -1) {
297 int posix_errno = errno;
298 #ifdef O_NOFOLLOW
299 #if defined(ENOTSUP) && defined(OSF1)
300 /* handle special Tru64 errno */
301 if (errno == ENOTSUP) {
302 posix_errno = ELOOP;
304 #endif /* ENOTSUP */
305 #ifdef EFTYPE
306 /* fix broken NetBSD errno */
307 if (errno == EFTYPE) {
308 posix_errno = ELOOP;
310 #endif /* EFTYPE */
311 /* fix broken FreeBSD errno */
312 if (errno == EMLINK) {
313 posix_errno = ELOOP;
315 #endif /* O_NOFOLLOW */
316 status = map_nt_error_from_unix(posix_errno);
317 if (errno == EMFILE) {
318 static time_t last_warned = 0L;
320 if (time((time_t *) NULL) > last_warned) {
321 DEBUG(0,("Too many open files, unable "
322 "to open more! smbd's max "
323 "open files = %d\n",
324 lp_max_open_files()));
325 last_warned = time((time_t *) NULL);
331 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
332 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
333 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
335 return status;
338 /****************************************************************************
339 Close the file associated with a fsp.
340 ****************************************************************************/
342 NTSTATUS fd_close(files_struct *fsp)
344 int ret;
346 if (fsp->dptr) {
347 dptr_CloseDir(fsp);
349 if (fsp->fh->fd == -1) {
350 return NT_STATUS_OK; /* What we used to call a stat open. */
352 if (fsp->fh->ref_count > 1) {
353 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
356 ret = SMB_VFS_CLOSE(fsp);
357 fsp->fh->fd = -1;
358 if (ret == -1) {
359 return map_nt_error_from_unix(errno);
361 return NT_STATUS_OK;
364 /****************************************************************************
365 Change the ownership of a file to that of the parent directory.
366 Do this by fd if possible.
367 ****************************************************************************/
369 void change_file_owner_to_parent(connection_struct *conn,
370 const char *inherit_from_dir,
371 files_struct *fsp)
373 struct smb_filename *smb_fname_parent = NULL;
374 NTSTATUS status;
375 int ret;
377 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
378 NULL, NULL, &smb_fname_parent);
379 if (!NT_STATUS_IS_OK(status)) {
380 return;
383 ret = SMB_VFS_STAT(conn, smb_fname_parent);
384 if (ret == -1) {
385 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
386 "directory %s. Error was %s\n",
387 smb_fname_str_dbg(smb_fname_parent),
388 strerror(errno)));
389 TALLOC_FREE(smb_fname_parent);
390 return;
393 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
394 /* Already this uid - no need to change. */
395 DEBUG(10,("change_file_owner_to_parent: file %s "
396 "is already owned by uid %d\n",
397 fsp_str_dbg(fsp),
398 (int)fsp->fsp_name->st.st_ex_uid ));
399 TALLOC_FREE(smb_fname_parent);
400 return;
403 become_root();
404 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
405 unbecome_root();
406 if (ret == -1) {
407 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
408 "file %s to parent directory uid %u. Error "
409 "was %s\n", fsp_str_dbg(fsp),
410 (unsigned int)smb_fname_parent->st.st_ex_uid,
411 strerror(errno) ));
412 } else {
413 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
414 "parent directory uid %u.\n", fsp_str_dbg(fsp),
415 (unsigned int)smb_fname_parent->st.st_ex_uid));
416 /* Ensure the uid entry is updated. */
417 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
420 TALLOC_FREE(smb_fname_parent);
423 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
424 const char *inherit_from_dir,
425 const char *fname,
426 SMB_STRUCT_STAT *psbuf)
428 struct smb_filename *smb_fname_parent = NULL;
429 struct smb_filename *smb_fname_cwd = NULL;
430 char *saved_dir = NULL;
431 TALLOC_CTX *ctx = talloc_tos();
432 NTSTATUS status = NT_STATUS_OK;
433 int ret;
435 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
436 &smb_fname_parent);
437 if (!NT_STATUS_IS_OK(status)) {
438 return status;
441 ret = SMB_VFS_STAT(conn, smb_fname_parent);
442 if (ret == -1) {
443 status = map_nt_error_from_unix(errno);
444 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
445 "directory %s. Error was %s\n",
446 smb_fname_str_dbg(smb_fname_parent),
447 strerror(errno)));
448 goto out;
451 /* We've already done an lstat into psbuf, and we know it's a
452 directory. If we can cd into the directory and the dev/ino
453 are the same then we can safely chown without races as
454 we're locking the directory in place by being in it. This
455 should work on any UNIX (thanks tridge :-). JRA.
458 saved_dir = vfs_GetWd(ctx,conn);
459 if (!saved_dir) {
460 status = map_nt_error_from_unix(errno);
461 DEBUG(0,("change_dir_owner_to_parent: failed to get "
462 "current working directory. Error was %s\n",
463 strerror(errno)));
464 goto out;
467 /* Chdir into the new path. */
468 if (vfs_ChDir(conn, fname) == -1) {
469 status = map_nt_error_from_unix(errno);
470 DEBUG(0,("change_dir_owner_to_parent: failed to change "
471 "current working directory to %s. Error "
472 "was %s\n", fname, strerror(errno) ));
473 goto chdir;
476 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
477 &smb_fname_cwd);
478 if (!NT_STATUS_IS_OK(status)) {
479 return status;
482 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
483 if (ret == -1) {
484 status = map_nt_error_from_unix(errno);
485 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
486 "directory '.' (%s) Error was %s\n",
487 fname, strerror(errno)));
488 goto chdir;
491 /* Ensure we're pointing at the same place. */
492 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
493 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
494 DEBUG(0,("change_dir_owner_to_parent: "
495 "device/inode on directory %s changed. "
496 "Refusing to chown !\n", fname ));
497 status = NT_STATUS_ACCESS_DENIED;
498 goto chdir;
501 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
502 /* Already this uid - no need to change. */
503 DEBUG(10,("change_dir_owner_to_parent: directory %s "
504 "is already owned by uid %d\n",
505 fname,
506 (int)smb_fname_cwd->st.st_ex_uid ));
507 status = NT_STATUS_OK;
508 goto chdir;
511 become_root();
512 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
513 (gid_t)-1);
514 unbecome_root();
515 if (ret == -1) {
516 status = map_nt_error_from_unix(errno);
517 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
518 "directory %s to parent directory uid %u. "
519 "Error was %s\n", fname,
520 (unsigned int)smb_fname_parent->st.st_ex_uid,
521 strerror(errno) ));
522 } else {
523 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
524 "directory %s to parent directory uid %u.\n",
525 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
526 /* Ensure the uid entry is updated. */
527 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
530 chdir:
531 vfs_ChDir(conn,saved_dir);
532 out:
533 TALLOC_FREE(smb_fname_parent);
534 TALLOC_FREE(smb_fname_cwd);
535 return status;
538 /****************************************************************************
539 Open a file - returning a guaranteed ATOMIC indication of if the
540 file was created or not.
541 ****************************************************************************/
543 static NTSTATUS fd_open_atomic(struct connection_struct *conn,
544 files_struct *fsp,
545 int flags,
546 mode_t mode,
547 bool *file_created)
549 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
550 bool file_existed = VALID_STAT(fsp->fsp_name->st);
552 *file_created = false;
554 if (!(flags & O_CREAT)) {
556 * We're not creating the file, just pass through.
558 return fd_open(conn, fsp, flags, mode);
561 if (flags & O_EXCL) {
563 * Fail if already exists, just pass through.
565 status = fd_open(conn, fsp, flags, mode);
566 if (NT_STATUS_IS_OK(status)) {
568 * Here we've opened with O_CREAT|O_EXCL
569 * and got success. We *know* we created
570 * this file.
572 *file_created = true;
574 return status;
578 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
579 * To know absolutely if we created the file or not,
580 * we can never call O_CREAT without O_EXCL. So if
581 * we think the file existed, try without O_CREAT|O_EXCL.
582 * If we think the file didn't exist, try with
583 * O_CREAT|O_EXCL. Keep bouncing between these two
584 * requests until either the file is created, or
585 * opened. Either way, we keep going until we get
586 * a returnable result (error, or open/create).
589 while(1) {
590 int curr_flags = flags;
592 if (file_existed) {
593 /* Just try open, do not create. */
594 curr_flags &= ~(O_CREAT);
595 status = fd_open(conn, fsp, curr_flags, mode);
596 if (NT_STATUS_EQUAL(status,
597 NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
599 * Someone deleted it in the meantime.
600 * Retry with O_EXCL.
602 file_existed = false;
603 DEBUG(10,("fd_open_atomic: file %s existed. "
604 "Retry.\n",
605 smb_fname_str_dbg(fsp->fsp_name)));
606 continue;
608 } else {
609 /* Try create exclusively, fail if it exists. */
610 curr_flags |= O_EXCL;
611 status = fd_open(conn, fsp, curr_flags, mode);
612 if (NT_STATUS_EQUAL(status,
613 NT_STATUS_OBJECT_NAME_COLLISION)) {
615 * Someone created it in the meantime.
616 * Retry without O_CREAT.
618 file_existed = true;
619 DEBUG(10,("fd_open_atomic: file %s "
620 "did not exist. Retry.\n",
621 smb_fname_str_dbg(fsp->fsp_name)));
622 continue;
624 if (NT_STATUS_IS_OK(status)) {
626 * Here we've opened with O_CREAT|O_EXCL
627 * and got success. We *know* we created
628 * this file.
630 *file_created = true;
633 /* Create is done, or failed. */
634 break;
636 return status;
639 /****************************************************************************
640 Open a file.
641 ****************************************************************************/
643 static NTSTATUS open_file(files_struct *fsp,
644 connection_struct *conn,
645 struct smb_request *req,
646 const char *parent_dir,
647 int flags,
648 mode_t unx_mode,
649 uint32 access_mask, /* client requested access mask. */
650 uint32 open_access_mask, /* what we're actually using in the open. */
651 bool *p_file_created)
653 struct smb_filename *smb_fname = fsp->fsp_name;
654 NTSTATUS status = NT_STATUS_OK;
655 int accmode = (flags & O_ACCMODE);
656 int local_flags = flags;
657 bool file_existed = VALID_STAT(fsp->fsp_name->st);
659 fsp->fh->fd = -1;
660 errno = EPERM;
662 /* Check permissions */
665 * This code was changed after seeing a client open request
666 * containing the open mode of (DENY_WRITE/read-only) with
667 * the 'create if not exist' bit set. The previous code
668 * would fail to open the file read only on a read-only share
669 * as it was checking the flags parameter directly against O_RDONLY,
670 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
671 * JRA.
674 if (!CAN_WRITE(conn)) {
675 /* It's a read-only share - fail if we wanted to write. */
676 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
677 DEBUG(3,("Permission denied opening %s\n",
678 smb_fname_str_dbg(smb_fname)));
679 return NT_STATUS_ACCESS_DENIED;
680 } else if(flags & O_CREAT) {
681 /* We don't want to write - but we must make sure that
682 O_CREAT doesn't create the file if we have write
683 access into the directory.
685 flags &= ~(O_CREAT|O_EXCL);
686 local_flags &= ~(O_CREAT|O_EXCL);
691 * This little piece of insanity is inspired by the
692 * fact that an NT client can open a file for O_RDONLY,
693 * but set the create disposition to FILE_EXISTS_TRUNCATE.
694 * If the client *can* write to the file, then it expects to
695 * truncate the file, even though it is opening for readonly.
696 * Quicken uses this stupid trick in backup file creation...
697 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
698 * for helping track this one down. It didn't bite us in 2.0.x
699 * as we always opened files read-write in that release. JRA.
702 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
703 DEBUG(10,("open_file: truncate requested on read-only open "
704 "for file %s\n", smb_fname_str_dbg(smb_fname)));
705 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
708 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
709 (!file_existed && (local_flags & O_CREAT)) ||
710 ((local_flags & O_TRUNC) == O_TRUNC) ) {
711 const char *wild;
712 int ret;
715 * We can't actually truncate here as the file may be locked.
716 * open_file_ntcreate will take care of the truncate later. JRA.
719 local_flags &= ~O_TRUNC;
721 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
723 * We would block on opening a FIFO with no one else on the
724 * other end. Do what we used to do and add O_NONBLOCK to the
725 * open flags. JRA.
728 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
729 local_flags |= O_NONBLOCK;
731 #endif
733 /* Don't create files with Microsoft wildcard characters. */
734 if (fsp->base_fsp) {
736 * wildcard characters are allowed in stream names
737 * only test the basefilename
739 wild = fsp->base_fsp->fsp_name->base_name;
740 } else {
741 wild = smb_fname->base_name;
743 if ((local_flags & O_CREAT) && !file_existed &&
744 ms_has_wild(wild)) {
745 return NT_STATUS_OBJECT_NAME_INVALID;
748 /* Can we access this file ? */
749 if (!fsp->base_fsp) {
750 /* Only do this check on non-stream open. */
751 if (file_existed) {
752 status = smbd_check_access_rights(conn,
753 smb_fname,
754 access_mask);
755 } else if (local_flags & O_CREAT){
756 status = check_parent_access(conn,
757 smb_fname,
758 SEC_DIR_ADD_FILE);
759 } else {
760 /* File didn't exist and no O_CREAT. */
761 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
763 if (!NT_STATUS_IS_OK(status)) {
764 DEBUG(10,("open_file: "
765 "%s on file "
766 "%s returned %s\n",
767 file_existed ?
768 "smbd_check_access_rights" :
769 "check_parent_access",
770 smb_fname_str_dbg(smb_fname),
771 nt_errstr(status) ));
772 return status;
776 /* Actually do the open */
777 status = fd_open_atomic(conn, fsp, local_flags,
778 unx_mode, p_file_created);
779 if (!NT_STATUS_IS_OK(status)) {
780 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
781 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
782 nt_errstr(status),local_flags,flags));
783 return status;
786 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
787 if (ret == -1) {
788 /* If we have an fd, this stat should succeed. */
789 DEBUG(0,("Error doing fstat on open file %s "
790 "(%s)\n",
791 smb_fname_str_dbg(smb_fname),
792 strerror(errno) ));
793 status = map_nt_error_from_unix(errno);
794 fd_close(fsp);
795 return status;
798 if (*p_file_created) {
799 /* We created this file. */
801 bool need_re_stat = false;
802 /* Do all inheritance work after we've
803 done a successful fstat call and filled
804 in the stat struct in fsp->fsp_name. */
806 /* Inherit the ACL if required */
807 if (lp_inherit_perms(SNUM(conn))) {
808 inherit_access_posix_acl(conn, parent_dir,
809 smb_fname->base_name,
810 unx_mode);
811 need_re_stat = true;
814 /* Change the owner if required. */
815 if (lp_inherit_owner(SNUM(conn))) {
816 change_file_owner_to_parent(conn, parent_dir,
817 fsp);
818 need_re_stat = true;
821 if (need_re_stat) {
822 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
823 /* If we have an fd, this stat should succeed. */
824 if (ret == -1) {
825 DEBUG(0,("Error doing fstat on open file %s "
826 "(%s)\n",
827 smb_fname_str_dbg(smb_fname),
828 strerror(errno) ));
832 notify_fname(conn, NOTIFY_ACTION_ADDED,
833 FILE_NOTIFY_CHANGE_FILE_NAME,
834 smb_fname->base_name);
836 } else {
837 fsp->fh->fd = -1; /* What we used to call a stat open. */
838 if (!file_existed) {
839 /* File must exist for a stat open. */
840 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
843 status = smbd_check_access_rights(conn,
844 smb_fname,
845 access_mask);
847 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
848 fsp->posix_open &&
849 S_ISLNK(smb_fname->st.st_ex_mode)) {
850 /* This is a POSIX stat open for delete
851 * or rename on a symlink that points
852 * nowhere. Allow. */
853 DEBUG(10,("open_file: allowing POSIX "
854 "open on bad symlink %s\n",
855 smb_fname_str_dbg(smb_fname)));
856 status = NT_STATUS_OK;
859 if (!NT_STATUS_IS_OK(status)) {
860 DEBUG(10,("open_file: "
861 "smbd_check_access_rights on file "
862 "%s returned %s\n",
863 smb_fname_str_dbg(smb_fname),
864 nt_errstr(status) ));
865 return status;
870 * POSIX allows read-only opens of directories. We don't
871 * want to do this (we use a different code path for this)
872 * so catch a directory open and return an EISDIR. JRA.
875 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
876 fd_close(fsp);
877 errno = EISDIR;
878 return NT_STATUS_FILE_IS_A_DIRECTORY;
881 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
882 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
883 fsp->file_pid = req ? req->smbpid : 0;
884 fsp->can_lock = True;
885 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
886 fsp->can_write =
887 CAN_WRITE(conn) &&
888 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
889 fsp->print_file = NULL;
890 fsp->modified = False;
891 fsp->sent_oplock_break = NO_BREAK_SENT;
892 fsp->is_directory = False;
893 if (conn->aio_write_behind_list &&
894 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
895 conn->case_sensitive)) {
896 fsp->aio_write_behind = True;
899 fsp->wcp = NULL; /* Write cache pointer. */
901 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
902 conn->session_info->unix_info->unix_name,
903 smb_fname_str_dbg(smb_fname),
904 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
905 conn->num_files_open));
907 errno = 0;
908 return NT_STATUS_OK;
911 /****************************************************************************
912 Check if we can open a file with a share mode.
913 Returns True if conflict, False if not.
914 ****************************************************************************/
916 static bool share_conflict(struct share_mode_entry *entry,
917 uint32 access_mask,
918 uint32 share_access)
920 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
921 "entry->share_access = 0x%x, "
922 "entry->private_options = 0x%x\n",
923 (unsigned int)entry->access_mask,
924 (unsigned int)entry->share_access,
925 (unsigned int)entry->private_options));
927 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
928 (unsigned int)access_mask, (unsigned int)share_access));
930 if ((entry->access_mask & (FILE_WRITE_DATA|
931 FILE_APPEND_DATA|
932 FILE_READ_DATA|
933 FILE_EXECUTE|
934 DELETE_ACCESS)) == 0) {
935 DEBUG(10,("share_conflict: No conflict due to "
936 "entry->access_mask = 0x%x\n",
937 (unsigned int)entry->access_mask ));
938 return False;
941 if ((access_mask & (FILE_WRITE_DATA|
942 FILE_APPEND_DATA|
943 FILE_READ_DATA|
944 FILE_EXECUTE|
945 DELETE_ACCESS)) == 0) {
946 DEBUG(10,("share_conflict: No conflict due to "
947 "access_mask = 0x%x\n",
948 (unsigned int)access_mask ));
949 return False;
952 #if 1 /* JRA TEST - Superdebug. */
953 #define CHECK_MASK(num, am, right, sa, share) \
954 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
955 (unsigned int)(num), (unsigned int)(am), \
956 (unsigned int)(right), (unsigned int)(am)&(right) )); \
957 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
958 (unsigned int)(num), (unsigned int)(sa), \
959 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
960 if (((am) & (right)) && !((sa) & (share))) { \
961 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
962 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
963 (unsigned int)(share) )); \
964 return True; \
966 #else
967 #define CHECK_MASK(num, am, right, sa, share) \
968 if (((am) & (right)) && !((sa) & (share))) { \
969 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
970 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
971 (unsigned int)(share) )); \
972 return True; \
974 #endif
976 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
977 share_access, FILE_SHARE_WRITE);
978 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
979 entry->share_access, FILE_SHARE_WRITE);
981 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
982 share_access, FILE_SHARE_READ);
983 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
984 entry->share_access, FILE_SHARE_READ);
986 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
987 share_access, FILE_SHARE_DELETE);
988 CHECK_MASK(6, access_mask, DELETE_ACCESS,
989 entry->share_access, FILE_SHARE_DELETE);
991 DEBUG(10,("share_conflict: No conflict.\n"));
992 return False;
995 #if defined(DEVELOPER)
996 static void validate_my_share_entries(struct smbd_server_connection *sconn,
997 int num,
998 struct share_mode_entry *share_entry)
1000 struct server_id self = messaging_server_id(sconn->msg_ctx);
1001 files_struct *fsp;
1003 if (!serverid_equal(&self, &share_entry->pid)) {
1004 return;
1007 if (is_deferred_open_entry(share_entry) &&
1008 !open_was_deferred(sconn, share_entry->op_mid)) {
1009 char *str = talloc_asprintf(talloc_tos(),
1010 "Got a deferred entry without a request: "
1011 "PANIC: %s\n",
1012 share_mode_str(talloc_tos(), num, share_entry));
1013 smb_panic(str);
1016 if (!is_valid_share_mode_entry(share_entry)) {
1017 return;
1020 fsp = file_find_dif(sconn, share_entry->id,
1021 share_entry->share_file_id);
1022 if (!fsp) {
1023 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1024 share_mode_str(talloc_tos(), num, share_entry) ));
1025 smb_panic("validate_my_share_entries: Cannot match a "
1026 "share entry with an open file\n");
1029 if (is_deferred_open_entry(share_entry)) {
1030 goto panic;
1033 if ((share_entry->op_type == NO_OPLOCK) &&
1034 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
1035 /* Someone has already written to it, but I haven't yet
1036 * noticed */
1037 return;
1040 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
1041 goto panic;
1044 return;
1046 panic:
1048 char *str;
1049 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1050 share_mode_str(talloc_tos(), num, share_entry) ));
1051 str = talloc_asprintf(talloc_tos(),
1052 "validate_my_share_entries: "
1053 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1054 fsp->fsp_name->base_name,
1055 (unsigned int)fsp->oplock_type,
1056 (unsigned int)share_entry->op_type );
1057 smb_panic(str);
1060 #endif
1062 bool is_stat_open(uint32 access_mask)
1064 return (access_mask &&
1065 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
1066 FILE_WRITE_ATTRIBUTES))==0) &&
1067 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
1068 FILE_WRITE_ATTRIBUTES)) != 0));
1071 /****************************************************************************
1072 Deal with share modes
1073 Invarient: Share mode must be locked on entry and exit.
1074 Returns -1 on error, or number of share modes on success (may be zero).
1075 ****************************************************************************/
1077 static NTSTATUS open_mode_check(connection_struct *conn,
1078 struct share_mode_lock *lck,
1079 uint32_t name_hash,
1080 uint32 access_mask,
1081 uint32 share_access,
1082 uint32 create_options,
1083 bool *file_existed)
1085 int i;
1087 if(lck->data->num_share_modes == 0) {
1088 return NT_STATUS_OK;
1091 /* A delete on close prohibits everything */
1093 if (is_delete_on_close_set(lck, name_hash)) {
1095 * Check the delete on close token
1096 * is valid. It could have been left
1097 * after a server crash.
1099 for(i = 0; i < lck->data->num_share_modes; i++) {
1100 if (!share_mode_stale_pid(lck->data, i)) {
1102 *file_existed = true;
1104 return NT_STATUS_DELETE_PENDING;
1107 return NT_STATUS_OK;
1110 if (is_stat_open(access_mask)) {
1111 /* Stat open that doesn't trigger oplock breaks or share mode
1112 * checks... ! JRA. */
1113 return NT_STATUS_OK;
1117 * Check if the share modes will give us access.
1120 #if defined(DEVELOPER)
1121 for(i = 0; i < lck->data->num_share_modes; i++) {
1122 validate_my_share_entries(conn->sconn, i,
1123 &lck->data->share_modes[i]);
1125 #endif
1127 /* Now we check the share modes, after any oplock breaks. */
1128 for(i = 0; i < lck->data->num_share_modes; i++) {
1130 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1131 continue;
1134 /* someone else has a share lock on it, check to see if we can
1135 * too */
1136 if (share_conflict(&lck->data->share_modes[i],
1137 access_mask, share_access)) {
1139 if (share_mode_stale_pid(lck->data, i)) {
1140 continue;
1143 *file_existed = true;
1145 return NT_STATUS_SHARING_VIOLATION;
1149 if (lck->data->num_share_modes != 0) {
1150 *file_existed = true;
1153 return NT_STATUS_OK;
1156 static bool is_delete_request(files_struct *fsp) {
1157 return ((fsp->access_mask == DELETE_ACCESS) &&
1158 (fsp->oplock_type == NO_OPLOCK));
1162 * Send a break message to the oplock holder and delay the open for
1163 * our client.
1166 static NTSTATUS send_break_message(files_struct *fsp,
1167 struct share_mode_entry *exclusive,
1168 uint64_t mid,
1169 int oplock_request)
1171 NTSTATUS status;
1172 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1174 DEBUG(10, ("Sending break request to PID %s\n",
1175 procid_str_static(&exclusive->pid)));
1176 exclusive->op_mid = mid;
1178 /* Create the message. */
1179 share_mode_entry_to_message(msg, exclusive);
1181 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1182 don't want this set in the share mode struct pointed to by lck. */
1184 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1185 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1186 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1189 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1190 MSG_SMB_BREAK_REQUEST,
1191 (uint8 *)msg,
1192 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1193 if (!NT_STATUS_IS_OK(status)) {
1194 DEBUG(3, ("Could not send oplock break message: %s\n",
1195 nt_errstr(status)));
1198 return status;
1202 * Return share_mode_entry pointers for :
1203 * 1). Batch oplock entry.
1204 * 2). Batch or exclusive oplock entry (may be identical to #1).
1205 * bool have_level2_oplock
1206 * bool have_no_oplock.
1207 * Do internal consistency checks on the share mode for a file.
1210 static void find_oplock_types(files_struct *fsp,
1211 int oplock_request,
1212 const struct share_mode_lock *lck,
1213 struct share_mode_entry **pp_batch,
1214 struct share_mode_entry **pp_ex_or_batch,
1215 bool *got_level2,
1216 bool *got_no_oplock)
1218 int i;
1220 *pp_batch = NULL;
1221 *pp_ex_or_batch = NULL;
1222 *got_level2 = false;
1223 *got_no_oplock = false;
1225 /* Ignore stat or internal opens, as is done in
1226 delay_for_batch_oplocks() and
1227 delay_for_exclusive_oplocks().
1229 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1230 return;
1233 for (i=0; i<lck->data->num_share_modes; i++) {
1234 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1235 continue;
1238 if (lck->data->share_modes[i].op_type == NO_OPLOCK &&
1239 is_stat_open(lck->data->share_modes[i].access_mask)) {
1240 /* We ignore stat opens in the table - they
1241 always have NO_OPLOCK and never get or
1242 cause breaks. JRA. */
1243 continue;
1246 if (BATCH_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1247 /* batch - can only be one. */
1248 if (share_mode_stale_pid(lck->data, i)) {
1249 DEBUG(10, ("Found stale batch oplock\n"));
1250 continue;
1252 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1253 smb_panic("Bad batch oplock entry.");
1255 *pp_batch = &lck->data->share_modes[i];
1258 if (EXCLUSIVE_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1259 if (share_mode_stale_pid(lck->data, i)) {
1260 DEBUG(10, ("Found stale duplicate oplock\n"));
1261 continue;
1263 /* Exclusive or batch - can only be one. */
1264 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1265 smb_panic("Bad exclusive or batch oplock entry.");
1267 *pp_ex_or_batch = &lck->data->share_modes[i];
1270 if (LEVEL_II_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1271 if (*pp_batch || *pp_ex_or_batch) {
1272 if (share_mode_stale_pid(lck->data, i)) {
1273 DEBUG(10, ("Found stale LevelII "
1274 "oplock\n"));
1275 continue;
1277 smb_panic("Bad levelII oplock entry.");
1279 *got_level2 = true;
1282 if (lck->data->share_modes[i].op_type == NO_OPLOCK) {
1283 if (*pp_batch || *pp_ex_or_batch) {
1284 if (share_mode_stale_pid(lck->data, i)) {
1285 DEBUG(10, ("Found stale NO_OPLOCK "
1286 "entry\n"));
1287 continue;
1289 smb_panic("Bad no oplock entry.");
1291 *got_no_oplock = true;
1296 static bool delay_for_batch_oplocks(files_struct *fsp,
1297 uint64_t mid,
1298 int oplock_request,
1299 struct share_mode_entry *batch_entry)
1301 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1302 return false;
1304 if (batch_entry == NULL) {
1305 return false;
1308 /* Found a batch oplock */
1309 send_break_message(fsp, batch_entry, mid, oplock_request);
1310 return true;
1313 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1314 uint64_t mid,
1315 int oplock_request,
1316 struct share_mode_entry *ex_entry)
1318 bool delay_it;
1320 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1321 return false;
1323 if (ex_entry == NULL) {
1324 return false;
1327 /* Found an exclusive or batch oplock */
1329 delay_it = is_delete_request(fsp) ?
1330 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1332 if (!delay_it) {
1333 return false;
1336 send_break_message(fsp, ex_entry, mid, oplock_request);
1337 return true;
1340 static bool file_has_brlocks(files_struct *fsp)
1342 struct byte_range_lock *br_lck;
1344 br_lck = brl_get_locks_readonly(fsp);
1345 if (!br_lck)
1346 return false;
1348 return br_lck->num_locks > 0 ? true : false;
1351 static void grant_fsp_oplock_type(files_struct *fsp,
1352 int oplock_request,
1353 bool got_level2_oplock,
1354 bool got_a_none_oplock)
1356 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1357 lp_level2_oplocks(SNUM(fsp->conn));
1359 /* Start by granting what the client asked for,
1360 but ensure no SAMBA_PRIVATE bits can be set. */
1361 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1363 if (oplock_request & INTERNAL_OPEN_ONLY) {
1364 /* No oplocks on internal open. */
1365 fsp->oplock_type = NO_OPLOCK;
1366 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1367 fsp->oplock_type, fsp_str_dbg(fsp)));
1368 return;
1371 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1372 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1373 fsp_str_dbg(fsp)));
1374 fsp->oplock_type = NO_OPLOCK;
1377 if (is_stat_open(fsp->access_mask)) {
1378 /* Leave the value already set. */
1379 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1380 fsp->oplock_type, fsp_str_dbg(fsp)));
1381 return;
1385 * Match what was requested (fsp->oplock_type) with
1386 * what was found in the existing share modes.
1389 if (got_a_none_oplock) {
1390 fsp->oplock_type = NO_OPLOCK;
1391 } else if (got_level2_oplock) {
1392 if (fsp->oplock_type == NO_OPLOCK ||
1393 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1394 /* Store a level2 oplock, but don't tell the client */
1395 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1396 } else {
1397 fsp->oplock_type = LEVEL_II_OPLOCK;
1399 } else {
1400 /* All share_mode_entries are placeholders or deferred.
1401 * Silently upgrade to fake levelII if the client didn't
1402 * ask for an oplock. */
1403 if (fsp->oplock_type == NO_OPLOCK) {
1404 /* Store a level2 oplock, but don't tell the client */
1405 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1410 * Don't grant level2 to clients that don't want them
1411 * or if we've turned them off.
1413 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1414 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1417 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1418 fsp->oplock_type, fsp_str_dbg(fsp)));
1421 static bool request_timed_out(struct timeval request_time,
1422 struct timeval timeout)
1424 struct timeval now, end_time;
1425 GetTimeOfDay(&now);
1426 end_time = timeval_sum(&request_time, &timeout);
1427 return (timeval_compare(&end_time, &now) < 0);
1430 /****************************************************************************
1431 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1432 ****************************************************************************/
1434 static void defer_open(struct share_mode_lock *lck,
1435 struct timeval request_time,
1436 struct timeval timeout,
1437 struct smb_request *req,
1438 struct deferred_open_record *state)
1440 struct server_id self = messaging_server_id(req->sconn->msg_ctx);
1442 /* Paranoia check */
1444 if (lck) {
1445 int i;
1447 for (i=0; i<lck->data->num_share_modes; i++) {
1448 struct share_mode_entry *e = &lck->data->share_modes[i];
1450 if (is_deferred_open_entry(e) &&
1451 serverid_equal(&self, &e->pid) &&
1452 (e->op_mid == req->mid)) {
1453 DEBUG(0, ("Trying to defer an already deferred "
1454 "request: mid=%llu, exiting\n",
1455 (unsigned long long)req->mid));
1456 exit_server("attempt to defer a deferred request");
1461 /* End paranoia check */
1463 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1464 "open entry for mid %llu\n",
1465 (unsigned int)request_time.tv_sec,
1466 (unsigned int)request_time.tv_usec,
1467 (unsigned long long)req->mid));
1469 if (!push_deferred_open_message_smb(req, request_time, timeout,
1470 state->id, (char *)state, sizeof(*state))) {
1471 exit_server("push_deferred_open_message_smb failed");
1473 if (lck) {
1474 add_deferred_open(lck, req->mid, request_time, self, state->id);
1479 /****************************************************************************
1480 On overwrite open ensure that the attributes match.
1481 ****************************************************************************/
1483 static bool open_match_attributes(connection_struct *conn,
1484 uint32 old_dos_attr,
1485 uint32 new_dos_attr,
1486 mode_t existing_unx_mode,
1487 mode_t new_unx_mode,
1488 mode_t *returned_unx_mode)
1490 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1492 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1493 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1495 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1496 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1497 *returned_unx_mode = new_unx_mode;
1498 } else {
1499 *returned_unx_mode = (mode_t)0;
1502 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1503 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1504 "returned_unx_mode = 0%o\n",
1505 (unsigned int)old_dos_attr,
1506 (unsigned int)existing_unx_mode,
1507 (unsigned int)new_dos_attr,
1508 (unsigned int)*returned_unx_mode ));
1510 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1511 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1512 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1513 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1514 return False;
1517 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1518 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1519 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1520 return False;
1523 return True;
1526 /****************************************************************************
1527 Special FCB or DOS processing in the case of a sharing violation.
1528 Try and find a duplicated file handle.
1529 ****************************************************************************/
1531 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1532 connection_struct *conn,
1533 files_struct *fsp_to_dup_into,
1534 const struct smb_filename *smb_fname,
1535 struct file_id id,
1536 uint16 file_pid,
1537 uint64_t vuid,
1538 uint32 access_mask,
1539 uint32 share_access,
1540 uint32 create_options)
1542 files_struct *fsp;
1544 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1545 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1547 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1548 fsp = file_find_di_next(fsp)) {
1550 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1551 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1552 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1553 fsp->fh->fd, (unsigned long long)fsp->vuid,
1554 (unsigned int)fsp->file_pid,
1555 (unsigned int)fsp->fh->private_options,
1556 (unsigned int)fsp->access_mask ));
1558 if (fsp->fh->fd != -1 &&
1559 fsp->vuid == vuid &&
1560 fsp->file_pid == file_pid &&
1561 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1562 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1563 (fsp->access_mask & FILE_WRITE_DATA) &&
1564 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1565 strequal(fsp->fsp_name->stream_name,
1566 smb_fname->stream_name)) {
1567 DEBUG(10,("fcb_or_dos_open: file match\n"));
1568 break;
1572 if (!fsp) {
1573 return NT_STATUS_NOT_FOUND;
1576 /* quite an insane set of semantics ... */
1577 if (is_executable(smb_fname->base_name) &&
1578 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1579 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1580 return NT_STATUS_INVALID_PARAMETER;
1583 /* We need to duplicate this fsp. */
1584 return dup_file_fsp(req, fsp, access_mask, share_access,
1585 create_options, fsp_to_dup_into);
1588 static void schedule_defer_open(struct share_mode_lock *lck,
1589 struct timeval request_time,
1590 struct smb_request *req)
1592 struct deferred_open_record state;
1594 /* This is a relative time, added to the absolute
1595 request_time value to get the absolute timeout time.
1596 Note that if this is the second or greater time we enter
1597 this codepath for this particular request mid then
1598 request_time is left as the absolute time of the *first*
1599 time this request mid was processed. This is what allows
1600 the request to eventually time out. */
1602 struct timeval timeout;
1604 /* Normally the smbd we asked should respond within
1605 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1606 * the client did, give twice the timeout as a safety
1607 * measure here in case the other smbd is stuck
1608 * somewhere else. */
1610 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1612 /* Nothing actually uses state.delayed_for_oplocks
1613 but it's handy to differentiate in debug messages
1614 between a 30 second delay due to oplock break, and
1615 a 1 second delay for share mode conflicts. */
1617 state.delayed_for_oplocks = True;
1618 state.async_open = false;
1619 state.id = lck->data->id;
1621 if (!request_timed_out(request_time, timeout)) {
1622 defer_open(lck, request_time, timeout, req, &state);
1626 /****************************************************************************
1627 Reschedule an open call that went asynchronous.
1628 ****************************************************************************/
1630 static void schedule_async_open(struct timeval request_time,
1631 struct smb_request *req)
1633 struct deferred_open_record state;
1634 struct timeval timeout;
1636 timeout = timeval_set(20, 0);
1638 ZERO_STRUCT(state);
1639 state.delayed_for_oplocks = false;
1640 state.async_open = true;
1642 if (!request_timed_out(request_time, timeout)) {
1643 defer_open(NULL, request_time, timeout, req, &state);
1647 /****************************************************************************
1648 Work out what access_mask to use from what the client sent us.
1649 ****************************************************************************/
1651 static NTSTATUS smbd_calculate_maximum_allowed_access(
1652 connection_struct *conn,
1653 const struct smb_filename *smb_fname,
1654 uint32_t *p_access_mask)
1656 struct security_descriptor *sd;
1657 uint32_t access_granted;
1658 NTSTATUS status;
1660 if (get_current_uid(conn) == (uid_t)0) {
1661 *p_access_mask |= FILE_GENERIC_ALL;
1662 return NT_STATUS_OK;
1665 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1666 (SECINFO_OWNER |
1667 SECINFO_GROUP |
1668 SECINFO_DACL),&sd);
1670 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1672 * File did not exist
1674 *p_access_mask = FILE_GENERIC_ALL;
1675 return NT_STATUS_OK;
1677 if (!NT_STATUS_IS_OK(status)) {
1678 DEBUG(10,("smbd_calculate_access_mask: "
1679 "Could not get acl on file %s: %s\n",
1680 smb_fname_str_dbg(smb_fname),
1681 nt_errstr(status)));
1682 return NT_STATUS_ACCESS_DENIED;
1686 * Never test FILE_READ_ATTRIBUTES. se_file_access_check()
1687 * also takes care of owner WRITE_DAC and READ_CONTROL.
1689 status = se_file_access_check(sd,
1690 get_current_nttok(conn),
1691 false,
1692 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1693 &access_granted);
1695 TALLOC_FREE(sd);
1697 if (!NT_STATUS_IS_OK(status)) {
1698 DEBUG(10, ("smbd_calculate_access_mask: "
1699 "Access denied on file %s: "
1700 "when calculating maximum access\n",
1701 smb_fname_str_dbg(smb_fname)));
1702 return NT_STATUS_ACCESS_DENIED;
1704 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1705 return NT_STATUS_OK;
1708 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1709 const struct smb_filename *smb_fname,
1710 uint32_t access_mask,
1711 uint32_t *access_mask_out)
1713 NTSTATUS status;
1714 uint32_t orig_access_mask = access_mask;
1715 uint32_t rejected_share_access;
1718 * Convert GENERIC bits to specific bits.
1721 se_map_generic(&access_mask, &file_generic_mapping);
1723 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1724 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1726 status = smbd_calculate_maximum_allowed_access(
1727 conn, smb_fname, &access_mask);
1729 if (!NT_STATUS_IS_OK(status)) {
1730 return status;
1733 access_mask &= conn->share_access;
1736 rejected_share_access = access_mask & ~(conn->share_access);
1738 if (rejected_share_access) {
1739 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1740 "file %s: rejected by share access mask[0x%08X] "
1741 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1742 smb_fname_str_dbg(smb_fname),
1743 conn->share_access,
1744 orig_access_mask, access_mask,
1745 rejected_share_access));
1746 return NT_STATUS_ACCESS_DENIED;
1749 *access_mask_out = access_mask;
1750 return NT_STATUS_OK;
1753 /****************************************************************************
1754 Remove the deferred open entry under lock.
1755 ****************************************************************************/
1757 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1758 struct server_id pid)
1760 struct share_mode_lock *lck = get_existing_share_mode_lock(
1761 talloc_tos(), id);
1762 if (lck == NULL) {
1763 DEBUG(0, ("could not get share mode lock\n"));
1764 return;
1766 del_deferred_open_entry(lck, mid, pid);
1767 TALLOC_FREE(lck);
1770 /****************************************************************************
1771 Return true if this is a state pointer to an asynchronous create.
1772 ****************************************************************************/
1774 bool is_deferred_open_async(const void *ptr)
1776 const struct deferred_open_record *state = (const struct deferred_open_record *)ptr;
1778 return state->async_open;
1781 static bool clear_ads(uint32_t create_disposition)
1783 bool ret = false;
1785 switch (create_disposition) {
1786 case FILE_SUPERSEDE:
1787 case FILE_OVERWRITE_IF:
1788 case FILE_OVERWRITE:
1789 ret = true;
1790 break;
1791 default:
1792 break;
1794 return ret;
1797 static int disposition_to_open_flags(uint32_t create_disposition)
1799 int ret = 0;
1802 * Currently we're using FILE_SUPERSEDE as the same as
1803 * FILE_OVERWRITE_IF but they really are
1804 * different. FILE_SUPERSEDE deletes an existing file
1805 * (requiring delete access) then recreates it.
1808 switch (create_disposition) {
1809 case FILE_SUPERSEDE:
1810 case FILE_OVERWRITE_IF:
1812 * If file exists replace/overwrite. If file doesn't
1813 * exist create.
1815 ret = O_CREAT|O_TRUNC;
1816 break;
1818 case FILE_OPEN:
1820 * If file exists open. If file doesn't exist error.
1822 ret = 0;
1823 break;
1825 case FILE_OVERWRITE:
1827 * If file exists overwrite. If file doesn't exist
1828 * error.
1830 ret = O_TRUNC;
1831 break;
1833 case FILE_CREATE:
1835 * If file exists error. If file doesn't exist create.
1837 ret = O_CREAT|O_EXCL;
1838 break;
1840 case FILE_OPEN_IF:
1842 * If file exists open. If file doesn't exist create.
1844 ret = O_CREAT;
1845 break;
1847 return ret;
1850 /****************************************************************************
1851 Open a file with a share mode. Passed in an already created files_struct *.
1852 ****************************************************************************/
1854 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1855 struct smb_request *req,
1856 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1857 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1858 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1859 uint32 create_options, /* options such as delete on close. */
1860 uint32 new_dos_attributes, /* attributes used for new file. */
1861 int oplock_request, /* internal Samba oplock codes. */
1862 /* Information (FILE_EXISTS etc.) */
1863 uint32_t private_flags, /* Samba specific flags. */
1864 int *pinfo,
1865 files_struct *fsp)
1867 struct smb_filename *smb_fname = fsp->fsp_name;
1868 int flags=0;
1869 int flags2=0;
1870 bool file_existed = VALID_STAT(smb_fname->st);
1871 bool def_acl = False;
1872 bool posix_open = False;
1873 bool new_file_created = False;
1874 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1875 mode_t new_unx_mode = (mode_t)0;
1876 mode_t unx_mode = (mode_t)0;
1877 int info;
1878 uint32 existing_dos_attributes = 0;
1879 struct timeval request_time = timeval_zero();
1880 struct share_mode_lock *lck = NULL;
1881 uint32 open_access_mask = access_mask;
1882 NTSTATUS status;
1883 char *parent_dir;
1884 SMB_STRUCT_STAT saved_stat = smb_fname->st;
1886 if (conn->printer) {
1888 * Printers are handled completely differently.
1889 * Most of the passed parameters are ignored.
1892 if (pinfo) {
1893 *pinfo = FILE_WAS_CREATED;
1896 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1897 smb_fname_str_dbg(smb_fname)));
1899 if (!req) {
1900 DEBUG(0,("open_file_ntcreate: printer open without "
1901 "an SMB request!\n"));
1902 return NT_STATUS_INTERNAL_ERROR;
1905 return print_spool_open(fsp, smb_fname->base_name,
1906 req->vuid);
1909 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1910 NULL)) {
1911 return NT_STATUS_NO_MEMORY;
1914 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1915 posix_open = True;
1916 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1917 new_dos_attributes = 0;
1918 } else {
1919 /* Windows allows a new file to be created and
1920 silently removes a FILE_ATTRIBUTE_DIRECTORY
1921 sent by the client. Do the same. */
1923 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1925 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1926 * created new. */
1927 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1928 smb_fname, parent_dir);
1931 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1932 "access_mask=0x%x share_access=0x%x "
1933 "create_disposition = 0x%x create_options=0x%x "
1934 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1935 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1936 access_mask, share_access, create_disposition,
1937 create_options, (unsigned int)unx_mode, oplock_request,
1938 (unsigned int)private_flags));
1940 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1941 DEBUG(0, ("No smb request but not an internal only open!\n"));
1942 return NT_STATUS_INTERNAL_ERROR;
1946 * Only non-internal opens can be deferred at all
1949 if (req) {
1950 void *ptr;
1951 if (get_deferred_open_message_state(req,
1952 &request_time,
1953 &ptr)) {
1954 /* Remember the absolute time of the original
1955 request with this mid. We'll use it later to
1956 see if this has timed out. */
1958 /* If it was an async create retry, the file
1959 didn't exist. */
1961 if (is_deferred_open_async(ptr)) {
1962 SET_STAT_INVALID(smb_fname->st);
1963 file_existed = false;
1964 } else {
1965 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1966 /* Remove the deferred open entry under lock. */
1967 remove_deferred_open_entry(
1968 state->id, req->mid,
1969 messaging_server_id(req->sconn->msg_ctx));
1972 /* Ensure we don't reprocess this message. */
1973 remove_deferred_open_message_smb(req->sconn, req->mid);
1977 if (!posix_open) {
1978 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1979 if (file_existed) {
1980 existing_dos_attributes = dos_mode(conn, smb_fname);
1984 /* ignore any oplock requests if oplocks are disabled */
1985 if (!lp_oplocks(SNUM(conn)) ||
1986 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1987 /* Mask off everything except the private Samba bits. */
1988 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1991 /* this is for OS/2 long file names - say we don't support them */
1992 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1993 /* OS/2 Workplace shell fix may be main code stream in a later
1994 * release. */
1995 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1996 "supported.\n"));
1997 if (use_nt_status()) {
1998 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2000 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2003 switch( create_disposition ) {
2004 case FILE_OPEN:
2005 /* If file exists open. If file doesn't exist error. */
2006 if (!file_existed) {
2007 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2008 "requested for file %s and file "
2009 "doesn't exist.\n",
2010 smb_fname_str_dbg(smb_fname)));
2011 errno = ENOENT;
2012 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2014 break;
2016 case FILE_OVERWRITE:
2017 /* If file exists overwrite. If file doesn't exist
2018 * error. */
2019 if (!file_existed) {
2020 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2021 "requested for file %s and file "
2022 "doesn't exist.\n",
2023 smb_fname_str_dbg(smb_fname) ));
2024 errno = ENOENT;
2025 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2027 break;
2029 case FILE_CREATE:
2030 /* If file exists error. If file doesn't exist
2031 * create. */
2032 if (file_existed) {
2033 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2034 "requested for file %s and file "
2035 "already exists.\n",
2036 smb_fname_str_dbg(smb_fname)));
2037 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2038 errno = EISDIR;
2039 } else {
2040 errno = EEXIST;
2042 return map_nt_error_from_unix(errno);
2044 break;
2046 case FILE_SUPERSEDE:
2047 case FILE_OVERWRITE_IF:
2048 case FILE_OPEN_IF:
2049 break;
2050 default:
2051 return NT_STATUS_INVALID_PARAMETER;
2054 flags2 = disposition_to_open_flags(create_disposition);
2056 /* We only care about matching attributes on file exists and
2057 * overwrite. */
2059 if (!posix_open && file_existed &&
2060 ((create_disposition == FILE_OVERWRITE) ||
2061 (create_disposition == FILE_OVERWRITE_IF))) {
2062 if (!open_match_attributes(conn, existing_dos_attributes,
2063 new_dos_attributes,
2064 smb_fname->st.st_ex_mode,
2065 unx_mode, &new_unx_mode)) {
2066 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2067 "for file %s (%x %x) (0%o, 0%o)\n",
2068 smb_fname_str_dbg(smb_fname),
2069 existing_dos_attributes,
2070 new_dos_attributes,
2071 (unsigned int)smb_fname->st.st_ex_mode,
2072 (unsigned int)unx_mode ));
2073 errno = EACCES;
2074 return NT_STATUS_ACCESS_DENIED;
2078 status = smbd_calculate_access_mask(conn, smb_fname,
2079 access_mask,
2080 &access_mask);
2081 if (!NT_STATUS_IS_OK(status)) {
2082 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2083 "on file %s returned %s\n",
2084 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2085 return status;
2088 open_access_mask = access_mask;
2090 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
2091 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2094 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2095 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2096 access_mask));
2099 * Note that we ignore the append flag as append does not
2100 * mean the same thing under DOS and Unix.
2103 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
2104 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
2105 /* DENY_DOS opens are always underlying read-write on the
2106 file handle, no matter what the requested access mask
2107 says. */
2108 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
2109 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
2110 flags = O_RDWR;
2111 } else {
2112 flags = O_WRONLY;
2114 } else {
2115 flags = O_RDONLY;
2119 * Currently we only look at FILE_WRITE_THROUGH for create options.
2122 #if defined(O_SYNC)
2123 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2124 flags2 |= O_SYNC;
2126 #endif /* O_SYNC */
2128 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2129 flags2 |= O_APPEND;
2132 if (!posix_open && !CAN_WRITE(conn)) {
2134 * We should really return a permission denied error if either
2135 * O_CREAT or O_TRUNC are set, but for compatibility with
2136 * older versions of Samba we just AND them out.
2138 flags2 &= ~(O_CREAT|O_TRUNC);
2142 * Ensure we can't write on a read-only share or file.
2145 if (flags != O_RDONLY && file_existed &&
2146 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2147 DEBUG(5,("open_file_ntcreate: write access requested for "
2148 "file %s on read only %s\n",
2149 smb_fname_str_dbg(smb_fname),
2150 !CAN_WRITE(conn) ? "share" : "file" ));
2151 errno = EACCES;
2152 return NT_STATUS_ACCESS_DENIED;
2155 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2156 fsp->share_access = share_access;
2157 fsp->fh->private_options = private_flags;
2158 fsp->access_mask = open_access_mask; /* We change this to the
2159 * requested access_mask after
2160 * the open is done. */
2161 fsp->posix_open = posix_open;
2163 /* Ensure no SAMBA_PRIVATE bits can be set. */
2164 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2166 if (timeval_is_zero(&request_time)) {
2167 request_time = fsp->open_time;
2170 if (file_existed) {
2171 struct share_mode_entry *batch_entry = NULL;
2172 struct share_mode_entry *exclusive_entry = NULL;
2173 bool got_level2_oplock = false;
2174 bool got_a_none_oplock = false;
2175 struct file_id id;
2177 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2178 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2180 lck = get_share_mode_lock(talloc_tos(), id,
2181 conn->connectpath,
2182 smb_fname, &old_write_time);
2183 if (lck == NULL) {
2184 DEBUG(0, ("Could not get share mode lock\n"));
2185 return NT_STATUS_SHARING_VIOLATION;
2188 /* Get the types we need to examine. */
2189 find_oplock_types(fsp,
2190 oplock_request,
2191 lck,
2192 &batch_entry,
2193 &exclusive_entry,
2194 &got_level2_oplock,
2195 &got_a_none_oplock);
2197 /* First pass - send break only on batch oplocks. */
2198 if ((req != NULL) &&
2199 delay_for_batch_oplocks(fsp,
2200 req->mid,
2201 oplock_request,
2202 batch_entry)) {
2203 schedule_defer_open(lck, request_time, req);
2204 TALLOC_FREE(lck);
2205 return NT_STATUS_SHARING_VIOLATION;
2208 /* Use the client requested access mask here, not the one we
2209 * open with. */
2210 status = open_mode_check(conn, lck, fsp->name_hash,
2211 access_mask, share_access,
2212 create_options, &file_existed);
2214 if (NT_STATUS_IS_OK(status)) {
2215 /* We might be going to allow this open. Check oplock
2216 * status again. */
2217 /* Second pass - send break for both batch or
2218 * exclusive oplocks. */
2219 if ((req != NULL) &&
2220 delay_for_exclusive_oplocks(
2221 fsp,
2222 req->mid,
2223 oplock_request,
2224 exclusive_entry)) {
2225 schedule_defer_open(lck, request_time, req);
2226 TALLOC_FREE(lck);
2227 return NT_STATUS_SHARING_VIOLATION;
2231 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2232 /* DELETE_PENDING is not deferred for a second */
2233 TALLOC_FREE(lck);
2234 return status;
2237 grant_fsp_oplock_type(fsp,
2238 oplock_request,
2239 got_level2_oplock,
2240 got_a_none_oplock);
2242 if (!NT_STATUS_IS_OK(status)) {
2243 uint32 can_access_mask;
2244 bool can_access = True;
2246 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2248 /* Check if this can be done with the deny_dos and fcb
2249 * calls. */
2250 if (private_flags &
2251 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2252 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2253 if (req == NULL) {
2254 DEBUG(0, ("DOS open without an SMB "
2255 "request!\n"));
2256 TALLOC_FREE(lck);
2257 return NT_STATUS_INTERNAL_ERROR;
2260 /* Use the client requested access mask here,
2261 * not the one we open with. */
2262 status = fcb_or_dos_open(req,
2263 conn,
2264 fsp,
2265 smb_fname,
2267 req->smbpid,
2268 req->vuid,
2269 access_mask,
2270 share_access,
2271 create_options);
2273 if (NT_STATUS_IS_OK(status)) {
2274 TALLOC_FREE(lck);
2275 if (pinfo) {
2276 *pinfo = FILE_WAS_OPENED;
2278 return NT_STATUS_OK;
2283 * This next line is a subtlety we need for
2284 * MS-Access. If a file open will fail due to share
2285 * permissions and also for security (access) reasons,
2286 * we need to return the access failed error, not the
2287 * share error. We can't open the file due to kernel
2288 * oplock deadlock (it's possible we failed above on
2289 * the open_mode_check()) so use a userspace check.
2292 if (flags & O_RDWR) {
2293 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2294 } else if (flags & O_WRONLY) {
2295 can_access_mask = FILE_WRITE_DATA;
2296 } else {
2297 can_access_mask = FILE_READ_DATA;
2300 if (((can_access_mask & FILE_WRITE_DATA) &&
2301 !CAN_WRITE(conn)) ||
2302 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2303 smb_fname, can_access_mask))) {
2304 can_access = False;
2308 * If we're returning a share violation, ensure we
2309 * cope with the braindead 1 second delay.
2312 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2313 lp_defer_sharing_violations()) {
2314 struct timeval timeout;
2315 struct deferred_open_record state;
2316 int timeout_usecs;
2318 /* this is a hack to speed up torture tests
2319 in 'make test' */
2320 timeout_usecs = lp_parm_int(SNUM(conn),
2321 "smbd","sharedelay",
2322 SHARING_VIOLATION_USEC_WAIT);
2324 /* This is a relative time, added to the absolute
2325 request_time value to get the absolute timeout time.
2326 Note that if this is the second or greater time we enter
2327 this codepath for this particular request mid then
2328 request_time is left as the absolute time of the *first*
2329 time this request mid was processed. This is what allows
2330 the request to eventually time out. */
2332 timeout = timeval_set(0, timeout_usecs);
2334 /* Nothing actually uses state.delayed_for_oplocks
2335 but it's handy to differentiate in debug messages
2336 between a 30 second delay due to oplock break, and
2337 a 1 second delay for share mode conflicts. */
2339 state.delayed_for_oplocks = False;
2340 state.async_open = false;
2341 state.id = id;
2343 if ((req != NULL)
2344 && !request_timed_out(request_time,
2345 timeout)) {
2346 defer_open(lck, request_time, timeout,
2347 req, &state);
2351 TALLOC_FREE(lck);
2352 if (can_access) {
2354 * We have detected a sharing violation here
2355 * so return the correct error code
2357 status = NT_STATUS_SHARING_VIOLATION;
2358 } else {
2359 status = NT_STATUS_ACCESS_DENIED;
2361 return status;
2365 * We exit this block with the share entry *locked*.....
2369 SMB_ASSERT(!file_existed || (lck != NULL));
2372 * Ensure we pay attention to default ACLs on directories if required.
2375 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2376 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2377 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2380 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2381 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2382 (unsigned int)flags, (unsigned int)flags2,
2383 (unsigned int)unx_mode, (unsigned int)access_mask,
2384 (unsigned int)open_access_mask));
2387 * open_file strips any O_TRUNC flags itself.
2390 fsp_open = open_file(fsp, conn, req, parent_dir,
2391 flags|flags2, unx_mode, access_mask,
2392 open_access_mask, &new_file_created);
2394 if (!NT_STATUS_IS_OK(fsp_open)) {
2395 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2396 schedule_async_open(request_time, req);
2398 TALLOC_FREE(lck);
2399 return fsp_open;
2402 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2404 * The file did exist, but some other (local or NFS)
2405 * process either renamed/unlinked and re-created the
2406 * file with different dev/ino after we walked the path,
2407 * but before we did the open. We could retry the
2408 * open but it's a rare enough case it's easier to
2409 * just fail the open to prevent creating any problems
2410 * in the open file db having the wrong dev/ino key.
2412 TALLOC_FREE(lck);
2413 fd_close(fsp);
2414 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2415 "Old (dev=0x%llu, ino =0x%llu). "
2416 "New (dev=0x%llu, ino=0x%llu). Failing open "
2417 " with NT_STATUS_ACCESS_DENIED.\n",
2418 smb_fname_str_dbg(smb_fname),
2419 (unsigned long long)saved_stat.st_ex_dev,
2420 (unsigned long long)saved_stat.st_ex_ino,
2421 (unsigned long long)smb_fname->st.st_ex_dev,
2422 (unsigned long long)smb_fname->st.st_ex_ino));
2423 return NT_STATUS_ACCESS_DENIED;
2426 if (!file_existed) {
2427 struct share_mode_entry *batch_entry = NULL;
2428 struct share_mode_entry *exclusive_entry = NULL;
2429 bool got_level2_oplock = false;
2430 bool got_a_none_oplock = false;
2431 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2432 struct file_id id;
2434 * Deal with the race condition where two smbd's detect the
2435 * file doesn't exist and do the create at the same time. One
2436 * of them will win and set a share mode, the other (ie. this
2437 * one) should check if the requested share mode for this
2438 * create is allowed.
2442 * Now the file exists and fsp is successfully opened,
2443 * fsp->dev and fsp->inode are valid and should replace the
2444 * dev=0,inode=0 from a non existent file. Spotted by
2445 * Nadav Danieli <nadavd@exanet.com>. JRA.
2448 id = fsp->file_id;
2450 lck = get_share_mode_lock(talloc_tos(), id,
2451 conn->connectpath,
2452 smb_fname, &old_write_time);
2454 if (lck == NULL) {
2455 DEBUG(0, ("open_file_ntcreate: Could not get share "
2456 "mode lock for %s\n",
2457 smb_fname_str_dbg(smb_fname)));
2458 fd_close(fsp);
2459 return NT_STATUS_SHARING_VIOLATION;
2462 /* Get the types we need to examine. */
2463 find_oplock_types(fsp,
2464 oplock_request,
2465 lck,
2466 &batch_entry,
2467 &exclusive_entry,
2468 &got_level2_oplock,
2469 &got_a_none_oplock);
2471 /* First pass - send break only on batch oplocks. */
2472 if ((req != NULL) &&
2473 delay_for_batch_oplocks(fsp,
2474 req->mid,
2475 oplock_request,
2476 batch_entry)) {
2477 schedule_defer_open(lck, request_time, req);
2478 TALLOC_FREE(lck);
2479 fd_close(fsp);
2480 return NT_STATUS_SHARING_VIOLATION;
2483 status = open_mode_check(conn, lck, fsp->name_hash,
2484 access_mask, share_access,
2485 create_options, &file_existed);
2487 if (NT_STATUS_IS_OK(status)) {
2488 /* We might be going to allow this open. Check oplock
2489 * status again. */
2490 /* Second pass - send break for both batch or
2491 * exclusive oplocks. */
2492 if ((req != NULL) &&
2493 delay_for_exclusive_oplocks(
2494 fsp,
2495 req->mid,
2496 oplock_request,
2497 exclusive_entry)) {
2498 schedule_defer_open(lck, request_time, req);
2499 TALLOC_FREE(lck);
2500 fd_close(fsp);
2501 return NT_STATUS_SHARING_VIOLATION;
2505 if (!NT_STATUS_IS_OK(status)) {
2506 struct deferred_open_record state;
2508 state.delayed_for_oplocks = False;
2509 state.async_open = false;
2510 state.id = id;
2512 /* Do it all over again immediately. In the second
2513 * round we will find that the file existed and handle
2514 * the DELETE_PENDING and FCB cases correctly. No need
2515 * to duplicate the code here. Essentially this is a
2516 * "goto top of this function", but don't tell
2517 * anybody... */
2519 if (req != NULL) {
2520 defer_open(lck, request_time, timeval_zero(),
2521 req, &state);
2523 TALLOC_FREE(lck);
2524 fd_close(fsp);
2525 return status;
2528 grant_fsp_oplock_type(fsp,
2529 oplock_request,
2530 got_level2_oplock,
2531 got_a_none_oplock);
2534 * We exit this block with the share entry *locked*.....
2539 SMB_ASSERT(lck != NULL);
2541 /* Delete streams if create_disposition requires it */
2542 if (!new_file_created && clear_ads(create_disposition) &&
2543 !is_ntfs_stream_smb_fname(smb_fname)) {
2544 status = delete_all_streams(conn, smb_fname->base_name);
2545 if (!NT_STATUS_IS_OK(status)) {
2546 TALLOC_FREE(lck);
2547 fd_close(fsp);
2548 return status;
2552 /* note that we ignore failure for the following. It is
2553 basically a hack for NFS, and NFS will never set one of
2554 these only read them. Nobody but Samba can ever set a deny
2555 mode and we have already checked our more authoritative
2556 locking database for permission to set this deny mode. If
2557 the kernel refuses the operations then the kernel is wrong.
2558 note that GPFS supports it as well - jmcd */
2560 if (fsp->fh->fd != -1) {
2561 int ret_flock;
2562 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2563 if(ret_flock == -1 ){
2565 TALLOC_FREE(lck);
2566 fd_close(fsp);
2568 return NT_STATUS_SHARING_VIOLATION;
2573 * At this point onwards, we can guarentee that the share entry
2574 * is locked, whether we created the file or not, and that the
2575 * deny mode is compatible with all current opens.
2579 * If requested, truncate the file.
2582 if (!new_file_created && (flags2&O_TRUNC)) {
2584 * We are modifying the file after open - update the stat
2585 * struct..
2587 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2588 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2589 status = map_nt_error_from_unix(errno);
2590 TALLOC_FREE(lck);
2591 fd_close(fsp);
2592 return status;
2597 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2598 * but we don't have to store this - just ignore it on access check.
2600 if (conn->sconn->using_smb2) {
2602 * SMB2 doesn't return it (according to Microsoft tests).
2603 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2604 * File created with access = 0x7 (Read, Write, Delete)
2605 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2607 fsp->access_mask = access_mask;
2608 } else {
2609 /* But SMB1 does. */
2610 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2613 if (file_existed) {
2614 /* stat opens on existing files don't get oplocks. */
2615 if (is_stat_open(open_access_mask)) {
2616 fsp->oplock_type = NO_OPLOCK;
2620 if (new_file_created) {
2621 info = FILE_WAS_CREATED;
2622 } else {
2623 if (flags2 & O_TRUNC) {
2624 info = FILE_WAS_OVERWRITTEN;
2625 } else {
2626 info = FILE_WAS_OPENED;
2630 if (pinfo) {
2631 *pinfo = info;
2635 * Setup the oplock info in both the shared memory and
2636 * file structs.
2639 status = set_file_oplock(fsp, fsp->oplock_type);
2640 if (!NT_STATUS_IS_OK(status)) {
2642 * Could not get the kernel oplock or there are byte-range
2643 * locks on the file.
2645 fsp->oplock_type = NO_OPLOCK;
2648 set_share_mode(lck, fsp, get_current_uid(conn),
2649 req ? req->mid : 0,
2650 fsp->oplock_type);
2652 /* Handle strange delete on close create semantics. */
2653 if (create_options & FILE_DELETE_ON_CLOSE) {
2655 status = can_set_delete_on_close(fsp, new_dos_attributes);
2657 if (!NT_STATUS_IS_OK(status)) {
2658 /* Remember to delete the mode we just added. */
2659 del_share_mode(lck, fsp);
2660 TALLOC_FREE(lck);
2661 fd_close(fsp);
2662 return status;
2664 /* Note that here we set the *inital* delete on close flag,
2665 not the regular one. The magic gets handled in close. */
2666 fsp->initial_delete_on_close = True;
2669 if (info != FILE_WAS_OPENED) {
2670 /* Files should be initially set as archive */
2671 if (lp_map_archive(SNUM(conn)) ||
2672 lp_store_dos_attributes(SNUM(conn))) {
2673 if (!posix_open) {
2674 if (file_set_dosmode(conn, smb_fname,
2675 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2676 parent_dir, true) == 0) {
2677 unx_mode = smb_fname->st.st_ex_mode;
2683 /* Determine sparse flag. */
2684 if (posix_open) {
2685 /* POSIX opens are sparse by default. */
2686 fsp->is_sparse = true;
2687 } else {
2688 fsp->is_sparse = (file_existed &&
2689 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2693 * Take care of inherited ACLs on created files - if default ACL not
2694 * selected.
2697 if (!posix_open && new_file_created && !def_acl) {
2699 int saved_errno = errno; /* We might get ENOSYS in the next
2700 * call.. */
2702 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2703 errno == ENOSYS) {
2704 errno = saved_errno; /* Ignore ENOSYS */
2707 } else if (new_unx_mode) {
2709 int ret = -1;
2711 /* Attributes need changing. File already existed. */
2714 int saved_errno = errno; /* We might get ENOSYS in the
2715 * next call.. */
2716 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2718 if (ret == -1 && errno == ENOSYS) {
2719 errno = saved_errno; /* Ignore ENOSYS */
2720 } else {
2721 DEBUG(5, ("open_file_ntcreate: reset "
2722 "attributes of file %s to 0%o\n",
2723 smb_fname_str_dbg(smb_fname),
2724 (unsigned int)new_unx_mode));
2725 ret = 0; /* Don't do the fchmod below. */
2729 if ((ret == -1) &&
2730 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2731 DEBUG(5, ("open_file_ntcreate: failed to reset "
2732 "attributes of file %s to 0%o\n",
2733 smb_fname_str_dbg(smb_fname),
2734 (unsigned int)new_unx_mode));
2737 /* If this is a successful open, we must remove any deferred open
2738 * records. */
2739 if (req != NULL) {
2740 del_deferred_open_entry(lck, req->mid,
2741 messaging_server_id(req->sconn->msg_ctx));
2743 TALLOC_FREE(lck);
2745 return NT_STATUS_OK;
2749 /****************************************************************************
2750 Open a file for for write to ensure that we can fchmod it.
2751 ****************************************************************************/
2753 NTSTATUS open_file_fchmod(connection_struct *conn,
2754 struct smb_filename *smb_fname,
2755 files_struct **result)
2757 if (!VALID_STAT(smb_fname->st)) {
2758 return NT_STATUS_INVALID_PARAMETER;
2761 return SMB_VFS_CREATE_FILE(
2762 conn, /* conn */
2763 NULL, /* req */
2764 0, /* root_dir_fid */
2765 smb_fname, /* fname */
2766 FILE_WRITE_DATA, /* access_mask */
2767 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2768 FILE_SHARE_DELETE),
2769 FILE_OPEN, /* create_disposition*/
2770 0, /* create_options */
2771 0, /* file_attributes */
2772 INTERNAL_OPEN_ONLY, /* oplock_request */
2773 0, /* allocation_size */
2774 0, /* private_flags */
2775 NULL, /* sd */
2776 NULL, /* ea_list */
2777 result, /* result */
2778 NULL); /* pinfo */
2781 static NTSTATUS mkdir_internal(connection_struct *conn,
2782 struct smb_filename *smb_dname,
2783 uint32 file_attributes)
2785 mode_t mode;
2786 char *parent_dir = NULL;
2787 NTSTATUS status;
2788 bool posix_open = false;
2789 bool need_re_stat = false;
2790 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2792 if(access_mask & ~(conn->share_access)) {
2793 DEBUG(5,("mkdir_internal: failing share access "
2794 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
2795 return NT_STATUS_ACCESS_DENIED;
2798 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2799 NULL)) {
2800 return NT_STATUS_NO_MEMORY;
2803 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2804 posix_open = true;
2805 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2806 } else {
2807 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2810 status = check_parent_access(conn,
2811 smb_dname,
2812 access_mask);
2813 if(!NT_STATUS_IS_OK(status)) {
2814 DEBUG(5,("mkdir_internal: check_parent_access "
2815 "on directory %s for path %s returned %s\n",
2816 parent_dir,
2817 smb_dname->base_name,
2818 nt_errstr(status) ));
2819 return status;
2822 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2823 return map_nt_error_from_unix(errno);
2826 /* Ensure we're checking for a symlink here.... */
2827 /* We don't want to get caught by a symlink racer. */
2829 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2830 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2831 smb_fname_str_dbg(smb_dname), strerror(errno)));
2832 return map_nt_error_from_unix(errno);
2835 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2836 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2837 smb_fname_str_dbg(smb_dname)));
2838 return NT_STATUS_NOT_A_DIRECTORY;
2841 if (lp_store_dos_attributes(SNUM(conn))) {
2842 if (!posix_open) {
2843 file_set_dosmode(conn, smb_dname,
2844 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2845 parent_dir, true);
2849 if (lp_inherit_perms(SNUM(conn))) {
2850 inherit_access_posix_acl(conn, parent_dir,
2851 smb_dname->base_name, mode);
2852 need_re_stat = true;
2855 if (!posix_open) {
2857 * Check if high bits should have been set,
2858 * then (if bits are missing): add them.
2859 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2860 * dir.
2862 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2863 (mode & ~smb_dname->st.st_ex_mode)) {
2864 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2865 (smb_dname->st.st_ex_mode |
2866 (mode & ~smb_dname->st.st_ex_mode)));
2867 need_re_stat = true;
2871 /* Change the owner if required. */
2872 if (lp_inherit_owner(SNUM(conn))) {
2873 change_dir_owner_to_parent(conn, parent_dir,
2874 smb_dname->base_name,
2875 &smb_dname->st);
2876 need_re_stat = true;
2879 if (need_re_stat) {
2880 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2881 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2882 smb_fname_str_dbg(smb_dname), strerror(errno)));
2883 return map_nt_error_from_unix(errno);
2887 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2888 smb_dname->base_name);
2890 return NT_STATUS_OK;
2893 /****************************************************************************
2894 Open a directory from an NT SMB call.
2895 ****************************************************************************/
2897 static NTSTATUS open_directory(connection_struct *conn,
2898 struct smb_request *req,
2899 struct smb_filename *smb_dname,
2900 uint32 access_mask,
2901 uint32 share_access,
2902 uint32 create_disposition,
2903 uint32 create_options,
2904 uint32 file_attributes,
2905 int *pinfo,
2906 files_struct **result)
2908 files_struct *fsp = NULL;
2909 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2910 struct share_mode_lock *lck = NULL;
2911 NTSTATUS status;
2912 struct timespec mtimespec;
2913 int info = 0;
2915 if (is_ntfs_stream_smb_fname(smb_dname)) {
2916 DEBUG(2, ("open_directory: %s is a stream name!\n",
2917 smb_fname_str_dbg(smb_dname)));
2918 return NT_STATUS_NOT_A_DIRECTORY;
2921 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2922 /* Ensure we have a directory attribute. */
2923 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2926 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2927 "share_access = 0x%x create_options = 0x%x, "
2928 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2929 smb_fname_str_dbg(smb_dname),
2930 (unsigned int)access_mask,
2931 (unsigned int)share_access,
2932 (unsigned int)create_options,
2933 (unsigned int)create_disposition,
2934 (unsigned int)file_attributes));
2936 status = smbd_calculate_access_mask(conn, smb_dname,
2937 access_mask, &access_mask);
2938 if (!NT_STATUS_IS_OK(status)) {
2939 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2940 "on file %s returned %s\n",
2941 smb_fname_str_dbg(smb_dname),
2942 nt_errstr(status)));
2943 return status;
2946 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2947 !security_token_has_privilege(get_current_nttok(conn),
2948 SEC_PRIV_SECURITY)) {
2949 DEBUG(10, ("open_directory: open on %s "
2950 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2951 smb_fname_str_dbg(smb_dname)));
2952 return NT_STATUS_PRIVILEGE_NOT_HELD;
2955 switch( create_disposition ) {
2956 case FILE_OPEN:
2958 if (!dir_existed) {
2959 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2962 info = FILE_WAS_OPENED;
2963 break;
2965 case FILE_CREATE:
2967 /* If directory exists error. If directory doesn't
2968 * exist create. */
2970 if (dir_existed) {
2971 status = NT_STATUS_OBJECT_NAME_COLLISION;
2972 DEBUG(2, ("open_directory: unable to create "
2973 "%s. Error was %s\n",
2974 smb_fname_str_dbg(smb_dname),
2975 nt_errstr(status)));
2976 return status;
2979 status = mkdir_internal(conn, smb_dname,
2980 file_attributes);
2982 if (!NT_STATUS_IS_OK(status)) {
2983 DEBUG(2, ("open_directory: unable to create "
2984 "%s. Error was %s\n",
2985 smb_fname_str_dbg(smb_dname),
2986 nt_errstr(status)));
2987 return status;
2990 info = FILE_WAS_CREATED;
2991 break;
2993 case FILE_OPEN_IF:
2995 * If directory exists open. If directory doesn't
2996 * exist create.
2999 if (dir_existed) {
3000 status = NT_STATUS_OK;
3001 info = FILE_WAS_OPENED;
3002 } else {
3003 status = mkdir_internal(conn, smb_dname,
3004 file_attributes);
3006 if (NT_STATUS_IS_OK(status)) {
3007 info = FILE_WAS_CREATED;
3008 } else {
3009 /* Cope with create race. */
3010 if (!NT_STATUS_EQUAL(status,
3011 NT_STATUS_OBJECT_NAME_COLLISION)) {
3012 DEBUG(2, ("open_directory: unable to create "
3013 "%s. Error was %s\n",
3014 smb_fname_str_dbg(smb_dname),
3015 nt_errstr(status)));
3016 return status;
3018 info = FILE_WAS_OPENED;
3022 break;
3024 case FILE_SUPERSEDE:
3025 case FILE_OVERWRITE:
3026 case FILE_OVERWRITE_IF:
3027 default:
3028 DEBUG(5,("open_directory: invalid create_disposition "
3029 "0x%x for directory %s\n",
3030 (unsigned int)create_disposition,
3031 smb_fname_str_dbg(smb_dname)));
3032 return NT_STATUS_INVALID_PARAMETER;
3035 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3036 DEBUG(5,("open_directory: %s is not a directory !\n",
3037 smb_fname_str_dbg(smb_dname)));
3038 return NT_STATUS_NOT_A_DIRECTORY;
3041 if (info == FILE_WAS_OPENED) {
3042 status = smbd_check_access_rights(conn, smb_dname, access_mask);
3043 if (!NT_STATUS_IS_OK(status)) {
3044 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3045 "file %s failed with %s\n",
3046 smb_fname_str_dbg(smb_dname),
3047 nt_errstr(status)));
3048 return status;
3052 status = file_new(req, conn, &fsp);
3053 if(!NT_STATUS_IS_OK(status)) {
3054 return status;
3058 * Setup the files_struct for it.
3061 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3062 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3063 fsp->file_pid = req ? req->smbpid : 0;
3064 fsp->can_lock = False;
3065 fsp->can_read = False;
3066 fsp->can_write = False;
3068 fsp->share_access = share_access;
3069 fsp->fh->private_options = 0;
3071 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3073 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3074 fsp->print_file = NULL;
3075 fsp->modified = False;
3076 fsp->oplock_type = NO_OPLOCK;
3077 fsp->sent_oplock_break = NO_BREAK_SENT;
3078 fsp->is_directory = True;
3079 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
3080 status = fsp_set_smb_fname(fsp, smb_dname);
3081 if (!NT_STATUS_IS_OK(status)) {
3082 file_free(req, fsp);
3083 return status;
3086 mtimespec = smb_dname->st.st_ex_mtime;
3088 #ifdef O_DIRECTORY
3089 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3090 #else
3091 /* POSIX allows us to open a directory with O_RDONLY. */
3092 status = fd_open(conn, fsp, O_RDONLY, 0);
3093 #endif
3094 if (!NT_STATUS_IS_OK(status)) {
3095 DEBUG(5, ("open_directory: Could not open fd for "
3096 "%s (%s)\n",
3097 smb_fname_str_dbg(smb_dname),
3098 nt_errstr(status)));
3099 file_free(req, fsp);
3100 return status;
3103 status = vfs_stat_fsp(fsp);
3104 if (!NT_STATUS_IS_OK(status)) {
3105 fd_close(fsp);
3106 file_free(req, fsp);
3107 return status;
3110 /* Ensure there was no race condition. */
3111 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
3112 DEBUG(5,("open_directory: stat struct differs for "
3113 "directory %s.\n",
3114 smb_fname_str_dbg(smb_dname)));
3115 fd_close(fsp);
3116 file_free(req, fsp);
3117 return NT_STATUS_ACCESS_DENIED;
3120 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3121 conn->connectpath, smb_dname,
3122 &mtimespec);
3124 if (lck == NULL) {
3125 DEBUG(0, ("open_directory: Could not get share mode lock for "
3126 "%s\n", smb_fname_str_dbg(smb_dname)));
3127 fd_close(fsp);
3128 file_free(req, fsp);
3129 return NT_STATUS_SHARING_VIOLATION;
3132 status = open_mode_check(conn, lck, fsp->name_hash,
3133 access_mask, share_access,
3134 create_options, &dir_existed);
3136 if (!NT_STATUS_IS_OK(status)) {
3137 TALLOC_FREE(lck);
3138 fd_close(fsp);
3139 file_free(req, fsp);
3140 return status;
3143 set_share_mode(lck, fsp, get_current_uid(conn),
3144 req ? req->mid : 0, NO_OPLOCK);
3146 /* For directories the delete on close bit at open time seems
3147 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3148 if (create_options & FILE_DELETE_ON_CLOSE) {
3149 status = can_set_delete_on_close(fsp, 0);
3150 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3151 TALLOC_FREE(lck);
3152 fd_close(fsp);
3153 file_free(req, fsp);
3154 return status;
3157 if (NT_STATUS_IS_OK(status)) {
3158 /* Note that here we set the *inital* delete on close flag,
3159 not the regular one. The magic gets handled in close. */
3160 fsp->initial_delete_on_close = True;
3164 TALLOC_FREE(lck);
3166 if (pinfo) {
3167 *pinfo = info;
3170 *result = fsp;
3171 return NT_STATUS_OK;
3174 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3175 struct smb_filename *smb_dname)
3177 NTSTATUS status;
3178 files_struct *fsp;
3180 status = SMB_VFS_CREATE_FILE(
3181 conn, /* conn */
3182 req, /* req */
3183 0, /* root_dir_fid */
3184 smb_dname, /* fname */
3185 FILE_READ_ATTRIBUTES, /* access_mask */
3186 FILE_SHARE_NONE, /* share_access */
3187 FILE_CREATE, /* create_disposition*/
3188 FILE_DIRECTORY_FILE, /* create_options */
3189 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3190 0, /* oplock_request */
3191 0, /* allocation_size */
3192 0, /* private_flags */
3193 NULL, /* sd */
3194 NULL, /* ea_list */
3195 &fsp, /* result */
3196 NULL); /* pinfo */
3198 if (NT_STATUS_IS_OK(status)) {
3199 close_file(req, fsp, NORMAL_CLOSE);
3202 return status;
3205 /****************************************************************************
3206 Receive notification that one of our open files has been renamed by another
3207 smbd process.
3208 ****************************************************************************/
3210 void msg_file_was_renamed(struct messaging_context *msg,
3211 void *private_data,
3212 uint32_t msg_type,
3213 struct server_id server_id,
3214 DATA_BLOB *data)
3216 files_struct *fsp;
3217 char *frm = (char *)data->data;
3218 struct file_id id;
3219 const char *sharepath;
3220 const char *base_name;
3221 const char *stream_name;
3222 struct smb_filename *smb_fname = NULL;
3223 size_t sp_len, bn_len;
3224 NTSTATUS status;
3225 struct smbd_server_connection *sconn =
3226 talloc_get_type_abort(private_data,
3227 struct smbd_server_connection);
3229 if (data->data == NULL
3230 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3231 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3232 (int)data->length));
3233 return;
3236 /* Unpack the message. */
3237 pull_file_id_24(frm, &id);
3238 sharepath = &frm[24];
3239 sp_len = strlen(sharepath);
3240 base_name = sharepath + sp_len + 1;
3241 bn_len = strlen(base_name);
3242 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3244 /* stream_name must always be NULL if there is no stream. */
3245 if (stream_name[0] == '\0') {
3246 stream_name = NULL;
3249 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3250 stream_name, NULL, &smb_fname);
3251 if (!NT_STATUS_IS_OK(status)) {
3252 return;
3255 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3256 "file_id %s\n",
3257 sharepath, smb_fname_str_dbg(smb_fname),
3258 file_id_string_tos(&id)));
3260 for(fsp = file_find_di_first(sconn, id); fsp;
3261 fsp = file_find_di_next(fsp)) {
3262 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3264 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3265 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3266 smb_fname_str_dbg(smb_fname)));
3267 status = fsp_set_smb_fname(fsp, smb_fname);
3268 if (!NT_STATUS_IS_OK(status)) {
3269 goto out;
3271 } else {
3272 /* TODO. JRA. */
3273 /* Now we have the complete path we can work out if this is
3274 actually within this share and adjust newname accordingly. */
3275 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3276 "not sharepath %s) "
3277 "%s from %s -> %s\n",
3278 fsp->conn->connectpath,
3279 sharepath,
3280 fsp_fnum_dbg(fsp),
3281 fsp_str_dbg(fsp),
3282 smb_fname_str_dbg(smb_fname)));
3285 out:
3286 TALLOC_FREE(smb_fname);
3287 return;
3291 * If a main file is opened for delete, all streams need to be checked for
3292 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3293 * If that works, delete them all by setting the delete on close and close.
3296 NTSTATUS open_streams_for_delete(connection_struct *conn,
3297 const char *fname)
3299 struct stream_struct *stream_info = NULL;
3300 files_struct **streams = NULL;
3301 int i;
3302 unsigned int num_streams = 0;
3303 TALLOC_CTX *frame = talloc_stackframe();
3304 NTSTATUS status;
3306 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3307 &num_streams, &stream_info);
3309 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3310 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3311 DEBUG(10, ("no streams around\n"));
3312 TALLOC_FREE(frame);
3313 return NT_STATUS_OK;
3316 if (!NT_STATUS_IS_OK(status)) {
3317 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3318 nt_errstr(status)));
3319 goto fail;
3322 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3323 num_streams));
3325 if (num_streams == 0) {
3326 TALLOC_FREE(frame);
3327 return NT_STATUS_OK;
3330 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3331 if (streams == NULL) {
3332 DEBUG(0, ("talloc failed\n"));
3333 status = NT_STATUS_NO_MEMORY;
3334 goto fail;
3337 for (i=0; i<num_streams; i++) {
3338 struct smb_filename *smb_fname = NULL;
3340 if (strequal(stream_info[i].name, "::$DATA")) {
3341 streams[i] = NULL;
3342 continue;
3345 status = create_synthetic_smb_fname(talloc_tos(), fname,
3346 stream_info[i].name,
3347 NULL, &smb_fname);
3348 if (!NT_STATUS_IS_OK(status)) {
3349 goto fail;
3352 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3353 DEBUG(10, ("Unable to stat stream: %s\n",
3354 smb_fname_str_dbg(smb_fname)));
3357 status = SMB_VFS_CREATE_FILE(
3358 conn, /* conn */
3359 NULL, /* req */
3360 0, /* root_dir_fid */
3361 smb_fname, /* fname */
3362 DELETE_ACCESS, /* access_mask */
3363 (FILE_SHARE_READ | /* share_access */
3364 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3365 FILE_OPEN, /* create_disposition*/
3366 0, /* create_options */
3367 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3368 0, /* oplock_request */
3369 0, /* allocation_size */
3370 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3371 NULL, /* sd */
3372 NULL, /* ea_list */
3373 &streams[i], /* result */
3374 NULL); /* pinfo */
3376 if (!NT_STATUS_IS_OK(status)) {
3377 DEBUG(10, ("Could not open stream %s: %s\n",
3378 smb_fname_str_dbg(smb_fname),
3379 nt_errstr(status)));
3381 TALLOC_FREE(smb_fname);
3382 break;
3384 TALLOC_FREE(smb_fname);
3388 * don't touch the variable "status" beyond this point :-)
3391 for (i -= 1 ; i >= 0; i--) {
3392 if (streams[i] == NULL) {
3393 continue;
3396 DEBUG(10, ("Closing stream # %d, %s\n", i,
3397 fsp_str_dbg(streams[i])));
3398 close_file(NULL, streams[i], NORMAL_CLOSE);
3401 fail:
3402 TALLOC_FREE(frame);
3403 return status;
3406 /*********************************************************************
3407 Create a default ACL by inheriting from the parent. If no inheritance
3408 from the parent available, don't set anything. This will leave the actual
3409 permissions the new file or directory already got from the filesystem
3410 as the NT ACL when read.
3411 *********************************************************************/
3413 static NTSTATUS inherit_new_acl(files_struct *fsp)
3415 TALLOC_CTX *ctx = talloc_tos();
3416 char *parent_name = NULL;
3417 struct security_descriptor *parent_desc = NULL;
3418 NTSTATUS status = NT_STATUS_OK;
3419 struct security_descriptor *psd = NULL;
3420 struct dom_sid *owner_sid = NULL;
3421 struct dom_sid *group_sid = NULL;
3422 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3423 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3424 bool inheritable_components = false;
3425 size_t size = 0;
3427 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3428 return NT_STATUS_NO_MEMORY;
3431 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3432 parent_name,
3433 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3434 &parent_desc);
3435 if (!NT_STATUS_IS_OK(status)) {
3436 return status;
3439 inheritable_components = sd_has_inheritable_components(parent_desc,
3440 fsp->is_directory);
3442 if (!inheritable_components && !inherit_owner) {
3443 /* Nothing to inherit and not setting owner. */
3444 return NT_STATUS_OK;
3447 /* Create an inherited descriptor from the parent. */
3449 if (DEBUGLEVEL >= 10) {
3450 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3451 fsp_str_dbg(fsp) ));
3452 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3455 /* Inherit from parent descriptor if "inherit owner" set. */
3456 if (inherit_owner) {
3457 owner_sid = parent_desc->owner_sid;
3458 group_sid = parent_desc->group_sid;
3461 if (owner_sid == NULL) {
3462 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3464 if (group_sid == NULL) {
3465 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3468 status = se_create_child_secdesc(ctx,
3469 &psd,
3470 &size,
3471 parent_desc,
3472 owner_sid,
3473 group_sid,
3474 fsp->is_directory);
3475 if (!NT_STATUS_IS_OK(status)) {
3476 return status;
3479 /* If inheritable_components == false,
3480 se_create_child_secdesc()
3481 creates a security desriptor with a NULL dacl
3482 entry, but with SEC_DESC_DACL_PRESENT. We need
3483 to remove that flag. */
3485 if (!inheritable_components) {
3486 security_info_sent &= ~SECINFO_DACL;
3487 psd->type &= ~SEC_DESC_DACL_PRESENT;
3490 if (DEBUGLEVEL >= 10) {
3491 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3492 fsp_str_dbg(fsp) ));
3493 NDR_PRINT_DEBUG(security_descriptor, psd);
3496 if (inherit_owner) {
3497 /* We need to be root to force this. */
3498 become_root();
3500 status = SMB_VFS_FSET_NT_ACL(fsp,
3501 security_info_sent,
3502 psd);
3503 if (inherit_owner) {
3504 unbecome_root();
3506 return status;
3510 * Wrapper around open_file_ntcreate and open_directory
3513 static NTSTATUS create_file_unixpath(connection_struct *conn,
3514 struct smb_request *req,
3515 struct smb_filename *smb_fname,
3516 uint32_t access_mask,
3517 uint32_t share_access,
3518 uint32_t create_disposition,
3519 uint32_t create_options,
3520 uint32_t file_attributes,
3521 uint32_t oplock_request,
3522 uint64_t allocation_size,
3523 uint32_t private_flags,
3524 struct security_descriptor *sd,
3525 struct ea_list *ea_list,
3527 files_struct **result,
3528 int *pinfo)
3530 int info = FILE_WAS_OPENED;
3531 files_struct *base_fsp = NULL;
3532 files_struct *fsp = NULL;
3533 NTSTATUS status;
3535 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3536 "file_attributes = 0x%x, share_access = 0x%x, "
3537 "create_disposition = 0x%x create_options = 0x%x "
3538 "oplock_request = 0x%x private_flags = 0x%x "
3539 "ea_list = 0x%p, sd = 0x%p, "
3540 "fname = %s\n",
3541 (unsigned int)access_mask,
3542 (unsigned int)file_attributes,
3543 (unsigned int)share_access,
3544 (unsigned int)create_disposition,
3545 (unsigned int)create_options,
3546 (unsigned int)oplock_request,
3547 (unsigned int)private_flags,
3548 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3550 if (create_options & FILE_OPEN_BY_FILE_ID) {
3551 status = NT_STATUS_NOT_SUPPORTED;
3552 goto fail;
3555 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3556 status = NT_STATUS_INVALID_PARAMETER;
3557 goto fail;
3560 if (req == NULL) {
3561 oplock_request |= INTERNAL_OPEN_ONLY;
3564 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3565 && (access_mask & DELETE_ACCESS)
3566 && !is_ntfs_stream_smb_fname(smb_fname)) {
3568 * We can't open a file with DELETE access if any of the
3569 * streams is open without FILE_SHARE_DELETE
3571 status = open_streams_for_delete(conn, smb_fname->base_name);
3573 if (!NT_STATUS_IS_OK(status)) {
3574 goto fail;
3578 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3579 !security_token_has_privilege(get_current_nttok(conn),
3580 SEC_PRIV_SECURITY)) {
3581 DEBUG(10, ("create_file_unixpath: open on %s "
3582 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3583 smb_fname_str_dbg(smb_fname)));
3584 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3585 goto fail;
3588 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3589 && is_ntfs_stream_smb_fname(smb_fname)
3590 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3591 uint32 base_create_disposition;
3592 struct smb_filename *smb_fname_base = NULL;
3594 if (create_options & FILE_DIRECTORY_FILE) {
3595 status = NT_STATUS_NOT_A_DIRECTORY;
3596 goto fail;
3599 switch (create_disposition) {
3600 case FILE_OPEN:
3601 base_create_disposition = FILE_OPEN;
3602 break;
3603 default:
3604 base_create_disposition = FILE_OPEN_IF;
3605 break;
3608 /* Create an smb_filename with stream_name == NULL. */
3609 status = create_synthetic_smb_fname(talloc_tos(),
3610 smb_fname->base_name,
3611 NULL, NULL,
3612 &smb_fname_base);
3613 if (!NT_STATUS_IS_OK(status)) {
3614 goto fail;
3617 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3618 DEBUG(10, ("Unable to stat stream: %s\n",
3619 smb_fname_str_dbg(smb_fname_base)));
3622 /* Open the base file. */
3623 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3624 FILE_SHARE_READ
3625 | FILE_SHARE_WRITE
3626 | FILE_SHARE_DELETE,
3627 base_create_disposition,
3628 0, 0, 0, 0, 0, NULL, NULL,
3629 &base_fsp, NULL);
3630 TALLOC_FREE(smb_fname_base);
3632 if (!NT_STATUS_IS_OK(status)) {
3633 DEBUG(10, ("create_file_unixpath for base %s failed: "
3634 "%s\n", smb_fname->base_name,
3635 nt_errstr(status)));
3636 goto fail;
3638 /* we don't need to low level fd */
3639 fd_close(base_fsp);
3643 * If it's a request for a directory open, deal with it separately.
3646 if (create_options & FILE_DIRECTORY_FILE) {
3648 if (create_options & FILE_NON_DIRECTORY_FILE) {
3649 status = NT_STATUS_INVALID_PARAMETER;
3650 goto fail;
3653 /* Can't open a temp directory. IFS kit test. */
3654 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3655 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3656 status = NT_STATUS_INVALID_PARAMETER;
3657 goto fail;
3661 * We will get a create directory here if the Win32
3662 * app specified a security descriptor in the
3663 * CreateDirectory() call.
3666 oplock_request = 0;
3667 status = open_directory(
3668 conn, req, smb_fname, access_mask, share_access,
3669 create_disposition, create_options, file_attributes,
3670 &info, &fsp);
3671 } else {
3674 * Ordinary file case.
3677 status = file_new(req, conn, &fsp);
3678 if(!NT_STATUS_IS_OK(status)) {
3679 goto fail;
3682 status = fsp_set_smb_fname(fsp, smb_fname);
3683 if (!NT_STATUS_IS_OK(status)) {
3684 goto fail;
3688 * We're opening the stream element of a base_fsp
3689 * we already opened. Set up the base_fsp pointer.
3691 if (base_fsp) {
3692 fsp->base_fsp = base_fsp;
3695 if (allocation_size) {
3696 fsp->initial_allocation_size = smb_roundup(fsp->conn,
3697 allocation_size);
3700 status = open_file_ntcreate(conn,
3701 req,
3702 access_mask,
3703 share_access,
3704 create_disposition,
3705 create_options,
3706 file_attributes,
3707 oplock_request,
3708 private_flags,
3709 &info,
3710 fsp);
3712 if(!NT_STATUS_IS_OK(status)) {
3713 file_free(req, fsp);
3714 fsp = NULL;
3717 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3719 /* A stream open never opens a directory */
3721 if (base_fsp) {
3722 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3723 goto fail;
3727 * Fail the open if it was explicitly a non-directory
3728 * file.
3731 if (create_options & FILE_NON_DIRECTORY_FILE) {
3732 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3733 goto fail;
3736 oplock_request = 0;
3737 status = open_directory(
3738 conn, req, smb_fname, access_mask,
3739 share_access, create_disposition,
3740 create_options, file_attributes,
3741 &info, &fsp);
3745 if (!NT_STATUS_IS_OK(status)) {
3746 goto fail;
3749 fsp->base_fsp = base_fsp;
3751 if ((ea_list != NULL) &&
3752 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3753 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3754 if (!NT_STATUS_IS_OK(status)) {
3755 goto fail;
3759 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3760 status = NT_STATUS_ACCESS_DENIED;
3761 goto fail;
3764 /* Save the requested allocation size. */
3765 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3766 if (allocation_size
3767 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3768 fsp->initial_allocation_size = smb_roundup(
3769 fsp->conn, allocation_size);
3770 if (fsp->is_directory) {
3771 /* Can't set allocation size on a directory. */
3772 status = NT_STATUS_ACCESS_DENIED;
3773 goto fail;
3775 if (vfs_allocate_file_space(
3776 fsp, fsp->initial_allocation_size) == -1) {
3777 status = NT_STATUS_DISK_FULL;
3778 goto fail;
3780 } else {
3781 fsp->initial_allocation_size = smb_roundup(
3782 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3784 } else {
3785 fsp->initial_allocation_size = 0;
3788 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3789 fsp->base_fsp == NULL) {
3790 if (sd != NULL) {
3792 * According to the MS documentation, the only time the security
3793 * descriptor is applied to the opened file is iff we *created* the
3794 * file; an existing file stays the same.
3796 * Also, it seems (from observation) that you can open the file with
3797 * any access mask but you can still write the sd. We need to override
3798 * the granted access before we call set_sd
3799 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3802 uint32_t sec_info_sent;
3803 uint32_t saved_access_mask = fsp->access_mask;
3805 sec_info_sent = get_sec_info(sd);
3807 fsp->access_mask = FILE_GENERIC_ALL;
3809 if (sec_info_sent & (SECINFO_OWNER|
3810 SECINFO_GROUP|
3811 SECINFO_DACL|
3812 SECINFO_SACL)) {
3813 status = set_sd(fsp, sd, sec_info_sent);
3816 fsp->access_mask = saved_access_mask;
3818 if (!NT_STATUS_IS_OK(status)) {
3819 goto fail;
3821 } else if (lp_inherit_acls(SNUM(conn))) {
3822 /* Inherit from parent. Errors here are not fatal. */
3823 status = inherit_new_acl(fsp);
3824 if (!NT_STATUS_IS_OK(status)) {
3825 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3826 fsp_str_dbg(fsp),
3827 nt_errstr(status) ));
3832 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3834 *result = fsp;
3835 if (pinfo != NULL) {
3836 *pinfo = info;
3839 smb_fname->st = fsp->fsp_name->st;
3841 return NT_STATUS_OK;
3843 fail:
3844 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3846 if (fsp != NULL) {
3847 if (base_fsp && fsp->base_fsp == base_fsp) {
3849 * The close_file below will close
3850 * fsp->base_fsp.
3852 base_fsp = NULL;
3854 close_file(req, fsp, ERROR_CLOSE);
3855 fsp = NULL;
3857 if (base_fsp != NULL) {
3858 close_file(req, base_fsp, ERROR_CLOSE);
3859 base_fsp = NULL;
3861 return status;
3865 * Calculate the full path name given a relative fid.
3867 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3868 struct smb_request *req,
3869 uint16_t root_dir_fid,
3870 const struct smb_filename *smb_fname,
3871 struct smb_filename **smb_fname_out)
3873 files_struct *dir_fsp;
3874 char *parent_fname = NULL;
3875 char *new_base_name = NULL;
3876 NTSTATUS status;
3878 if (root_dir_fid == 0 || !smb_fname) {
3879 status = NT_STATUS_INTERNAL_ERROR;
3880 goto out;
3883 dir_fsp = file_fsp(req, root_dir_fid);
3885 if (dir_fsp == NULL) {
3886 status = NT_STATUS_INVALID_HANDLE;
3887 goto out;
3890 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3891 status = NT_STATUS_INVALID_HANDLE;
3892 goto out;
3895 if (!dir_fsp->is_directory) {
3898 * Check to see if this is a mac fork of some kind.
3901 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3902 is_ntfs_stream_smb_fname(smb_fname)) {
3903 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3904 goto out;
3908 we need to handle the case when we get a
3909 relative open relative to a file and the
3910 pathname is blank - this is a reopen!
3911 (hint from demyn plantenberg)
3914 status = NT_STATUS_INVALID_HANDLE;
3915 goto out;
3918 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3920 * We're at the toplevel dir, the final file name
3921 * must not contain ./, as this is filtered out
3922 * normally by srvstr_get_path and unix_convert
3923 * explicitly rejects paths containing ./.
3925 parent_fname = talloc_strdup(talloc_tos(), "");
3926 if (parent_fname == NULL) {
3927 status = NT_STATUS_NO_MEMORY;
3928 goto out;
3930 } else {
3931 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3934 * Copy in the base directory name.
3937 parent_fname = talloc_array(talloc_tos(), char,
3938 dir_name_len+2);
3939 if (parent_fname == NULL) {
3940 status = NT_STATUS_NO_MEMORY;
3941 goto out;
3943 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3944 dir_name_len+1);
3947 * Ensure it ends in a '/'.
3948 * We used TALLOC_SIZE +2 to add space for the '/'.
3951 if(dir_name_len
3952 && (parent_fname[dir_name_len-1] != '\\')
3953 && (parent_fname[dir_name_len-1] != '/')) {
3954 parent_fname[dir_name_len] = '/';
3955 parent_fname[dir_name_len+1] = '\0';
3959 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3960 smb_fname->base_name);
3961 if (new_base_name == NULL) {
3962 status = NT_STATUS_NO_MEMORY;
3963 goto out;
3966 status = filename_convert(req,
3967 conn,
3968 req->flags2 & FLAGS2_DFS_PATHNAMES,
3969 new_base_name,
3971 NULL,
3972 smb_fname_out);
3973 if (!NT_STATUS_IS_OK(status)) {
3974 goto out;
3977 out:
3978 TALLOC_FREE(parent_fname);
3979 TALLOC_FREE(new_base_name);
3980 return status;
3983 NTSTATUS create_file_default(connection_struct *conn,
3984 struct smb_request *req,
3985 uint16_t root_dir_fid,
3986 struct smb_filename *smb_fname,
3987 uint32_t access_mask,
3988 uint32_t share_access,
3989 uint32_t create_disposition,
3990 uint32_t create_options,
3991 uint32_t file_attributes,
3992 uint32_t oplock_request,
3993 uint64_t allocation_size,
3994 uint32_t private_flags,
3995 struct security_descriptor *sd,
3996 struct ea_list *ea_list,
3997 files_struct **result,
3998 int *pinfo)
4000 int info = FILE_WAS_OPENED;
4001 files_struct *fsp = NULL;
4002 NTSTATUS status;
4003 bool stream_name = false;
4005 DEBUG(10,("create_file: access_mask = 0x%x "
4006 "file_attributes = 0x%x, share_access = 0x%x, "
4007 "create_disposition = 0x%x create_options = 0x%x "
4008 "oplock_request = 0x%x "
4009 "private_flags = 0x%x "
4010 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4011 "fname = %s\n",
4012 (unsigned int)access_mask,
4013 (unsigned int)file_attributes,
4014 (unsigned int)share_access,
4015 (unsigned int)create_disposition,
4016 (unsigned int)create_options,
4017 (unsigned int)oplock_request,
4018 (unsigned int)private_flags,
4019 (unsigned int)root_dir_fid,
4020 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4023 * Calculate the filename from the root_dir_if if necessary.
4026 if (root_dir_fid != 0) {
4027 struct smb_filename *smb_fname_out = NULL;
4028 status = get_relative_fid_filename(conn, req, root_dir_fid,
4029 smb_fname, &smb_fname_out);
4030 if (!NT_STATUS_IS_OK(status)) {
4031 goto fail;
4033 smb_fname = smb_fname_out;
4037 * Check to see if this is a mac fork of some kind.
4040 stream_name = is_ntfs_stream_smb_fname(smb_fname);
4041 if (stream_name) {
4042 enum FAKE_FILE_TYPE fake_file_type;
4044 fake_file_type = is_fake_file(smb_fname);
4046 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
4049 * Here we go! support for changing the disk quotas
4050 * --metze
4052 * We need to fake up to open this MAGIC QUOTA file
4053 * and return a valid FID.
4055 * w2k close this file directly after openening xp
4056 * also tries a QUERY_FILE_INFO on the file and then
4057 * close it
4059 status = open_fake_file(req, conn, req->vuid,
4060 fake_file_type, smb_fname,
4061 access_mask, &fsp);
4062 if (!NT_STATUS_IS_OK(status)) {
4063 goto fail;
4066 ZERO_STRUCT(smb_fname->st);
4067 goto done;
4070 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
4071 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4072 goto fail;
4076 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
4077 int ret;
4078 smb_fname->stream_name = NULL;
4079 /* We have to handle this error here. */
4080 if (create_options & FILE_DIRECTORY_FILE) {
4081 status = NT_STATUS_NOT_A_DIRECTORY;
4082 goto fail;
4084 if (lp_posix_pathnames()) {
4085 ret = SMB_VFS_LSTAT(conn, smb_fname);
4086 } else {
4087 ret = SMB_VFS_STAT(conn, smb_fname);
4090 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
4091 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4092 goto fail;
4096 status = create_file_unixpath(
4097 conn, req, smb_fname, access_mask, share_access,
4098 create_disposition, create_options, file_attributes,
4099 oplock_request, allocation_size, private_flags,
4100 sd, ea_list,
4101 &fsp, &info);
4103 if (!NT_STATUS_IS_OK(status)) {
4104 goto fail;
4107 done:
4108 DEBUG(10, ("create_file: info=%d\n", info));
4110 *result = fsp;
4111 if (pinfo != NULL) {
4112 *pinfo = info;
4114 return NT_STATUS_OK;
4116 fail:
4117 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
4119 if (fsp != NULL) {
4120 close_file(req, fsp, ERROR_CLOSE);
4121 fsp = NULL;
4123 return status;