libcli/smb: fix padding in smb2_create_blob*
[Samba/gebeck_regimport.git] / source3 / smbd / open.c
blobb67c045e340a674ac568ee969429c56538087f5f
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 "serverid.h"
33 #include "messages.h"
35 extern const struct generic_mapping file_generic_mapping;
37 struct deferred_open_record {
38 bool delayed_for_oplocks;
39 bool async_open;
40 struct file_id id;
43 /****************************************************************************
44 If the requester wanted DELETE_ACCESS and was rejected because
45 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
46 overrides this.
47 ****************************************************************************/
49 static bool parent_override_delete(connection_struct *conn,
50 const struct smb_filename *smb_fname,
51 uint32_t access_mask,
52 uint32_t rejected_mask)
54 if ((access_mask & DELETE_ACCESS) &&
55 (rejected_mask & DELETE_ACCESS) &&
56 can_delete_file_in_directory(conn, smb_fname)) {
57 return true;
59 return false;
62 /****************************************************************************
63 Check if we have open rights.
64 ****************************************************************************/
66 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
67 const struct smb_filename *smb_fname,
68 bool use_privs,
69 uint32_t access_mask)
71 /* Check if we have rights to open. */
72 NTSTATUS status;
73 struct security_descriptor *sd = NULL;
74 uint32_t rejected_share_access;
75 uint32_t rejected_mask = access_mask;
77 rejected_share_access = access_mask & ~(conn->share_access);
79 if (rejected_share_access) {
80 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
81 "on %s (0x%x)\n",
82 (unsigned int)access_mask,
83 smb_fname_str_dbg(smb_fname),
84 (unsigned int)rejected_share_access ));
85 return NT_STATUS_ACCESS_DENIED;
88 if (!use_privs && get_current_uid(conn) == (uid_t)0) {
89 /* I'm sorry sir, I didn't know you were root... */
90 DEBUG(10,("smbd_check_access_rights: root override "
91 "on %s. Granting 0x%x\n",
92 smb_fname_str_dbg(smb_fname),
93 (unsigned int)access_mask ));
94 return NT_STATUS_OK;
97 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
98 DEBUG(10,("smbd_check_access_rights: not checking ACL "
99 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
100 smb_fname_str_dbg(smb_fname),
101 (unsigned int)access_mask ));
102 return NT_STATUS_OK;
105 if (access_mask == DELETE_ACCESS &&
106 VALID_STAT(smb_fname->st) &&
107 S_ISLNK(smb_fname->st.st_ex_mode)) {
108 /* We can always delete a symlink. */
109 DEBUG(10,("smbd_check_access_rights: not checking ACL "
110 "on DELETE_ACCESS on symlink %s.\n",
111 smb_fname_str_dbg(smb_fname) ));
112 return NT_STATUS_OK;
115 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
116 (SECINFO_OWNER |
117 SECINFO_GROUP |
118 SECINFO_DACL),&sd);
120 if (!NT_STATUS_IS_OK(status)) {
121 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
122 "on %s: %s\n",
123 smb_fname_str_dbg(smb_fname),
124 nt_errstr(status)));
126 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
127 goto access_denied;
130 return status;
134 * Never test FILE_READ_ATTRIBUTES. se_file_access_check() also takes care of
135 * owner WRITE_DAC and READ_CONTROL.
137 status = se_file_access_check(sd,
138 get_current_nttok(conn),
139 use_privs,
140 (access_mask & ~FILE_READ_ATTRIBUTES),
141 &rejected_mask);
143 DEBUG(10,("smbd_check_access_rights: file %s requesting "
144 "0x%x returning 0x%x (%s)\n",
145 smb_fname_str_dbg(smb_fname),
146 (unsigned int)access_mask,
147 (unsigned int)rejected_mask,
148 nt_errstr(status) ));
150 if (!NT_STATUS_IS_OK(status)) {
151 if (DEBUGLEVEL >= 10) {
152 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
153 smb_fname_str_dbg(smb_fname) ));
154 NDR_PRINT_DEBUG(security_descriptor, sd);
158 TALLOC_FREE(sd);
160 if (NT_STATUS_IS_OK(status) ||
161 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
162 return status;
165 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
167 access_denied:
169 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
170 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
171 !lp_store_dos_attributes(SNUM(conn)) &&
172 (lp_map_readonly(SNUM(conn)) ||
173 lp_map_archive(SNUM(conn)) ||
174 lp_map_hidden(SNUM(conn)) ||
175 lp_map_system(SNUM(conn)))) {
176 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
178 DEBUG(10,("smbd_check_access_rights: "
179 "overrode "
180 "FILE_WRITE_ATTRIBUTES "
181 "on file %s\n",
182 smb_fname_str_dbg(smb_fname)));
185 if (parent_override_delete(conn,
186 smb_fname,
187 access_mask,
188 rejected_mask)) {
189 /* Were we trying to do an open
190 * for delete and didn't get DELETE
191 * access (only) ? Check if the
192 * directory allows DELETE_CHILD.
193 * See here:
194 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
195 * for details. */
197 rejected_mask &= ~DELETE_ACCESS;
199 DEBUG(10,("smbd_check_access_rights: "
200 "overrode "
201 "DELETE_ACCESS on "
202 "file %s\n",
203 smb_fname_str_dbg(smb_fname)));
206 if (rejected_mask != 0) {
207 return NT_STATUS_ACCESS_DENIED;
209 return NT_STATUS_OK;
212 static NTSTATUS check_parent_access(struct connection_struct *conn,
213 struct smb_filename *smb_fname,
214 uint32_t access_mask)
216 NTSTATUS status;
217 char *parent_dir = NULL;
218 struct security_descriptor *parent_sd = NULL;
219 uint32_t access_granted = 0;
221 if (!parent_dirname(talloc_tos(),
222 smb_fname->base_name,
223 &parent_dir,
224 NULL)) {
225 return NT_STATUS_NO_MEMORY;
228 if (get_current_uid(conn) == (uid_t)0) {
229 /* I'm sorry sir, I didn't know you were root... */
230 DEBUG(10,("check_parent_access: root override "
231 "on %s. Granting 0x%x\n",
232 smb_fname_str_dbg(smb_fname),
233 (unsigned int)access_mask ));
234 return NT_STATUS_OK;
237 status = SMB_VFS_GET_NT_ACL(conn,
238 parent_dir,
239 SECINFO_DACL,
240 &parent_sd);
242 if (!NT_STATUS_IS_OK(status)) {
243 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
244 "%s with error %s\n",
245 parent_dir,
246 nt_errstr(status)));
247 return status;
251 * Never test FILE_READ_ATTRIBUTES. se_file_access_check() also takes care of
252 * owner WRITE_DAC and READ_CONTROL.
254 status = se_file_access_check(parent_sd,
255 get_current_nttok(conn),
256 false,
257 (access_mask & ~FILE_READ_ATTRIBUTES),
258 &access_granted);
259 if(!NT_STATUS_IS_OK(status)) {
260 DEBUG(5,("check_parent_access: access check "
261 "on directory %s for "
262 "path %s for mask 0x%x returned (0x%x) %s\n",
263 parent_dir,
264 smb_fname->base_name,
265 access_mask,
266 access_granted,
267 nt_errstr(status) ));
268 return status;
271 return NT_STATUS_OK;
274 /****************************************************************************
275 fd support routines - attempt to do a dos_open.
276 ****************************************************************************/
278 NTSTATUS fd_open(struct connection_struct *conn,
279 files_struct *fsp,
280 int flags,
281 mode_t mode)
283 struct smb_filename *smb_fname = fsp->fsp_name;
284 NTSTATUS status = NT_STATUS_OK;
286 #ifdef O_NOFOLLOW
288 * Never follow symlinks on a POSIX client. The
289 * client should be doing this.
292 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
293 flags |= O_NOFOLLOW;
295 #endif
297 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
298 if (fsp->fh->fd == -1) {
299 int posix_errno = errno;
300 #ifdef O_NOFOLLOW
301 #if defined(ENOTSUP) && defined(OSF1)
302 /* handle special Tru64 errno */
303 if (errno == ENOTSUP) {
304 posix_errno = ELOOP;
306 #endif /* ENOTSUP */
307 #ifdef EFTYPE
308 /* fix broken NetBSD errno */
309 if (errno == EFTYPE) {
310 posix_errno = ELOOP;
312 #endif /* EFTYPE */
313 /* fix broken FreeBSD errno */
314 if (errno == EMLINK) {
315 posix_errno = ELOOP;
317 #endif /* O_NOFOLLOW */
318 status = map_nt_error_from_unix(posix_errno);
319 if (errno == EMFILE) {
320 static time_t last_warned = 0L;
322 if (time((time_t *) NULL) > last_warned) {
323 DEBUG(0,("Too many open files, unable "
324 "to open more! smbd's max "
325 "open files = %d\n",
326 lp_max_open_files()));
327 last_warned = time((time_t *) NULL);
333 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
334 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
335 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
337 return status;
340 /****************************************************************************
341 Close the file associated with a fsp.
342 ****************************************************************************/
344 NTSTATUS fd_close(files_struct *fsp)
346 int ret;
348 if (fsp->dptr) {
349 dptr_CloseDir(fsp);
351 if (fsp->fh->fd == -1) {
352 return NT_STATUS_OK; /* What we used to call a stat open. */
354 if (fsp->fh->ref_count > 1) {
355 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
358 ret = SMB_VFS_CLOSE(fsp);
359 fsp->fh->fd = -1;
360 if (ret == -1) {
361 return map_nt_error_from_unix(errno);
363 return NT_STATUS_OK;
366 /****************************************************************************
367 Change the ownership of a file to that of the parent directory.
368 Do this by fd if possible.
369 ****************************************************************************/
371 void change_file_owner_to_parent(connection_struct *conn,
372 const char *inherit_from_dir,
373 files_struct *fsp)
375 struct smb_filename *smb_fname_parent = NULL;
376 NTSTATUS status;
377 int ret;
379 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
380 NULL, NULL, &smb_fname_parent);
381 if (!NT_STATUS_IS_OK(status)) {
382 return;
385 ret = SMB_VFS_STAT(conn, smb_fname_parent);
386 if (ret == -1) {
387 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
388 "directory %s. Error was %s\n",
389 smb_fname_str_dbg(smb_fname_parent),
390 strerror(errno)));
391 TALLOC_FREE(smb_fname_parent);
392 return;
395 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
396 /* Already this uid - no need to change. */
397 DEBUG(10,("change_file_owner_to_parent: file %s "
398 "is already owned by uid %d\n",
399 fsp_str_dbg(fsp),
400 (int)fsp->fsp_name->st.st_ex_uid ));
401 TALLOC_FREE(smb_fname_parent);
402 return;
405 become_root();
406 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
407 unbecome_root();
408 if (ret == -1) {
409 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
410 "file %s to parent directory uid %u. Error "
411 "was %s\n", fsp_str_dbg(fsp),
412 (unsigned int)smb_fname_parent->st.st_ex_uid,
413 strerror(errno) ));
414 } else {
415 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
416 "parent directory uid %u.\n", fsp_str_dbg(fsp),
417 (unsigned int)smb_fname_parent->st.st_ex_uid));
418 /* Ensure the uid entry is updated. */
419 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
422 TALLOC_FREE(smb_fname_parent);
425 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
426 const char *inherit_from_dir,
427 const char *fname,
428 SMB_STRUCT_STAT *psbuf)
430 struct smb_filename *smb_fname_parent = NULL;
431 struct smb_filename *smb_fname_cwd = NULL;
432 char *saved_dir = NULL;
433 TALLOC_CTX *ctx = talloc_tos();
434 NTSTATUS status = NT_STATUS_OK;
435 int ret;
437 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
438 &smb_fname_parent);
439 if (!NT_STATUS_IS_OK(status)) {
440 return status;
443 ret = SMB_VFS_STAT(conn, smb_fname_parent);
444 if (ret == -1) {
445 status = map_nt_error_from_unix(errno);
446 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
447 "directory %s. Error was %s\n",
448 smb_fname_str_dbg(smb_fname_parent),
449 strerror(errno)));
450 goto out;
453 /* We've already done an lstat into psbuf, and we know it's a
454 directory. If we can cd into the directory and the dev/ino
455 are the same then we can safely chown without races as
456 we're locking the directory in place by being in it. This
457 should work on any UNIX (thanks tridge :-). JRA.
460 saved_dir = vfs_GetWd(ctx,conn);
461 if (!saved_dir) {
462 status = map_nt_error_from_unix(errno);
463 DEBUG(0,("change_dir_owner_to_parent: failed to get "
464 "current working directory. Error was %s\n",
465 strerror(errno)));
466 goto out;
469 /* Chdir into the new path. */
470 if (vfs_ChDir(conn, fname) == -1) {
471 status = map_nt_error_from_unix(errno);
472 DEBUG(0,("change_dir_owner_to_parent: failed to change "
473 "current working directory to %s. Error "
474 "was %s\n", fname, strerror(errno) ));
475 goto chdir;
478 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
479 &smb_fname_cwd);
480 if (!NT_STATUS_IS_OK(status)) {
481 return status;
484 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
485 if (ret == -1) {
486 status = map_nt_error_from_unix(errno);
487 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
488 "directory '.' (%s) Error was %s\n",
489 fname, strerror(errno)));
490 goto chdir;
493 /* Ensure we're pointing at the same place. */
494 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
495 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
496 DEBUG(0,("change_dir_owner_to_parent: "
497 "device/inode on directory %s changed. "
498 "Refusing to chown !\n", fname ));
499 status = NT_STATUS_ACCESS_DENIED;
500 goto chdir;
503 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
504 /* Already this uid - no need to change. */
505 DEBUG(10,("change_dir_owner_to_parent: directory %s "
506 "is already owned by uid %d\n",
507 fname,
508 (int)smb_fname_cwd->st.st_ex_uid ));
509 status = NT_STATUS_OK;
510 goto chdir;
513 become_root();
514 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
515 (gid_t)-1);
516 unbecome_root();
517 if (ret == -1) {
518 status = map_nt_error_from_unix(errno);
519 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
520 "directory %s to parent directory uid %u. "
521 "Error was %s\n", fname,
522 (unsigned int)smb_fname_parent->st.st_ex_uid,
523 strerror(errno) ));
524 } else {
525 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
526 "directory %s to parent directory uid %u.\n",
527 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
528 /* Ensure the uid entry is updated. */
529 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
532 chdir:
533 vfs_ChDir(conn,saved_dir);
534 out:
535 TALLOC_FREE(smb_fname_parent);
536 TALLOC_FREE(smb_fname_cwd);
537 return status;
540 /****************************************************************************
541 Open a file - returning a guaranteed ATOMIC indication of if the
542 file was created or not.
543 ****************************************************************************/
545 static NTSTATUS fd_open_atomic(struct connection_struct *conn,
546 files_struct *fsp,
547 int flags,
548 mode_t mode,
549 bool *file_created)
551 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
552 bool file_existed = VALID_STAT(fsp->fsp_name->st);
554 *file_created = false;
556 if (!(flags & O_CREAT)) {
558 * We're not creating the file, just pass through.
560 return fd_open(conn, fsp, flags, mode);
563 if (flags & O_EXCL) {
565 * Fail if already exists, just pass through.
567 status = fd_open(conn, fsp, flags, mode);
570 * Here we've opened with O_CREAT|O_EXCL. If that went
571 * NT_STATUS_OK, we *know* we created this file.
573 *file_created = NT_STATUS_IS_OK(status);
575 return status;
579 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
580 * To know absolutely if we created the file or not,
581 * we can never call O_CREAT without O_EXCL. So if
582 * we think the file existed, try without O_CREAT|O_EXCL.
583 * If we think the file didn't exist, try with
584 * O_CREAT|O_EXCL. Keep bouncing between these two
585 * requests until either the file is created, or
586 * opened. Either way, we keep going until we get
587 * a returnable result (error, or open/create).
590 while(1) {
591 int curr_flags = flags;
593 if (file_existed) {
594 /* Just try open, do not create. */
595 curr_flags &= ~(O_CREAT);
596 status = fd_open(conn, fsp, curr_flags, mode);
597 if (NT_STATUS_EQUAL(status,
598 NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
600 * Someone deleted it in the meantime.
601 * Retry with O_EXCL.
603 file_existed = false;
604 DEBUG(10,("fd_open_atomic: file %s existed. "
605 "Retry.\n",
606 smb_fname_str_dbg(fsp->fsp_name)));
607 continue;
609 } else {
610 /* Try create exclusively, fail if it exists. */
611 curr_flags |= O_EXCL;
612 status = fd_open(conn, fsp, curr_flags, mode);
613 if (NT_STATUS_EQUAL(status,
614 NT_STATUS_OBJECT_NAME_COLLISION)) {
616 * Someone created it in the meantime.
617 * Retry without O_CREAT.
619 file_existed = true;
620 DEBUG(10,("fd_open_atomic: file %s "
621 "did not exist. Retry.\n",
622 smb_fname_str_dbg(fsp->fsp_name)));
623 continue;
625 if (NT_STATUS_IS_OK(status)) {
627 * Here we've opened with O_CREAT|O_EXCL
628 * and got success. We *know* we created
629 * this file.
631 *file_created = true;
634 /* Create is done, or failed. */
635 break;
637 return status;
640 /****************************************************************************
641 Open a file.
642 ****************************************************************************/
644 static NTSTATUS open_file(files_struct *fsp,
645 connection_struct *conn,
646 struct smb_request *req,
647 const char *parent_dir,
648 int flags,
649 mode_t unx_mode,
650 uint32 access_mask, /* client requested access mask. */
651 uint32 open_access_mask, /* what we're actually using in the open. */
652 bool *p_file_created)
654 struct smb_filename *smb_fname = fsp->fsp_name;
655 NTSTATUS status = NT_STATUS_OK;
656 int accmode = (flags & O_ACCMODE);
657 int local_flags = flags;
658 bool file_existed = VALID_STAT(fsp->fsp_name->st);
660 fsp->fh->fd = -1;
661 errno = EPERM;
663 /* Check permissions */
666 * This code was changed after seeing a client open request
667 * containing the open mode of (DENY_WRITE/read-only) with
668 * the 'create if not exist' bit set. The previous code
669 * would fail to open the file read only on a read-only share
670 * as it was checking the flags parameter directly against O_RDONLY,
671 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
672 * JRA.
675 if (!CAN_WRITE(conn)) {
676 /* It's a read-only share - fail if we wanted to write. */
677 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
678 DEBUG(3,("Permission denied opening %s\n",
679 smb_fname_str_dbg(smb_fname)));
680 return NT_STATUS_ACCESS_DENIED;
681 } else if(flags & O_CREAT) {
682 /* We don't want to write - but we must make sure that
683 O_CREAT doesn't create the file if we have write
684 access into the directory.
686 flags &= ~(O_CREAT|O_EXCL);
687 local_flags &= ~(O_CREAT|O_EXCL);
692 * This little piece of insanity is inspired by the
693 * fact that an NT client can open a file for O_RDONLY,
694 * but set the create disposition to FILE_EXISTS_TRUNCATE.
695 * If the client *can* write to the file, then it expects to
696 * truncate the file, even though it is opening for readonly.
697 * Quicken uses this stupid trick in backup file creation...
698 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
699 * for helping track this one down. It didn't bite us in 2.0.x
700 * as we always opened files read-write in that release. JRA.
703 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
704 DEBUG(10,("open_file: truncate requested on read-only open "
705 "for file %s\n", smb_fname_str_dbg(smb_fname)));
706 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
709 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
710 (!file_existed && (local_flags & O_CREAT)) ||
711 ((local_flags & O_TRUNC) == O_TRUNC) ) {
712 const char *wild;
713 int ret;
715 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
717 * We would block on opening a FIFO with no one else on the
718 * other end. Do what we used to do and add O_NONBLOCK to the
719 * open flags. JRA.
722 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
723 local_flags &= ~O_TRUNC; /* Can't truncate a FIFO. */
724 local_flags |= O_NONBLOCK;
726 #endif
728 /* Don't create files with Microsoft wildcard characters. */
729 if (fsp->base_fsp) {
731 * wildcard characters are allowed in stream names
732 * only test the basefilename
734 wild = fsp->base_fsp->fsp_name->base_name;
735 } else {
736 wild = smb_fname->base_name;
738 if ((local_flags & O_CREAT) && !file_existed &&
739 ms_has_wild(wild)) {
740 return NT_STATUS_OBJECT_NAME_INVALID;
743 /* Can we access this file ? */
744 if (!fsp->base_fsp) {
745 /* Only do this check on non-stream open. */
746 if (file_existed) {
747 status = smbd_check_access_rights(conn,
748 smb_fname,
749 false,
750 access_mask);
751 } else if (local_flags & O_CREAT){
752 status = check_parent_access(conn,
753 smb_fname,
754 SEC_DIR_ADD_FILE);
755 } else {
756 /* File didn't exist and no O_CREAT. */
757 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
759 if (!NT_STATUS_IS_OK(status)) {
760 DEBUG(10,("open_file: "
761 "%s on file "
762 "%s returned %s\n",
763 file_existed ?
764 "smbd_check_access_rights" :
765 "check_parent_access",
766 smb_fname_str_dbg(smb_fname),
767 nt_errstr(status) ));
768 return status;
772 /* Actually do the open */
773 status = fd_open_atomic(conn, fsp, local_flags,
774 unx_mode, p_file_created);
775 if (!NT_STATUS_IS_OK(status)) {
776 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
777 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
778 nt_errstr(status),local_flags,flags));
779 return status;
782 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
783 if (ret == -1) {
784 /* If we have an fd, this stat should succeed. */
785 DEBUG(0,("Error doing fstat on open file %s "
786 "(%s)\n",
787 smb_fname_str_dbg(smb_fname),
788 strerror(errno) ));
789 status = map_nt_error_from_unix(errno);
790 fd_close(fsp);
791 return status;
794 if (*p_file_created) {
795 /* We created this file. */
797 bool need_re_stat = false;
798 /* Do all inheritance work after we've
799 done a successful fstat call and filled
800 in the stat struct in fsp->fsp_name. */
802 /* Inherit the ACL if required */
803 if (lp_inherit_perms(SNUM(conn))) {
804 inherit_access_posix_acl(conn, parent_dir,
805 smb_fname->base_name,
806 unx_mode);
807 need_re_stat = true;
810 /* Change the owner if required. */
811 if (lp_inherit_owner(SNUM(conn))) {
812 change_file_owner_to_parent(conn, parent_dir,
813 fsp);
814 need_re_stat = true;
817 if (need_re_stat) {
818 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
819 /* If we have an fd, this stat should succeed. */
820 if (ret == -1) {
821 DEBUG(0,("Error doing fstat on open file %s "
822 "(%s)\n",
823 smb_fname_str_dbg(smb_fname),
824 strerror(errno) ));
828 notify_fname(conn, NOTIFY_ACTION_ADDED,
829 FILE_NOTIFY_CHANGE_FILE_NAME,
830 smb_fname->base_name);
832 } else {
833 fsp->fh->fd = -1; /* What we used to call a stat open. */
834 if (!file_existed) {
835 /* File must exist for a stat open. */
836 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
839 status = smbd_check_access_rights(conn,
840 smb_fname,
841 false,
842 access_mask);
844 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
845 fsp->posix_open &&
846 S_ISLNK(smb_fname->st.st_ex_mode)) {
847 /* This is a POSIX stat open for delete
848 * or rename on a symlink that points
849 * nowhere. Allow. */
850 DEBUG(10,("open_file: allowing POSIX "
851 "open on bad symlink %s\n",
852 smb_fname_str_dbg(smb_fname)));
853 status = NT_STATUS_OK;
856 if (!NT_STATUS_IS_OK(status)) {
857 DEBUG(10,("open_file: "
858 "smbd_check_access_rights on file "
859 "%s returned %s\n",
860 smb_fname_str_dbg(smb_fname),
861 nt_errstr(status) ));
862 return status;
867 * POSIX allows read-only opens of directories. We don't
868 * want to do this (we use a different code path for this)
869 * so catch a directory open and return an EISDIR. JRA.
872 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
873 fd_close(fsp);
874 errno = EISDIR;
875 return NT_STATUS_FILE_IS_A_DIRECTORY;
878 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
879 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
880 fsp->file_pid = req ? req->smbpid : 0;
881 fsp->can_lock = True;
882 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
883 fsp->can_write =
884 CAN_WRITE(conn) &&
885 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
886 fsp->print_file = NULL;
887 fsp->modified = False;
888 fsp->sent_oplock_break = NO_BREAK_SENT;
889 fsp->is_directory = False;
890 if (conn->aio_write_behind_list &&
891 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
892 conn->case_sensitive)) {
893 fsp->aio_write_behind = True;
896 fsp->wcp = NULL; /* Write cache pointer. */
898 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
899 conn->session_info->unix_info->unix_name,
900 smb_fname_str_dbg(smb_fname),
901 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
902 conn->num_files_open));
904 errno = 0;
905 return NT_STATUS_OK;
908 /****************************************************************************
909 Check if we can open a file with a share mode.
910 Returns True if conflict, False if not.
911 ****************************************************************************/
913 static bool share_conflict(struct share_mode_entry *entry,
914 uint32 access_mask,
915 uint32 share_access)
917 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
918 "entry->share_access = 0x%x, "
919 "entry->private_options = 0x%x\n",
920 (unsigned int)entry->access_mask,
921 (unsigned int)entry->share_access,
922 (unsigned int)entry->private_options));
924 if (server_id_is_disconnected(&entry->pid)) {
926 * note: cleanup should have been done by
927 * delay_for_batch_oplocks()
929 return false;
932 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
933 (unsigned int)access_mask, (unsigned int)share_access));
935 if ((entry->access_mask & (FILE_WRITE_DATA|
936 FILE_APPEND_DATA|
937 FILE_READ_DATA|
938 FILE_EXECUTE|
939 DELETE_ACCESS)) == 0) {
940 DEBUG(10,("share_conflict: No conflict due to "
941 "entry->access_mask = 0x%x\n",
942 (unsigned int)entry->access_mask ));
943 return False;
946 if ((access_mask & (FILE_WRITE_DATA|
947 FILE_APPEND_DATA|
948 FILE_READ_DATA|
949 FILE_EXECUTE|
950 DELETE_ACCESS)) == 0) {
951 DEBUG(10,("share_conflict: No conflict due to "
952 "access_mask = 0x%x\n",
953 (unsigned int)access_mask ));
954 return False;
957 #if 1 /* JRA TEST - Superdebug. */
958 #define CHECK_MASK(num, am, right, sa, share) \
959 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
960 (unsigned int)(num), (unsigned int)(am), \
961 (unsigned int)(right), (unsigned int)(am)&(right) )); \
962 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
963 (unsigned int)(num), (unsigned int)(sa), \
964 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
965 if (((am) & (right)) && !((sa) & (share))) { \
966 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
967 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
968 (unsigned int)(share) )); \
969 return True; \
971 #else
972 #define CHECK_MASK(num, am, right, sa, share) \
973 if (((am) & (right)) && !((sa) & (share))) { \
974 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
975 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
976 (unsigned int)(share) )); \
977 return True; \
979 #endif
981 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
982 share_access, FILE_SHARE_WRITE);
983 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
984 entry->share_access, FILE_SHARE_WRITE);
986 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
987 share_access, FILE_SHARE_READ);
988 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
989 entry->share_access, FILE_SHARE_READ);
991 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
992 share_access, FILE_SHARE_DELETE);
993 CHECK_MASK(6, access_mask, DELETE_ACCESS,
994 entry->share_access, FILE_SHARE_DELETE);
996 DEBUG(10,("share_conflict: No conflict.\n"));
997 return False;
1000 #if defined(DEVELOPER)
1001 static void validate_my_share_entries(struct smbd_server_connection *sconn,
1002 int num,
1003 struct share_mode_entry *share_entry)
1005 struct server_id self = messaging_server_id(sconn->msg_ctx);
1006 files_struct *fsp;
1008 if (!serverid_equal(&self, &share_entry->pid)) {
1009 return;
1012 if (is_deferred_open_entry(share_entry) &&
1013 !open_was_deferred(sconn, share_entry->op_mid)) {
1014 char *str = talloc_asprintf(talloc_tos(),
1015 "Got a deferred entry without a request: "
1016 "PANIC: %s\n",
1017 share_mode_str(talloc_tos(), num, share_entry));
1018 smb_panic(str);
1021 if (!is_valid_share_mode_entry(share_entry)) {
1022 return;
1025 fsp = file_find_dif(sconn, share_entry->id,
1026 share_entry->share_file_id);
1027 if (!fsp) {
1028 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1029 share_mode_str(talloc_tos(), num, share_entry) ));
1030 smb_panic("validate_my_share_entries: Cannot match a "
1031 "share entry with an open file\n");
1034 if (is_deferred_open_entry(share_entry)) {
1035 goto panic;
1038 if ((share_entry->op_type == NO_OPLOCK) &&
1039 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
1040 /* Someone has already written to it, but I haven't yet
1041 * noticed */
1042 return;
1045 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
1046 goto panic;
1049 return;
1051 panic:
1053 char *str;
1054 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1055 share_mode_str(talloc_tos(), num, share_entry) ));
1056 str = talloc_asprintf(talloc_tos(),
1057 "validate_my_share_entries: "
1058 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1059 fsp->fsp_name->base_name,
1060 (unsigned int)fsp->oplock_type,
1061 (unsigned int)share_entry->op_type );
1062 smb_panic(str);
1065 #endif
1067 bool is_stat_open(uint32 access_mask)
1069 return (access_mask &&
1070 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
1071 FILE_WRITE_ATTRIBUTES))==0) &&
1072 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
1073 FILE_WRITE_ATTRIBUTES)) != 0));
1076 /****************************************************************************
1077 Deal with share modes
1078 Invarient: Share mode must be locked on entry and exit.
1079 Returns -1 on error, or number of share modes on success (may be zero).
1080 ****************************************************************************/
1082 static NTSTATUS open_mode_check(connection_struct *conn,
1083 struct share_mode_lock *lck,
1084 uint32_t name_hash,
1085 uint32 access_mask,
1086 uint32 share_access,
1087 uint32 create_options,
1088 bool *file_existed)
1090 int i;
1092 if(lck->data->num_share_modes == 0) {
1093 return NT_STATUS_OK;
1096 /* A delete on close prohibits everything */
1098 if (is_delete_on_close_set(lck, name_hash)) {
1100 * Check the delete on close token
1101 * is valid. It could have been left
1102 * after a server crash.
1104 for(i = 0; i < lck->data->num_share_modes; i++) {
1105 if (!share_mode_stale_pid(lck->data, i)) {
1107 *file_existed = true;
1109 return NT_STATUS_DELETE_PENDING;
1112 return NT_STATUS_OK;
1115 if (is_stat_open(access_mask)) {
1116 /* Stat open that doesn't trigger oplock breaks or share mode
1117 * checks... ! JRA. */
1118 return NT_STATUS_OK;
1122 * Check if the share modes will give us access.
1125 #if defined(DEVELOPER)
1126 for(i = 0; i < lck->data->num_share_modes; i++) {
1127 validate_my_share_entries(conn->sconn, i,
1128 &lck->data->share_modes[i]);
1130 #endif
1132 /* Now we check the share modes, after any oplock breaks. */
1133 for(i = 0; i < lck->data->num_share_modes; i++) {
1135 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1136 continue;
1139 /* someone else has a share lock on it, check to see if we can
1140 * too */
1141 if (share_conflict(&lck->data->share_modes[i],
1142 access_mask, share_access)) {
1144 if (share_mode_stale_pid(lck->data, i)) {
1145 continue;
1148 *file_existed = true;
1150 return NT_STATUS_SHARING_VIOLATION;
1154 if (lck->data->num_share_modes != 0) {
1155 *file_existed = true;
1158 return NT_STATUS_OK;
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 if (server_id_is_disconnected(&batch_entry->pid)) {
1310 * TODO: clean up.
1311 * This could be achieved by sending a break message
1312 * to ourselves. Special considerations for files
1313 * with delete_on_close flag set!
1315 * For now we keep it simple and do not
1316 * allow delete on close for durable handles.
1318 return false;
1321 /* Found a batch oplock */
1322 send_break_message(fsp, batch_entry, mid, oplock_request);
1323 return true;
1326 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1327 uint64_t mid,
1328 int oplock_request,
1329 struct share_mode_entry *ex_entry)
1331 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1332 return false;
1334 if (ex_entry == NULL) {
1335 return false;
1338 if (server_id_is_disconnected(&ex_entry->pid)) {
1340 * since only durable handles can get disconnected,
1341 * and we can only get durable handles with batch oplocks,
1342 * this should actually never be reached...
1344 return false;
1347 send_break_message(fsp, ex_entry, mid, oplock_request);
1348 return true;
1351 static bool file_has_brlocks(files_struct *fsp)
1353 struct byte_range_lock *br_lck;
1355 br_lck = brl_get_locks_readonly(fsp);
1356 if (!br_lck)
1357 return false;
1359 return br_lck->num_locks > 0 ? true : false;
1362 static void grant_fsp_oplock_type(files_struct *fsp,
1363 int oplock_request,
1364 bool got_level2_oplock,
1365 bool got_a_none_oplock)
1367 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1368 lp_level2_oplocks(SNUM(fsp->conn));
1370 /* Start by granting what the client asked for,
1371 but ensure no SAMBA_PRIVATE bits can be set. */
1372 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1374 if (oplock_request & INTERNAL_OPEN_ONLY) {
1375 /* No oplocks on internal open. */
1376 fsp->oplock_type = NO_OPLOCK;
1377 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1378 fsp->oplock_type, fsp_str_dbg(fsp)));
1379 return;
1382 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1383 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1384 fsp_str_dbg(fsp)));
1385 fsp->oplock_type = NO_OPLOCK;
1388 if (is_stat_open(fsp->access_mask)) {
1389 /* Leave the value already set. */
1390 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1391 fsp->oplock_type, fsp_str_dbg(fsp)));
1392 return;
1396 * Match what was requested (fsp->oplock_type) with
1397 * what was found in the existing share modes.
1400 if (got_a_none_oplock) {
1401 fsp->oplock_type = NO_OPLOCK;
1402 } else if (got_level2_oplock) {
1403 if (fsp->oplock_type == NO_OPLOCK ||
1404 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1405 /* Store a level2 oplock, but don't tell the client */
1406 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1407 } else {
1408 fsp->oplock_type = LEVEL_II_OPLOCK;
1410 } else {
1411 /* All share_mode_entries are placeholders or deferred.
1412 * Silently upgrade to fake levelII if the client didn't
1413 * ask for an oplock. */
1414 if (fsp->oplock_type == NO_OPLOCK) {
1415 /* Store a level2 oplock, but don't tell the client */
1416 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1421 * Don't grant level2 to clients that don't want them
1422 * or if we've turned them off.
1424 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1425 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1428 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1429 fsp->oplock_type, fsp_str_dbg(fsp)));
1432 static bool request_timed_out(struct timeval request_time,
1433 struct timeval timeout)
1435 struct timeval now, end_time;
1436 GetTimeOfDay(&now);
1437 end_time = timeval_sum(&request_time, &timeout);
1438 return (timeval_compare(&end_time, &now) < 0);
1441 /****************************************************************************
1442 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1443 ****************************************************************************/
1445 static void defer_open(struct share_mode_lock *lck,
1446 struct timeval request_time,
1447 struct timeval timeout,
1448 struct smb_request *req,
1449 struct deferred_open_record *state)
1451 struct server_id self = messaging_server_id(req->sconn->msg_ctx);
1453 /* Paranoia check */
1455 if (lck) {
1456 int i;
1458 for (i=0; i<lck->data->num_share_modes; i++) {
1459 struct share_mode_entry *e = &lck->data->share_modes[i];
1461 if (is_deferred_open_entry(e) &&
1462 serverid_equal(&self, &e->pid) &&
1463 (e->op_mid == req->mid)) {
1464 DEBUG(0, ("Trying to defer an already deferred "
1465 "request: mid=%llu, exiting\n",
1466 (unsigned long long)req->mid));
1467 exit_server("attempt to defer a deferred request");
1472 /* End paranoia check */
1474 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1475 "open entry for mid %llu\n",
1476 (unsigned int)request_time.tv_sec,
1477 (unsigned int)request_time.tv_usec,
1478 (unsigned long long)req->mid));
1480 if (!push_deferred_open_message_smb(req, request_time, timeout,
1481 state->id, (char *)state, sizeof(*state))) {
1482 exit_server("push_deferred_open_message_smb failed");
1484 if (lck) {
1485 add_deferred_open(lck, req->mid, request_time, self, state->id);
1490 /****************************************************************************
1491 On overwrite open ensure that the attributes match.
1492 ****************************************************************************/
1494 static bool open_match_attributes(connection_struct *conn,
1495 uint32 old_dos_attr,
1496 uint32 new_dos_attr,
1497 mode_t existing_unx_mode,
1498 mode_t new_unx_mode,
1499 mode_t *returned_unx_mode)
1501 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1503 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1504 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1506 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1507 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1508 *returned_unx_mode = new_unx_mode;
1509 } else {
1510 *returned_unx_mode = (mode_t)0;
1513 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1514 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1515 "returned_unx_mode = 0%o\n",
1516 (unsigned int)old_dos_attr,
1517 (unsigned int)existing_unx_mode,
1518 (unsigned int)new_dos_attr,
1519 (unsigned int)*returned_unx_mode ));
1521 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1522 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1523 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1524 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1525 return False;
1528 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1529 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1530 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1531 return False;
1534 return True;
1537 /****************************************************************************
1538 Special FCB or DOS processing in the case of a sharing violation.
1539 Try and find a duplicated file handle.
1540 ****************************************************************************/
1542 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1543 connection_struct *conn,
1544 files_struct *fsp_to_dup_into,
1545 const struct smb_filename *smb_fname,
1546 struct file_id id,
1547 uint16 file_pid,
1548 uint64_t vuid,
1549 uint32 access_mask,
1550 uint32 share_access,
1551 uint32 create_options)
1553 files_struct *fsp;
1555 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1556 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1558 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1559 fsp = file_find_di_next(fsp)) {
1561 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1562 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1563 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1564 fsp->fh->fd, (unsigned long long)fsp->vuid,
1565 (unsigned int)fsp->file_pid,
1566 (unsigned int)fsp->fh->private_options,
1567 (unsigned int)fsp->access_mask ));
1569 if (fsp->fh->fd != -1 &&
1570 fsp->vuid == vuid &&
1571 fsp->file_pid == file_pid &&
1572 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1573 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1574 (fsp->access_mask & FILE_WRITE_DATA) &&
1575 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1576 strequal(fsp->fsp_name->stream_name,
1577 smb_fname->stream_name)) {
1578 DEBUG(10,("fcb_or_dos_open: file match\n"));
1579 break;
1583 if (!fsp) {
1584 return NT_STATUS_NOT_FOUND;
1587 /* quite an insane set of semantics ... */
1588 if (is_executable(smb_fname->base_name) &&
1589 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1590 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1591 return NT_STATUS_INVALID_PARAMETER;
1594 /* We need to duplicate this fsp. */
1595 return dup_file_fsp(req, fsp, access_mask, share_access,
1596 create_options, fsp_to_dup_into);
1599 static void schedule_defer_open(struct share_mode_lock *lck,
1600 struct timeval request_time,
1601 struct smb_request *req)
1603 struct deferred_open_record state;
1605 /* This is a relative time, added to the absolute
1606 request_time value to get the absolute timeout time.
1607 Note that if this is the second or greater time we enter
1608 this codepath for this particular request mid then
1609 request_time is left as the absolute time of the *first*
1610 time this request mid was processed. This is what allows
1611 the request to eventually time out. */
1613 struct timeval timeout;
1615 /* Normally the smbd we asked should respond within
1616 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1617 * the client did, give twice the timeout as a safety
1618 * measure here in case the other smbd is stuck
1619 * somewhere else. */
1621 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1623 /* Nothing actually uses state.delayed_for_oplocks
1624 but it's handy to differentiate in debug messages
1625 between a 30 second delay due to oplock break, and
1626 a 1 second delay for share mode conflicts. */
1628 state.delayed_for_oplocks = True;
1629 state.async_open = false;
1630 state.id = lck->data->id;
1632 if (!request_timed_out(request_time, timeout)) {
1633 defer_open(lck, request_time, timeout, req, &state);
1637 /****************************************************************************
1638 Reschedule an open call that went asynchronous.
1639 ****************************************************************************/
1641 static void schedule_async_open(struct timeval request_time,
1642 struct smb_request *req)
1644 struct deferred_open_record state;
1645 struct timeval timeout;
1647 timeout = timeval_set(20, 0);
1649 ZERO_STRUCT(state);
1650 state.delayed_for_oplocks = false;
1651 state.async_open = true;
1653 if (!request_timed_out(request_time, timeout)) {
1654 defer_open(NULL, request_time, timeout, req, &state);
1658 /****************************************************************************
1659 Work out what access_mask to use from what the client sent us.
1660 ****************************************************************************/
1662 static NTSTATUS smbd_calculate_maximum_allowed_access(
1663 connection_struct *conn,
1664 const struct smb_filename *smb_fname,
1665 bool use_privs,
1666 uint32_t *p_access_mask)
1668 struct security_descriptor *sd;
1669 uint32_t access_granted;
1670 NTSTATUS status;
1672 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
1673 *p_access_mask |= FILE_GENERIC_ALL;
1674 return NT_STATUS_OK;
1677 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1678 (SECINFO_OWNER |
1679 SECINFO_GROUP |
1680 SECINFO_DACL),&sd);
1682 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1684 * File did not exist
1686 *p_access_mask = FILE_GENERIC_ALL;
1687 return NT_STATUS_OK;
1689 if (!NT_STATUS_IS_OK(status)) {
1690 DEBUG(10,("Could not get acl on file %s: %s\n",
1691 smb_fname_str_dbg(smb_fname),
1692 nt_errstr(status)));
1693 return NT_STATUS_ACCESS_DENIED;
1697 * Never test FILE_READ_ATTRIBUTES. se_file_access_check()
1698 * also takes care of owner WRITE_DAC and READ_CONTROL.
1700 status = se_file_access_check(sd,
1701 get_current_nttok(conn),
1702 use_privs,
1703 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1704 &access_granted);
1706 TALLOC_FREE(sd);
1708 if (!NT_STATUS_IS_OK(status)) {
1709 DEBUG(10, ("Access denied on file %s: "
1710 "when calculating maximum access\n",
1711 smb_fname_str_dbg(smb_fname)));
1712 return NT_STATUS_ACCESS_DENIED;
1714 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1715 return NT_STATUS_OK;
1718 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1719 const struct smb_filename *smb_fname,
1720 bool use_privs,
1721 uint32_t access_mask,
1722 uint32_t *access_mask_out)
1724 NTSTATUS status;
1725 uint32_t orig_access_mask = access_mask;
1726 uint32_t rejected_share_access;
1729 * Convert GENERIC bits to specific bits.
1732 se_map_generic(&access_mask, &file_generic_mapping);
1734 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1735 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1737 status = smbd_calculate_maximum_allowed_access(
1738 conn, smb_fname, use_privs, &access_mask);
1740 if (!NT_STATUS_IS_OK(status)) {
1741 return status;
1744 access_mask &= conn->share_access;
1747 rejected_share_access = access_mask & ~(conn->share_access);
1749 if (rejected_share_access) {
1750 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1751 "file %s: rejected by share access mask[0x%08X] "
1752 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1753 smb_fname_str_dbg(smb_fname),
1754 conn->share_access,
1755 orig_access_mask, access_mask,
1756 rejected_share_access));
1757 return NT_STATUS_ACCESS_DENIED;
1760 *access_mask_out = access_mask;
1761 return NT_STATUS_OK;
1764 /****************************************************************************
1765 Remove the deferred open entry under lock.
1766 ****************************************************************************/
1768 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1769 struct server_id pid)
1771 struct share_mode_lock *lck = get_existing_share_mode_lock(
1772 talloc_tos(), id);
1773 if (lck == NULL) {
1774 DEBUG(0, ("could not get share mode lock\n"));
1775 return;
1777 del_deferred_open_entry(lck, mid, pid);
1778 TALLOC_FREE(lck);
1781 /****************************************************************************
1782 Return true if this is a state pointer to an asynchronous create.
1783 ****************************************************************************/
1785 bool is_deferred_open_async(const void *ptr)
1787 const struct deferred_open_record *state = (const struct deferred_open_record *)ptr;
1789 return state->async_open;
1792 static bool clear_ads(uint32_t create_disposition)
1794 bool ret = false;
1796 switch (create_disposition) {
1797 case FILE_SUPERSEDE:
1798 case FILE_OVERWRITE_IF:
1799 case FILE_OVERWRITE:
1800 ret = true;
1801 break;
1802 default:
1803 break;
1805 return ret;
1808 static int disposition_to_open_flags(uint32_t create_disposition)
1810 int ret = 0;
1813 * Currently we're using FILE_SUPERSEDE as the same as
1814 * FILE_OVERWRITE_IF but they really are
1815 * different. FILE_SUPERSEDE deletes an existing file
1816 * (requiring delete access) then recreates it.
1819 switch (create_disposition) {
1820 case FILE_SUPERSEDE:
1821 case FILE_OVERWRITE_IF:
1823 * If file exists replace/overwrite. If file doesn't
1824 * exist create.
1826 ret = O_CREAT|O_TRUNC;
1827 break;
1829 case FILE_OPEN:
1831 * If file exists open. If file doesn't exist error.
1833 ret = 0;
1834 break;
1836 case FILE_OVERWRITE:
1838 * If file exists overwrite. If file doesn't exist
1839 * error.
1841 ret = O_TRUNC;
1842 break;
1844 case FILE_CREATE:
1846 * If file exists error. If file doesn't exist create.
1848 ret = O_CREAT|O_EXCL;
1849 break;
1851 case FILE_OPEN_IF:
1853 * If file exists open. If file doesn't exist create.
1855 ret = O_CREAT;
1856 break;
1858 return ret;
1861 /****************************************************************************
1862 Open a file with a share mode. Passed in an already created files_struct *.
1863 ****************************************************************************/
1865 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1866 struct smb_request *req,
1867 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1868 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1869 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1870 uint32 create_options, /* options such as delete on close. */
1871 uint32 new_dos_attributes, /* attributes used for new file. */
1872 int oplock_request, /* internal Samba oplock codes. */
1873 /* Information (FILE_EXISTS etc.) */
1874 uint32_t private_flags, /* Samba specific flags. */
1875 int *pinfo,
1876 files_struct *fsp)
1878 struct smb_filename *smb_fname = fsp->fsp_name;
1879 int flags=0;
1880 int flags2=0;
1881 bool file_existed = VALID_STAT(smb_fname->st);
1882 bool def_acl = False;
1883 bool posix_open = False;
1884 bool new_file_created = False;
1885 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1886 mode_t new_unx_mode = (mode_t)0;
1887 mode_t unx_mode = (mode_t)0;
1888 int info;
1889 uint32 existing_dos_attributes = 0;
1890 struct timeval request_time = timeval_zero();
1891 struct share_mode_lock *lck = NULL;
1892 uint32 open_access_mask = access_mask;
1893 NTSTATUS status;
1894 char *parent_dir;
1895 SMB_STRUCT_STAT saved_stat = smb_fname->st;
1897 if (conn->printer) {
1899 * Printers are handled completely differently.
1900 * Most of the passed parameters are ignored.
1903 if (pinfo) {
1904 *pinfo = FILE_WAS_CREATED;
1907 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1908 smb_fname_str_dbg(smb_fname)));
1910 if (!req) {
1911 DEBUG(0,("open_file_ntcreate: printer open without "
1912 "an SMB request!\n"));
1913 return NT_STATUS_INTERNAL_ERROR;
1916 return print_spool_open(fsp, smb_fname->base_name,
1917 req->vuid);
1920 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1921 NULL)) {
1922 return NT_STATUS_NO_MEMORY;
1925 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1926 posix_open = True;
1927 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1928 new_dos_attributes = 0;
1929 } else {
1930 /* Windows allows a new file to be created and
1931 silently removes a FILE_ATTRIBUTE_DIRECTORY
1932 sent by the client. Do the same. */
1934 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1936 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1937 * created new. */
1938 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1939 smb_fname, parent_dir);
1942 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1943 "access_mask=0x%x share_access=0x%x "
1944 "create_disposition = 0x%x create_options=0x%x "
1945 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1946 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1947 access_mask, share_access, create_disposition,
1948 create_options, (unsigned int)unx_mode, oplock_request,
1949 (unsigned int)private_flags));
1951 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1952 DEBUG(0, ("No smb request but not an internal only open!\n"));
1953 return NT_STATUS_INTERNAL_ERROR;
1957 * Only non-internal opens can be deferred at all
1960 if (req) {
1961 void *ptr;
1962 if (get_deferred_open_message_state(req,
1963 &request_time,
1964 &ptr)) {
1965 /* Remember the absolute time of the original
1966 request with this mid. We'll use it later to
1967 see if this has timed out. */
1969 /* If it was an async create retry, the file
1970 didn't exist. */
1972 if (is_deferred_open_async(ptr)) {
1973 SET_STAT_INVALID(smb_fname->st);
1974 file_existed = false;
1975 } else {
1976 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1977 /* Remove the deferred open entry under lock. */
1978 remove_deferred_open_entry(
1979 state->id, req->mid,
1980 messaging_server_id(req->sconn->msg_ctx));
1983 /* Ensure we don't reprocess this message. */
1984 remove_deferred_open_message_smb(req->sconn, req->mid);
1988 if (!posix_open) {
1989 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1990 if (file_existed) {
1991 existing_dos_attributes = dos_mode(conn, smb_fname);
1995 /* ignore any oplock requests if oplocks are disabled */
1996 if (!lp_oplocks(SNUM(conn)) ||
1997 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1998 /* Mask off everything except the private Samba bits. */
1999 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2002 /* this is for OS/2 long file names - say we don't support them */
2003 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
2004 /* OS/2 Workplace shell fix may be main code stream in a later
2005 * release. */
2006 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2007 "supported.\n"));
2008 if (use_nt_status()) {
2009 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2011 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2014 switch( create_disposition ) {
2015 case FILE_OPEN:
2016 /* If file exists open. If file doesn't exist error. */
2017 if (!file_existed) {
2018 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2019 "requested for file %s and file "
2020 "doesn't exist.\n",
2021 smb_fname_str_dbg(smb_fname)));
2022 errno = ENOENT;
2023 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2025 break;
2027 case FILE_OVERWRITE:
2028 /* If file exists overwrite. If file doesn't exist
2029 * error. */
2030 if (!file_existed) {
2031 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2032 "requested for file %s and file "
2033 "doesn't exist.\n",
2034 smb_fname_str_dbg(smb_fname) ));
2035 errno = ENOENT;
2036 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2038 break;
2040 case FILE_CREATE:
2041 /* If file exists error. If file doesn't exist
2042 * create. */
2043 if (file_existed) {
2044 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2045 "requested for file %s and file "
2046 "already exists.\n",
2047 smb_fname_str_dbg(smb_fname)));
2048 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2049 errno = EISDIR;
2050 } else {
2051 errno = EEXIST;
2053 return map_nt_error_from_unix(errno);
2055 break;
2057 case FILE_SUPERSEDE:
2058 case FILE_OVERWRITE_IF:
2059 case FILE_OPEN_IF:
2060 break;
2061 default:
2062 return NT_STATUS_INVALID_PARAMETER;
2065 flags2 = disposition_to_open_flags(create_disposition);
2067 /* We only care about matching attributes on file exists and
2068 * overwrite. */
2070 if (!posix_open && file_existed &&
2071 ((create_disposition == FILE_OVERWRITE) ||
2072 (create_disposition == FILE_OVERWRITE_IF))) {
2073 if (!open_match_attributes(conn, existing_dos_attributes,
2074 new_dos_attributes,
2075 smb_fname->st.st_ex_mode,
2076 unx_mode, &new_unx_mode)) {
2077 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2078 "for file %s (%x %x) (0%o, 0%o)\n",
2079 smb_fname_str_dbg(smb_fname),
2080 existing_dos_attributes,
2081 new_dos_attributes,
2082 (unsigned int)smb_fname->st.st_ex_mode,
2083 (unsigned int)unx_mode ));
2084 errno = EACCES;
2085 return NT_STATUS_ACCESS_DENIED;
2089 status = smbd_calculate_access_mask(conn, smb_fname,
2090 false,
2091 access_mask,
2092 &access_mask);
2093 if (!NT_STATUS_IS_OK(status)) {
2094 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2095 "on file %s returned %s\n",
2096 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2097 return status;
2100 open_access_mask = access_mask;
2102 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
2103 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2106 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2107 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2108 access_mask));
2111 * Note that we ignore the append flag as append does not
2112 * mean the same thing under DOS and Unix.
2115 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
2116 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
2117 /* DENY_DOS opens are always underlying read-write on the
2118 file handle, no matter what the requested access mask
2119 says. */
2120 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
2121 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
2122 flags = O_RDWR;
2123 } else {
2124 flags = O_WRONLY;
2126 } else {
2127 flags = O_RDONLY;
2131 * Currently we only look at FILE_WRITE_THROUGH for create options.
2134 #if defined(O_SYNC)
2135 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2136 flags2 |= O_SYNC;
2138 #endif /* O_SYNC */
2140 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2141 flags2 |= O_APPEND;
2144 if (!posix_open && !CAN_WRITE(conn)) {
2146 * We should really return a permission denied error if either
2147 * O_CREAT or O_TRUNC are set, but for compatibility with
2148 * older versions of Samba we just AND them out.
2150 flags2 &= ~(O_CREAT|O_TRUNC);
2154 * Ensure we can't write on a read-only share or file.
2157 if (flags != O_RDONLY && file_existed &&
2158 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2159 DEBUG(5,("open_file_ntcreate: write access requested for "
2160 "file %s on read only %s\n",
2161 smb_fname_str_dbg(smb_fname),
2162 !CAN_WRITE(conn) ? "share" : "file" ));
2163 errno = EACCES;
2164 return NT_STATUS_ACCESS_DENIED;
2167 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2168 fsp->share_access = share_access;
2169 fsp->fh->private_options = private_flags;
2170 fsp->access_mask = open_access_mask; /* We change this to the
2171 * requested access_mask after
2172 * the open is done. */
2173 fsp->posix_open = posix_open;
2175 /* Ensure no SAMBA_PRIVATE bits can be set. */
2176 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2178 if (timeval_is_zero(&request_time)) {
2179 request_time = fsp->open_time;
2182 if (file_existed) {
2183 struct share_mode_entry *batch_entry = NULL;
2184 struct share_mode_entry *exclusive_entry = NULL;
2185 bool got_level2_oplock = false;
2186 bool got_a_none_oplock = false;
2187 struct file_id id;
2189 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2190 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2192 lck = get_share_mode_lock(talloc_tos(), id,
2193 conn->connectpath,
2194 smb_fname, &old_write_time);
2195 if (lck == NULL) {
2196 DEBUG(0, ("Could not get share mode lock\n"));
2197 return NT_STATUS_SHARING_VIOLATION;
2200 /* Get the types we need to examine. */
2201 find_oplock_types(fsp,
2202 oplock_request,
2203 lck,
2204 &batch_entry,
2205 &exclusive_entry,
2206 &got_level2_oplock,
2207 &got_a_none_oplock);
2209 /* First pass - send break only on batch oplocks. */
2210 if ((req != NULL) &&
2211 delay_for_batch_oplocks(fsp,
2212 req->mid,
2213 oplock_request,
2214 batch_entry)) {
2215 schedule_defer_open(lck, request_time, req);
2216 TALLOC_FREE(lck);
2217 return NT_STATUS_SHARING_VIOLATION;
2220 /* Use the client requested access mask here, not the one we
2221 * open with. */
2222 status = open_mode_check(conn, lck, fsp->name_hash,
2223 access_mask, share_access,
2224 create_options, &file_existed);
2226 if (NT_STATUS_IS_OK(status)) {
2227 /* We might be going to allow this open. Check oplock
2228 * status again. */
2229 /* Second pass - send break for both batch or
2230 * exclusive oplocks. */
2231 if ((req != NULL) &&
2232 delay_for_exclusive_oplocks(
2233 fsp,
2234 req->mid,
2235 oplock_request,
2236 exclusive_entry)) {
2237 schedule_defer_open(lck, request_time, req);
2238 TALLOC_FREE(lck);
2239 return NT_STATUS_SHARING_VIOLATION;
2243 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2244 /* DELETE_PENDING is not deferred for a second */
2245 TALLOC_FREE(lck);
2246 return status;
2249 grant_fsp_oplock_type(fsp,
2250 oplock_request,
2251 got_level2_oplock,
2252 got_a_none_oplock);
2254 if (!NT_STATUS_IS_OK(status)) {
2255 uint32 can_access_mask;
2256 bool can_access = True;
2258 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2260 /* Check if this can be done with the deny_dos and fcb
2261 * calls. */
2262 if (private_flags &
2263 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2264 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2265 if (req == NULL) {
2266 DEBUG(0, ("DOS open without an SMB "
2267 "request!\n"));
2268 TALLOC_FREE(lck);
2269 return NT_STATUS_INTERNAL_ERROR;
2272 /* Use the client requested access mask here,
2273 * not the one we open with. */
2274 status = fcb_or_dos_open(req,
2275 conn,
2276 fsp,
2277 smb_fname,
2279 req->smbpid,
2280 req->vuid,
2281 access_mask,
2282 share_access,
2283 create_options);
2285 if (NT_STATUS_IS_OK(status)) {
2286 TALLOC_FREE(lck);
2287 if (pinfo) {
2288 *pinfo = FILE_WAS_OPENED;
2290 return NT_STATUS_OK;
2295 * This next line is a subtlety we need for
2296 * MS-Access. If a file open will fail due to share
2297 * permissions and also for security (access) reasons,
2298 * we need to return the access failed error, not the
2299 * share error. We can't open the file due to kernel
2300 * oplock deadlock (it's possible we failed above on
2301 * the open_mode_check()) so use a userspace check.
2304 if (flags & O_RDWR) {
2305 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2306 } else if (flags & O_WRONLY) {
2307 can_access_mask = FILE_WRITE_DATA;
2308 } else {
2309 can_access_mask = FILE_READ_DATA;
2312 if (((can_access_mask & FILE_WRITE_DATA) &&
2313 !CAN_WRITE(conn)) ||
2314 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2315 smb_fname,
2316 false,
2317 can_access_mask))) {
2318 can_access = False;
2322 * If we're returning a share violation, ensure we
2323 * cope with the braindead 1 second delay.
2326 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2327 lp_defer_sharing_violations()) {
2328 struct timeval timeout;
2329 struct deferred_open_record state;
2330 int timeout_usecs;
2332 /* this is a hack to speed up torture tests
2333 in 'make test' */
2334 timeout_usecs = lp_parm_int(SNUM(conn),
2335 "smbd","sharedelay",
2336 SHARING_VIOLATION_USEC_WAIT);
2338 /* This is a relative time, added to the absolute
2339 request_time value to get the absolute timeout time.
2340 Note that if this is the second or greater time we enter
2341 this codepath for this particular request mid then
2342 request_time is left as the absolute time of the *first*
2343 time this request mid was processed. This is what allows
2344 the request to eventually time out. */
2346 timeout = timeval_set(0, timeout_usecs);
2348 /* Nothing actually uses state.delayed_for_oplocks
2349 but it's handy to differentiate in debug messages
2350 between a 30 second delay due to oplock break, and
2351 a 1 second delay for share mode conflicts. */
2353 state.delayed_for_oplocks = False;
2354 state.async_open = false;
2355 state.id = id;
2357 if ((req != NULL)
2358 && !request_timed_out(request_time,
2359 timeout)) {
2360 defer_open(lck, request_time, timeout,
2361 req, &state);
2365 TALLOC_FREE(lck);
2366 if (can_access) {
2368 * We have detected a sharing violation here
2369 * so return the correct error code
2371 status = NT_STATUS_SHARING_VIOLATION;
2372 } else {
2373 status = NT_STATUS_ACCESS_DENIED;
2375 return status;
2379 * We exit this block with the share entry *locked*.....
2383 SMB_ASSERT(!file_existed || (lck != NULL));
2386 * Ensure we pay attention to default ACLs on directories if required.
2389 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2390 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2391 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2394 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2395 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2396 (unsigned int)flags, (unsigned int)flags2,
2397 (unsigned int)unx_mode, (unsigned int)access_mask,
2398 (unsigned int)open_access_mask));
2400 fsp_open = open_file(fsp, conn, req, parent_dir,
2401 flags|flags2, unx_mode, access_mask,
2402 open_access_mask, &new_file_created);
2404 if (!NT_STATUS_IS_OK(fsp_open)) {
2405 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2406 schedule_async_open(request_time, req);
2408 TALLOC_FREE(lck);
2409 return fsp_open;
2412 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2414 * The file did exist, but some other (local or NFS)
2415 * process either renamed/unlinked and re-created the
2416 * file with different dev/ino after we walked the path,
2417 * but before we did the open. We could retry the
2418 * open but it's a rare enough case it's easier to
2419 * just fail the open to prevent creating any problems
2420 * in the open file db having the wrong dev/ino key.
2422 TALLOC_FREE(lck);
2423 fd_close(fsp);
2424 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2425 "Old (dev=0x%llu, ino =0x%llu). "
2426 "New (dev=0x%llu, ino=0x%llu). Failing open "
2427 " with NT_STATUS_ACCESS_DENIED.\n",
2428 smb_fname_str_dbg(smb_fname),
2429 (unsigned long long)saved_stat.st_ex_dev,
2430 (unsigned long long)saved_stat.st_ex_ino,
2431 (unsigned long long)smb_fname->st.st_ex_dev,
2432 (unsigned long long)smb_fname->st.st_ex_ino));
2433 return NT_STATUS_ACCESS_DENIED;
2436 if (!file_existed) {
2437 struct share_mode_entry *batch_entry = NULL;
2438 struct share_mode_entry *exclusive_entry = NULL;
2439 bool got_level2_oplock = false;
2440 bool got_a_none_oplock = false;
2441 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2442 struct file_id id;
2444 * Deal with the race condition where two smbd's detect the
2445 * file doesn't exist and do the create at the same time. One
2446 * of them will win and set a share mode, the other (ie. this
2447 * one) should check if the requested share mode for this
2448 * create is allowed.
2452 * Now the file exists and fsp is successfully opened,
2453 * fsp->dev and fsp->inode are valid and should replace the
2454 * dev=0,inode=0 from a non existent file. Spotted by
2455 * Nadav Danieli <nadavd@exanet.com>. JRA.
2458 id = fsp->file_id;
2460 lck = get_share_mode_lock(talloc_tos(), id,
2461 conn->connectpath,
2462 smb_fname, &old_write_time);
2464 if (lck == NULL) {
2465 DEBUG(0, ("open_file_ntcreate: Could not get share "
2466 "mode lock for %s\n",
2467 smb_fname_str_dbg(smb_fname)));
2468 fd_close(fsp);
2469 return NT_STATUS_SHARING_VIOLATION;
2472 /* Get the types we need to examine. */
2473 find_oplock_types(fsp,
2474 oplock_request,
2475 lck,
2476 &batch_entry,
2477 &exclusive_entry,
2478 &got_level2_oplock,
2479 &got_a_none_oplock);
2481 /* First pass - send break only on batch oplocks. */
2482 if ((req != NULL) &&
2483 delay_for_batch_oplocks(fsp,
2484 req->mid,
2485 oplock_request,
2486 batch_entry)) {
2487 schedule_defer_open(lck, request_time, req);
2488 TALLOC_FREE(lck);
2489 fd_close(fsp);
2490 return NT_STATUS_SHARING_VIOLATION;
2493 status = open_mode_check(conn, lck, fsp->name_hash,
2494 access_mask, share_access,
2495 create_options, &file_existed);
2497 if (NT_STATUS_IS_OK(status)) {
2498 /* We might be going to allow this open. Check oplock
2499 * status again. */
2500 /* Second pass - send break for both batch or
2501 * exclusive oplocks. */
2502 if ((req != NULL) &&
2503 delay_for_exclusive_oplocks(
2504 fsp,
2505 req->mid,
2506 oplock_request,
2507 exclusive_entry)) {
2508 schedule_defer_open(lck, request_time, req);
2509 TALLOC_FREE(lck);
2510 fd_close(fsp);
2511 return NT_STATUS_SHARING_VIOLATION;
2515 if (!NT_STATUS_IS_OK(status)) {
2516 struct deferred_open_record state;
2518 state.delayed_for_oplocks = False;
2519 state.async_open = false;
2520 state.id = id;
2522 /* Do it all over again immediately. In the second
2523 * round we will find that the file existed and handle
2524 * the DELETE_PENDING and FCB cases correctly. No need
2525 * to duplicate the code here. Essentially this is a
2526 * "goto top of this function", but don't tell
2527 * anybody... */
2529 if (req != NULL) {
2530 defer_open(lck, request_time, timeval_zero(),
2531 req, &state);
2533 TALLOC_FREE(lck);
2534 fd_close(fsp);
2535 return status;
2538 grant_fsp_oplock_type(fsp,
2539 oplock_request,
2540 got_level2_oplock,
2541 got_a_none_oplock);
2544 * We exit this block with the share entry *locked*.....
2549 SMB_ASSERT(lck != NULL);
2551 /* Delete streams if create_disposition requires it */
2552 if (!new_file_created && clear_ads(create_disposition) &&
2553 !is_ntfs_stream_smb_fname(smb_fname)) {
2554 status = delete_all_streams(conn, smb_fname->base_name);
2555 if (!NT_STATUS_IS_OK(status)) {
2556 TALLOC_FREE(lck);
2557 fd_close(fsp);
2558 return status;
2562 /* note that we ignore failure for the following. It is
2563 basically a hack for NFS, and NFS will never set one of
2564 these only read them. Nobody but Samba can ever set a deny
2565 mode and we have already checked our more authoritative
2566 locking database for permission to set this deny mode. If
2567 the kernel refuses the operations then the kernel is wrong.
2568 note that GPFS supports it as well - jmcd */
2570 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
2571 int ret_flock;
2572 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2573 if(ret_flock == -1 ){
2575 TALLOC_FREE(lck);
2576 fd_close(fsp);
2578 return NT_STATUS_SHARING_VIOLATION;
2583 * At this point onwards, we can guarentee that the share entry
2584 * is locked, whether we created the file or not, and that the
2585 * deny mode is compatible with all current opens.
2589 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2590 * but we don't have to store this - just ignore it on access check.
2592 if (conn->sconn->using_smb2) {
2594 * SMB2 doesn't return it (according to Microsoft tests).
2595 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2596 * File created with access = 0x7 (Read, Write, Delete)
2597 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2599 fsp->access_mask = access_mask;
2600 } else {
2601 /* But SMB1 does. */
2602 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2605 if (file_existed) {
2606 /* stat opens on existing files don't get oplocks. */
2607 if (is_stat_open(open_access_mask)) {
2608 fsp->oplock_type = NO_OPLOCK;
2612 if (new_file_created) {
2613 info = FILE_WAS_CREATED;
2614 } else {
2615 if (flags2 & O_TRUNC) {
2616 info = FILE_WAS_OVERWRITTEN;
2617 } else {
2618 info = FILE_WAS_OPENED;
2622 if (pinfo) {
2623 *pinfo = info;
2627 * Setup the oplock info in both the shared memory and
2628 * file structs.
2631 status = set_file_oplock(fsp, fsp->oplock_type);
2632 if (!NT_STATUS_IS_OK(status)) {
2634 * Could not get the kernel oplock or there are byte-range
2635 * locks on the file.
2637 fsp->oplock_type = NO_OPLOCK;
2640 set_share_mode(lck, fsp, get_current_uid(conn),
2641 req ? req->mid : 0,
2642 fsp->oplock_type);
2644 /* Handle strange delete on close create semantics. */
2645 if (create_options & FILE_DELETE_ON_CLOSE) {
2647 status = can_set_delete_on_close(fsp, new_dos_attributes);
2649 if (!NT_STATUS_IS_OK(status)) {
2650 /* Remember to delete the mode we just added. */
2651 del_share_mode(lck, fsp);
2652 TALLOC_FREE(lck);
2653 fd_close(fsp);
2654 return status;
2656 /* Note that here we set the *inital* delete on close flag,
2657 not the regular one. The magic gets handled in close. */
2658 fsp->initial_delete_on_close = True;
2661 if (info != FILE_WAS_OPENED) {
2662 /* Files should be initially set as archive */
2663 if (lp_map_archive(SNUM(conn)) ||
2664 lp_store_dos_attributes(SNUM(conn))) {
2665 if (!posix_open) {
2666 if (file_set_dosmode(conn, smb_fname,
2667 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2668 parent_dir, true) == 0) {
2669 unx_mode = smb_fname->st.st_ex_mode;
2675 /* Determine sparse flag. */
2676 if (posix_open) {
2677 /* POSIX opens are sparse by default. */
2678 fsp->is_sparse = true;
2679 } else {
2680 fsp->is_sparse = (file_existed &&
2681 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2685 * Take care of inherited ACLs on created files - if default ACL not
2686 * selected.
2689 if (!posix_open && new_file_created && !def_acl) {
2691 int saved_errno = errno; /* We might get ENOSYS in the next
2692 * call.. */
2694 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2695 errno == ENOSYS) {
2696 errno = saved_errno; /* Ignore ENOSYS */
2699 } else if (new_unx_mode) {
2701 int ret = -1;
2703 /* Attributes need changing. File already existed. */
2706 int saved_errno = errno; /* We might get ENOSYS in the
2707 * next call.. */
2708 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2710 if (ret == -1 && errno == ENOSYS) {
2711 errno = saved_errno; /* Ignore ENOSYS */
2712 } else {
2713 DEBUG(5, ("open_file_ntcreate: reset "
2714 "attributes of file %s to 0%o\n",
2715 smb_fname_str_dbg(smb_fname),
2716 (unsigned int)new_unx_mode));
2717 ret = 0; /* Don't do the fchmod below. */
2721 if ((ret == -1) &&
2722 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2723 DEBUG(5, ("open_file_ntcreate: failed to reset "
2724 "attributes of file %s to 0%o\n",
2725 smb_fname_str_dbg(smb_fname),
2726 (unsigned int)new_unx_mode));
2729 /* If this is a successful open, we must remove any deferred open
2730 * records. */
2731 if (req != NULL) {
2732 del_deferred_open_entry(lck, req->mid,
2733 messaging_server_id(req->sconn->msg_ctx));
2735 TALLOC_FREE(lck);
2737 return NT_STATUS_OK;
2741 /****************************************************************************
2742 Open a file for for write to ensure that we can fchmod it.
2743 ****************************************************************************/
2745 NTSTATUS open_file_fchmod(connection_struct *conn,
2746 struct smb_filename *smb_fname,
2747 files_struct **result)
2749 if (!VALID_STAT(smb_fname->st)) {
2750 return NT_STATUS_INVALID_PARAMETER;
2753 return SMB_VFS_CREATE_FILE(
2754 conn, /* conn */
2755 NULL, /* req */
2756 0, /* root_dir_fid */
2757 smb_fname, /* fname */
2758 FILE_WRITE_DATA, /* access_mask */
2759 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2760 FILE_SHARE_DELETE),
2761 FILE_OPEN, /* create_disposition*/
2762 0, /* create_options */
2763 0, /* file_attributes */
2764 INTERNAL_OPEN_ONLY, /* oplock_request */
2765 0, /* allocation_size */
2766 0, /* private_flags */
2767 NULL, /* sd */
2768 NULL, /* ea_list */
2769 result, /* result */
2770 NULL); /* pinfo */
2773 static NTSTATUS mkdir_internal(connection_struct *conn,
2774 struct smb_filename *smb_dname,
2775 uint32 file_attributes)
2777 mode_t mode;
2778 char *parent_dir = NULL;
2779 NTSTATUS status;
2780 bool posix_open = false;
2781 bool need_re_stat = false;
2782 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2784 if(access_mask & ~(conn->share_access)) {
2785 DEBUG(5,("mkdir_internal: failing share access "
2786 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
2787 return NT_STATUS_ACCESS_DENIED;
2790 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2791 NULL)) {
2792 return NT_STATUS_NO_MEMORY;
2795 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2796 posix_open = true;
2797 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2798 } else {
2799 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2802 status = check_parent_access(conn,
2803 smb_dname,
2804 access_mask);
2805 if(!NT_STATUS_IS_OK(status)) {
2806 DEBUG(5,("mkdir_internal: check_parent_access "
2807 "on directory %s for path %s returned %s\n",
2808 parent_dir,
2809 smb_dname->base_name,
2810 nt_errstr(status) ));
2811 return status;
2814 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2815 return map_nt_error_from_unix(errno);
2818 /* Ensure we're checking for a symlink here.... */
2819 /* We don't want to get caught by a symlink racer. */
2821 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2822 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2823 smb_fname_str_dbg(smb_dname), strerror(errno)));
2824 return map_nt_error_from_unix(errno);
2827 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2828 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2829 smb_fname_str_dbg(smb_dname)));
2830 return NT_STATUS_NOT_A_DIRECTORY;
2833 if (lp_store_dos_attributes(SNUM(conn))) {
2834 if (!posix_open) {
2835 file_set_dosmode(conn, smb_dname,
2836 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2837 parent_dir, true);
2841 if (lp_inherit_perms(SNUM(conn))) {
2842 inherit_access_posix_acl(conn, parent_dir,
2843 smb_dname->base_name, mode);
2844 need_re_stat = true;
2847 if (!posix_open) {
2849 * Check if high bits should have been set,
2850 * then (if bits are missing): add them.
2851 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2852 * dir.
2854 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2855 (mode & ~smb_dname->st.st_ex_mode)) {
2856 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2857 (smb_dname->st.st_ex_mode |
2858 (mode & ~smb_dname->st.st_ex_mode)));
2859 need_re_stat = true;
2863 /* Change the owner if required. */
2864 if (lp_inherit_owner(SNUM(conn))) {
2865 change_dir_owner_to_parent(conn, parent_dir,
2866 smb_dname->base_name,
2867 &smb_dname->st);
2868 need_re_stat = true;
2871 if (need_re_stat) {
2872 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2873 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2874 smb_fname_str_dbg(smb_dname), strerror(errno)));
2875 return map_nt_error_from_unix(errno);
2879 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2880 smb_dname->base_name);
2882 return NT_STATUS_OK;
2885 /****************************************************************************
2886 Open a directory from an NT SMB call.
2887 ****************************************************************************/
2889 static NTSTATUS open_directory(connection_struct *conn,
2890 struct smb_request *req,
2891 struct smb_filename *smb_dname,
2892 uint32 access_mask,
2893 uint32 share_access,
2894 uint32 create_disposition,
2895 uint32 create_options,
2896 uint32 file_attributes,
2897 int *pinfo,
2898 files_struct **result)
2900 files_struct *fsp = NULL;
2901 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2902 struct share_mode_lock *lck = NULL;
2903 NTSTATUS status;
2904 struct timespec mtimespec;
2905 int info = 0;
2907 if (is_ntfs_stream_smb_fname(smb_dname)) {
2908 DEBUG(2, ("open_directory: %s is a stream name!\n",
2909 smb_fname_str_dbg(smb_dname)));
2910 return NT_STATUS_NOT_A_DIRECTORY;
2913 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2914 /* Ensure we have a directory attribute. */
2915 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2918 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2919 "share_access = 0x%x create_options = 0x%x, "
2920 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2921 smb_fname_str_dbg(smb_dname),
2922 (unsigned int)access_mask,
2923 (unsigned int)share_access,
2924 (unsigned int)create_options,
2925 (unsigned int)create_disposition,
2926 (unsigned int)file_attributes));
2928 status = smbd_calculate_access_mask(conn, smb_dname, false,
2929 access_mask, &access_mask);
2930 if (!NT_STATUS_IS_OK(status)) {
2931 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2932 "on file %s returned %s\n",
2933 smb_fname_str_dbg(smb_dname),
2934 nt_errstr(status)));
2935 return status;
2938 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2939 !security_token_has_privilege(get_current_nttok(conn),
2940 SEC_PRIV_SECURITY)) {
2941 DEBUG(10, ("open_directory: open on %s "
2942 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2943 smb_fname_str_dbg(smb_dname)));
2944 return NT_STATUS_PRIVILEGE_NOT_HELD;
2947 switch( create_disposition ) {
2948 case FILE_OPEN:
2950 if (!dir_existed) {
2951 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2954 info = FILE_WAS_OPENED;
2955 break;
2957 case FILE_CREATE:
2959 /* If directory exists error. If directory doesn't
2960 * exist create. */
2962 if (dir_existed) {
2963 status = NT_STATUS_OBJECT_NAME_COLLISION;
2964 DEBUG(2, ("open_directory: unable to create "
2965 "%s. Error was %s\n",
2966 smb_fname_str_dbg(smb_dname),
2967 nt_errstr(status)));
2968 return status;
2971 status = mkdir_internal(conn, smb_dname,
2972 file_attributes);
2974 if (!NT_STATUS_IS_OK(status)) {
2975 DEBUG(2, ("open_directory: unable to create "
2976 "%s. Error was %s\n",
2977 smb_fname_str_dbg(smb_dname),
2978 nt_errstr(status)));
2979 return status;
2982 info = FILE_WAS_CREATED;
2983 break;
2985 case FILE_OPEN_IF:
2987 * If directory exists open. If directory doesn't
2988 * exist create.
2991 if (dir_existed) {
2992 status = NT_STATUS_OK;
2993 info = FILE_WAS_OPENED;
2994 } else {
2995 status = mkdir_internal(conn, smb_dname,
2996 file_attributes);
2998 if (NT_STATUS_IS_OK(status)) {
2999 info = FILE_WAS_CREATED;
3000 } else {
3001 /* Cope with create race. */
3002 if (!NT_STATUS_EQUAL(status,
3003 NT_STATUS_OBJECT_NAME_COLLISION)) {
3004 DEBUG(2, ("open_directory: unable to create "
3005 "%s. Error was %s\n",
3006 smb_fname_str_dbg(smb_dname),
3007 nt_errstr(status)));
3008 return status;
3010 info = FILE_WAS_OPENED;
3014 break;
3016 case FILE_SUPERSEDE:
3017 case FILE_OVERWRITE:
3018 case FILE_OVERWRITE_IF:
3019 default:
3020 DEBUG(5,("open_directory: invalid create_disposition "
3021 "0x%x for directory %s\n",
3022 (unsigned int)create_disposition,
3023 smb_fname_str_dbg(smb_dname)));
3024 return NT_STATUS_INVALID_PARAMETER;
3027 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3028 DEBUG(5,("open_directory: %s is not a directory !\n",
3029 smb_fname_str_dbg(smb_dname)));
3030 return NT_STATUS_NOT_A_DIRECTORY;
3033 if (info == FILE_WAS_OPENED) {
3034 status = smbd_check_access_rights(conn,
3035 smb_dname,
3036 false,
3037 access_mask);
3038 if (!NT_STATUS_IS_OK(status)) {
3039 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3040 "file %s failed with %s\n",
3041 smb_fname_str_dbg(smb_dname),
3042 nt_errstr(status)));
3043 return status;
3047 status = file_new(req, conn, &fsp);
3048 if(!NT_STATUS_IS_OK(status)) {
3049 return status;
3053 * Setup the files_struct for it.
3056 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3057 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3058 fsp->file_pid = req ? req->smbpid : 0;
3059 fsp->can_lock = False;
3060 fsp->can_read = False;
3061 fsp->can_write = False;
3063 fsp->share_access = share_access;
3064 fsp->fh->private_options = 0;
3066 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3068 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3069 fsp->print_file = NULL;
3070 fsp->modified = False;
3071 fsp->oplock_type = NO_OPLOCK;
3072 fsp->sent_oplock_break = NO_BREAK_SENT;
3073 fsp->is_directory = True;
3074 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
3075 status = fsp_set_smb_fname(fsp, smb_dname);
3076 if (!NT_STATUS_IS_OK(status)) {
3077 file_free(req, fsp);
3078 return status;
3081 mtimespec = smb_dname->st.st_ex_mtime;
3083 #ifdef O_DIRECTORY
3084 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3085 #else
3086 /* POSIX allows us to open a directory with O_RDONLY. */
3087 status = fd_open(conn, fsp, O_RDONLY, 0);
3088 #endif
3089 if (!NT_STATUS_IS_OK(status)) {
3090 DEBUG(5, ("open_directory: Could not open fd for "
3091 "%s (%s)\n",
3092 smb_fname_str_dbg(smb_dname),
3093 nt_errstr(status)));
3094 file_free(req, fsp);
3095 return status;
3098 status = vfs_stat_fsp(fsp);
3099 if (!NT_STATUS_IS_OK(status)) {
3100 fd_close(fsp);
3101 file_free(req, fsp);
3102 return status;
3105 /* Ensure there was no race condition. */
3106 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
3107 DEBUG(5,("open_directory: stat struct differs for "
3108 "directory %s.\n",
3109 smb_fname_str_dbg(smb_dname)));
3110 fd_close(fsp);
3111 file_free(req, fsp);
3112 return NT_STATUS_ACCESS_DENIED;
3115 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3116 conn->connectpath, smb_dname,
3117 &mtimespec);
3119 if (lck == NULL) {
3120 DEBUG(0, ("open_directory: Could not get share mode lock for "
3121 "%s\n", smb_fname_str_dbg(smb_dname)));
3122 fd_close(fsp);
3123 file_free(req, fsp);
3124 return NT_STATUS_SHARING_VIOLATION;
3127 status = open_mode_check(conn, lck, fsp->name_hash,
3128 access_mask, share_access,
3129 create_options, &dir_existed);
3131 if (!NT_STATUS_IS_OK(status)) {
3132 TALLOC_FREE(lck);
3133 fd_close(fsp);
3134 file_free(req, fsp);
3135 return status;
3138 set_share_mode(lck, fsp, get_current_uid(conn),
3139 req ? req->mid : 0, NO_OPLOCK);
3141 /* For directories the delete on close bit at open time seems
3142 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3143 if (create_options & FILE_DELETE_ON_CLOSE) {
3144 status = can_set_delete_on_close(fsp, 0);
3145 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3146 TALLOC_FREE(lck);
3147 fd_close(fsp);
3148 file_free(req, fsp);
3149 return status;
3152 if (NT_STATUS_IS_OK(status)) {
3153 /* Note that here we set the *inital* delete on close flag,
3154 not the regular one. The magic gets handled in close. */
3155 fsp->initial_delete_on_close = True;
3159 TALLOC_FREE(lck);
3161 if (pinfo) {
3162 *pinfo = info;
3165 *result = fsp;
3166 return NT_STATUS_OK;
3169 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3170 struct smb_filename *smb_dname)
3172 NTSTATUS status;
3173 files_struct *fsp;
3175 status = SMB_VFS_CREATE_FILE(
3176 conn, /* conn */
3177 req, /* req */
3178 0, /* root_dir_fid */
3179 smb_dname, /* fname */
3180 FILE_READ_ATTRIBUTES, /* access_mask */
3181 FILE_SHARE_NONE, /* share_access */
3182 FILE_CREATE, /* create_disposition*/
3183 FILE_DIRECTORY_FILE, /* create_options */
3184 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3185 0, /* oplock_request */
3186 0, /* allocation_size */
3187 0, /* private_flags */
3188 NULL, /* sd */
3189 NULL, /* ea_list */
3190 &fsp, /* result */
3191 NULL); /* pinfo */
3193 if (NT_STATUS_IS_OK(status)) {
3194 close_file(req, fsp, NORMAL_CLOSE);
3197 return status;
3200 /****************************************************************************
3201 Receive notification that one of our open files has been renamed by another
3202 smbd process.
3203 ****************************************************************************/
3205 void msg_file_was_renamed(struct messaging_context *msg,
3206 void *private_data,
3207 uint32_t msg_type,
3208 struct server_id server_id,
3209 DATA_BLOB *data)
3211 files_struct *fsp;
3212 char *frm = (char *)data->data;
3213 struct file_id id;
3214 const char *sharepath;
3215 const char *base_name;
3216 const char *stream_name;
3217 struct smb_filename *smb_fname = NULL;
3218 size_t sp_len, bn_len;
3219 NTSTATUS status;
3220 struct smbd_server_connection *sconn =
3221 talloc_get_type_abort(private_data,
3222 struct smbd_server_connection);
3224 if (data->data == NULL
3225 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3226 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3227 (int)data->length));
3228 return;
3231 /* Unpack the message. */
3232 pull_file_id_24(frm, &id);
3233 sharepath = &frm[24];
3234 sp_len = strlen(sharepath);
3235 base_name = sharepath + sp_len + 1;
3236 bn_len = strlen(base_name);
3237 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3239 /* stream_name must always be NULL if there is no stream. */
3240 if (stream_name[0] == '\0') {
3241 stream_name = NULL;
3244 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3245 stream_name, NULL, &smb_fname);
3246 if (!NT_STATUS_IS_OK(status)) {
3247 return;
3250 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3251 "file_id %s\n",
3252 sharepath, smb_fname_str_dbg(smb_fname),
3253 file_id_string_tos(&id)));
3255 for(fsp = file_find_di_first(sconn, id); fsp;
3256 fsp = file_find_di_next(fsp)) {
3257 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3259 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3260 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3261 smb_fname_str_dbg(smb_fname)));
3262 status = fsp_set_smb_fname(fsp, smb_fname);
3263 if (!NT_STATUS_IS_OK(status)) {
3264 goto out;
3266 } else {
3267 /* TODO. JRA. */
3268 /* Now we have the complete path we can work out if this is
3269 actually within this share and adjust newname accordingly. */
3270 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3271 "not sharepath %s) "
3272 "%s from %s -> %s\n",
3273 fsp->conn->connectpath,
3274 sharepath,
3275 fsp_fnum_dbg(fsp),
3276 fsp_str_dbg(fsp),
3277 smb_fname_str_dbg(smb_fname)));
3280 out:
3281 TALLOC_FREE(smb_fname);
3282 return;
3286 * If a main file is opened for delete, all streams need to be checked for
3287 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3288 * If that works, delete them all by setting the delete on close and close.
3291 NTSTATUS open_streams_for_delete(connection_struct *conn,
3292 const char *fname)
3294 struct stream_struct *stream_info = NULL;
3295 files_struct **streams = NULL;
3296 int i;
3297 unsigned int num_streams = 0;
3298 TALLOC_CTX *frame = talloc_stackframe();
3299 NTSTATUS status;
3301 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3302 &num_streams, &stream_info);
3304 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3305 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3306 DEBUG(10, ("no streams around\n"));
3307 TALLOC_FREE(frame);
3308 return NT_STATUS_OK;
3311 if (!NT_STATUS_IS_OK(status)) {
3312 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3313 nt_errstr(status)));
3314 goto fail;
3317 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3318 num_streams));
3320 if (num_streams == 0) {
3321 TALLOC_FREE(frame);
3322 return NT_STATUS_OK;
3325 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3326 if (streams == NULL) {
3327 DEBUG(0, ("talloc failed\n"));
3328 status = NT_STATUS_NO_MEMORY;
3329 goto fail;
3332 for (i=0; i<num_streams; i++) {
3333 struct smb_filename *smb_fname = NULL;
3335 if (strequal(stream_info[i].name, "::$DATA")) {
3336 streams[i] = NULL;
3337 continue;
3340 status = create_synthetic_smb_fname(talloc_tos(), fname,
3341 stream_info[i].name,
3342 NULL, &smb_fname);
3343 if (!NT_STATUS_IS_OK(status)) {
3344 goto fail;
3347 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3348 DEBUG(10, ("Unable to stat stream: %s\n",
3349 smb_fname_str_dbg(smb_fname)));
3352 status = SMB_VFS_CREATE_FILE(
3353 conn, /* conn */
3354 NULL, /* req */
3355 0, /* root_dir_fid */
3356 smb_fname, /* fname */
3357 DELETE_ACCESS, /* access_mask */
3358 (FILE_SHARE_READ | /* share_access */
3359 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3360 FILE_OPEN, /* create_disposition*/
3361 0, /* create_options */
3362 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3363 0, /* oplock_request */
3364 0, /* allocation_size */
3365 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3366 NULL, /* sd */
3367 NULL, /* ea_list */
3368 &streams[i], /* result */
3369 NULL); /* pinfo */
3371 if (!NT_STATUS_IS_OK(status)) {
3372 DEBUG(10, ("Could not open stream %s: %s\n",
3373 smb_fname_str_dbg(smb_fname),
3374 nt_errstr(status)));
3376 TALLOC_FREE(smb_fname);
3377 break;
3379 TALLOC_FREE(smb_fname);
3383 * don't touch the variable "status" beyond this point :-)
3386 for (i -= 1 ; i >= 0; i--) {
3387 if (streams[i] == NULL) {
3388 continue;
3391 DEBUG(10, ("Closing stream # %d, %s\n", i,
3392 fsp_str_dbg(streams[i])));
3393 close_file(NULL, streams[i], NORMAL_CLOSE);
3396 fail:
3397 TALLOC_FREE(frame);
3398 return status;
3401 /*********************************************************************
3402 Create a default ACL by inheriting from the parent. If no inheritance
3403 from the parent available, don't set anything. This will leave the actual
3404 permissions the new file or directory already got from the filesystem
3405 as the NT ACL when read.
3406 *********************************************************************/
3408 static NTSTATUS inherit_new_acl(files_struct *fsp)
3410 TALLOC_CTX *ctx = talloc_tos();
3411 char *parent_name = NULL;
3412 struct security_descriptor *parent_desc = NULL;
3413 NTSTATUS status = NT_STATUS_OK;
3414 struct security_descriptor *psd = NULL;
3415 struct dom_sid *owner_sid = NULL;
3416 struct dom_sid *group_sid = NULL;
3417 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3418 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3419 bool inheritable_components = false;
3420 size_t size = 0;
3422 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3423 return NT_STATUS_NO_MEMORY;
3426 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3427 parent_name,
3428 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3429 &parent_desc);
3430 if (!NT_STATUS_IS_OK(status)) {
3431 return status;
3434 inheritable_components = sd_has_inheritable_components(parent_desc,
3435 fsp->is_directory);
3437 if (!inheritable_components && !inherit_owner) {
3438 /* Nothing to inherit and not setting owner. */
3439 return NT_STATUS_OK;
3442 /* Create an inherited descriptor from the parent. */
3444 if (DEBUGLEVEL >= 10) {
3445 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3446 fsp_str_dbg(fsp) ));
3447 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3450 /* Inherit from parent descriptor if "inherit owner" set. */
3451 if (inherit_owner) {
3452 owner_sid = parent_desc->owner_sid;
3453 group_sid = parent_desc->group_sid;
3456 if (owner_sid == NULL) {
3457 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3459 if (group_sid == NULL) {
3460 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3463 status = se_create_child_secdesc(ctx,
3464 &psd,
3465 &size,
3466 parent_desc,
3467 owner_sid,
3468 group_sid,
3469 fsp->is_directory);
3470 if (!NT_STATUS_IS_OK(status)) {
3471 return status;
3474 /* If inheritable_components == false,
3475 se_create_child_secdesc()
3476 creates a security desriptor with a NULL dacl
3477 entry, but with SEC_DESC_DACL_PRESENT. We need
3478 to remove that flag. */
3480 if (!inheritable_components) {
3481 security_info_sent &= ~SECINFO_DACL;
3482 psd->type &= ~SEC_DESC_DACL_PRESENT;
3485 if (DEBUGLEVEL >= 10) {
3486 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3487 fsp_str_dbg(fsp) ));
3488 NDR_PRINT_DEBUG(security_descriptor, psd);
3491 if (inherit_owner) {
3492 /* We need to be root to force this. */
3493 become_root();
3495 status = SMB_VFS_FSET_NT_ACL(fsp,
3496 security_info_sent,
3497 psd);
3498 if (inherit_owner) {
3499 unbecome_root();
3501 return status;
3505 * Wrapper around open_file_ntcreate and open_directory
3508 static NTSTATUS create_file_unixpath(connection_struct *conn,
3509 struct smb_request *req,
3510 struct smb_filename *smb_fname,
3511 uint32_t access_mask,
3512 uint32_t share_access,
3513 uint32_t create_disposition,
3514 uint32_t create_options,
3515 uint32_t file_attributes,
3516 uint32_t oplock_request,
3517 uint64_t allocation_size,
3518 uint32_t private_flags,
3519 struct security_descriptor *sd,
3520 struct ea_list *ea_list,
3522 files_struct **result,
3523 int *pinfo)
3525 int info = FILE_WAS_OPENED;
3526 files_struct *base_fsp = NULL;
3527 files_struct *fsp = NULL;
3528 NTSTATUS status;
3530 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3531 "file_attributes = 0x%x, share_access = 0x%x, "
3532 "create_disposition = 0x%x create_options = 0x%x "
3533 "oplock_request = 0x%x private_flags = 0x%x "
3534 "ea_list = 0x%p, sd = 0x%p, "
3535 "fname = %s\n",
3536 (unsigned int)access_mask,
3537 (unsigned int)file_attributes,
3538 (unsigned int)share_access,
3539 (unsigned int)create_disposition,
3540 (unsigned int)create_options,
3541 (unsigned int)oplock_request,
3542 (unsigned int)private_flags,
3543 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3545 if (create_options & FILE_OPEN_BY_FILE_ID) {
3546 status = NT_STATUS_NOT_SUPPORTED;
3547 goto fail;
3550 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3551 status = NT_STATUS_INVALID_PARAMETER;
3552 goto fail;
3555 if (req == NULL) {
3556 oplock_request |= INTERNAL_OPEN_ONLY;
3559 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3560 && (access_mask & DELETE_ACCESS)
3561 && !is_ntfs_stream_smb_fname(smb_fname)) {
3563 * We can't open a file with DELETE access if any of the
3564 * streams is open without FILE_SHARE_DELETE
3566 status = open_streams_for_delete(conn, smb_fname->base_name);
3568 if (!NT_STATUS_IS_OK(status)) {
3569 goto fail;
3573 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3574 !security_token_has_privilege(get_current_nttok(conn),
3575 SEC_PRIV_SECURITY)) {
3576 DEBUG(10, ("create_file_unixpath: open on %s "
3577 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3578 smb_fname_str_dbg(smb_fname)));
3579 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3580 goto fail;
3583 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3584 && is_ntfs_stream_smb_fname(smb_fname)
3585 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3586 uint32 base_create_disposition;
3587 struct smb_filename *smb_fname_base = NULL;
3589 if (create_options & FILE_DIRECTORY_FILE) {
3590 status = NT_STATUS_NOT_A_DIRECTORY;
3591 goto fail;
3594 switch (create_disposition) {
3595 case FILE_OPEN:
3596 base_create_disposition = FILE_OPEN;
3597 break;
3598 default:
3599 base_create_disposition = FILE_OPEN_IF;
3600 break;
3603 /* Create an smb_filename with stream_name == NULL. */
3604 status = create_synthetic_smb_fname(talloc_tos(),
3605 smb_fname->base_name,
3606 NULL, NULL,
3607 &smb_fname_base);
3608 if (!NT_STATUS_IS_OK(status)) {
3609 goto fail;
3612 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3613 DEBUG(10, ("Unable to stat stream: %s\n",
3614 smb_fname_str_dbg(smb_fname_base)));
3617 /* Open the base file. */
3618 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3619 FILE_SHARE_READ
3620 | FILE_SHARE_WRITE
3621 | FILE_SHARE_DELETE,
3622 base_create_disposition,
3623 0, 0, 0, 0, 0, NULL, NULL,
3624 &base_fsp, NULL);
3625 TALLOC_FREE(smb_fname_base);
3627 if (!NT_STATUS_IS_OK(status)) {
3628 DEBUG(10, ("create_file_unixpath for base %s failed: "
3629 "%s\n", smb_fname->base_name,
3630 nt_errstr(status)));
3631 goto fail;
3633 /* we don't need to low level fd */
3634 fd_close(base_fsp);
3638 * If it's a request for a directory open, deal with it separately.
3641 if (create_options & FILE_DIRECTORY_FILE) {
3643 if (create_options & FILE_NON_DIRECTORY_FILE) {
3644 status = NT_STATUS_INVALID_PARAMETER;
3645 goto fail;
3648 /* Can't open a temp directory. IFS kit test. */
3649 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3650 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3651 status = NT_STATUS_INVALID_PARAMETER;
3652 goto fail;
3656 * We will get a create directory here if the Win32
3657 * app specified a security descriptor in the
3658 * CreateDirectory() call.
3661 oplock_request = 0;
3662 status = open_directory(
3663 conn, req, smb_fname, access_mask, share_access,
3664 create_disposition, create_options, file_attributes,
3665 &info, &fsp);
3666 } else {
3669 * Ordinary file case.
3672 status = file_new(req, conn, &fsp);
3673 if(!NT_STATUS_IS_OK(status)) {
3674 goto fail;
3677 status = fsp_set_smb_fname(fsp, smb_fname);
3678 if (!NT_STATUS_IS_OK(status)) {
3679 goto fail;
3682 if (base_fsp) {
3684 * We're opening the stream element of a
3685 * base_fsp we already opened. Set up the
3686 * base_fsp pointer.
3688 fsp->base_fsp = base_fsp;
3691 if (allocation_size) {
3692 fsp->initial_allocation_size = smb_roundup(fsp->conn,
3693 allocation_size);
3696 status = open_file_ntcreate(conn,
3697 req,
3698 access_mask,
3699 share_access,
3700 create_disposition,
3701 create_options,
3702 file_attributes,
3703 oplock_request,
3704 private_flags,
3705 &info,
3706 fsp);
3708 if(!NT_STATUS_IS_OK(status)) {
3709 file_free(req, fsp);
3710 fsp = NULL;
3713 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3715 /* A stream open never opens a directory */
3717 if (base_fsp) {
3718 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3719 goto fail;
3723 * Fail the open if it was explicitly a non-directory
3724 * file.
3727 if (create_options & FILE_NON_DIRECTORY_FILE) {
3728 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3729 goto fail;
3732 oplock_request = 0;
3733 status = open_directory(
3734 conn, req, smb_fname, access_mask,
3735 share_access, create_disposition,
3736 create_options, file_attributes,
3737 &info, &fsp);
3741 if (!NT_STATUS_IS_OK(status)) {
3742 goto fail;
3745 fsp->base_fsp = base_fsp;
3747 if ((ea_list != NULL) &&
3748 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3749 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3750 if (!NT_STATUS_IS_OK(status)) {
3751 goto fail;
3755 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3756 status = NT_STATUS_ACCESS_DENIED;
3757 goto fail;
3760 /* Save the requested allocation size. */
3761 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3762 if (allocation_size
3763 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3764 fsp->initial_allocation_size = smb_roundup(
3765 fsp->conn, allocation_size);
3766 if (fsp->is_directory) {
3767 /* Can't set allocation size on a directory. */
3768 status = NT_STATUS_ACCESS_DENIED;
3769 goto fail;
3771 if (vfs_allocate_file_space(
3772 fsp, fsp->initial_allocation_size) == -1) {
3773 status = NT_STATUS_DISK_FULL;
3774 goto fail;
3776 } else {
3777 fsp->initial_allocation_size = smb_roundup(
3778 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3780 } else {
3781 fsp->initial_allocation_size = 0;
3784 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3785 fsp->base_fsp == NULL) {
3786 if (sd != NULL) {
3788 * According to the MS documentation, the only time the security
3789 * descriptor is applied to the opened file is iff we *created* the
3790 * file; an existing file stays the same.
3792 * Also, it seems (from observation) that you can open the file with
3793 * any access mask but you can still write the sd. We need to override
3794 * the granted access before we call set_sd
3795 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3798 uint32_t sec_info_sent;
3799 uint32_t saved_access_mask = fsp->access_mask;
3801 sec_info_sent = get_sec_info(sd);
3803 fsp->access_mask = FILE_GENERIC_ALL;
3805 if (sec_info_sent & (SECINFO_OWNER|
3806 SECINFO_GROUP|
3807 SECINFO_DACL|
3808 SECINFO_SACL)) {
3809 status = set_sd(fsp, sd, sec_info_sent);
3812 fsp->access_mask = saved_access_mask;
3814 if (!NT_STATUS_IS_OK(status)) {
3815 goto fail;
3817 } else if (lp_inherit_acls(SNUM(conn))) {
3818 /* Inherit from parent. Errors here are not fatal. */
3819 status = inherit_new_acl(fsp);
3820 if (!NT_STATUS_IS_OK(status)) {
3821 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3822 fsp_str_dbg(fsp),
3823 nt_errstr(status) ));
3828 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3830 *result = fsp;
3831 if (pinfo != NULL) {
3832 *pinfo = info;
3835 smb_fname->st = fsp->fsp_name->st;
3837 return NT_STATUS_OK;
3839 fail:
3840 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3842 if (fsp != NULL) {
3843 if (base_fsp && fsp->base_fsp == base_fsp) {
3845 * The close_file below will close
3846 * fsp->base_fsp.
3848 base_fsp = NULL;
3850 close_file(req, fsp, ERROR_CLOSE);
3851 fsp = NULL;
3853 if (base_fsp != NULL) {
3854 close_file(req, base_fsp, ERROR_CLOSE);
3855 base_fsp = NULL;
3857 return status;
3861 * Calculate the full path name given a relative fid.
3863 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3864 struct smb_request *req,
3865 uint16_t root_dir_fid,
3866 const struct smb_filename *smb_fname,
3867 struct smb_filename **smb_fname_out)
3869 files_struct *dir_fsp;
3870 char *parent_fname = NULL;
3871 char *new_base_name = NULL;
3872 NTSTATUS status;
3874 if (root_dir_fid == 0 || !smb_fname) {
3875 status = NT_STATUS_INTERNAL_ERROR;
3876 goto out;
3879 dir_fsp = file_fsp(req, root_dir_fid);
3881 if (dir_fsp == NULL) {
3882 status = NT_STATUS_INVALID_HANDLE;
3883 goto out;
3886 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3887 status = NT_STATUS_INVALID_HANDLE;
3888 goto out;
3891 if (!dir_fsp->is_directory) {
3894 * Check to see if this is a mac fork of some kind.
3897 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3898 is_ntfs_stream_smb_fname(smb_fname)) {
3899 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3900 goto out;
3904 we need to handle the case when we get a
3905 relative open relative to a file and the
3906 pathname is blank - this is a reopen!
3907 (hint from demyn plantenberg)
3910 status = NT_STATUS_INVALID_HANDLE;
3911 goto out;
3914 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3916 * We're at the toplevel dir, the final file name
3917 * must not contain ./, as this is filtered out
3918 * normally by srvstr_get_path and unix_convert
3919 * explicitly rejects paths containing ./.
3921 parent_fname = talloc_strdup(talloc_tos(), "");
3922 if (parent_fname == NULL) {
3923 status = NT_STATUS_NO_MEMORY;
3924 goto out;
3926 } else {
3927 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3930 * Copy in the base directory name.
3933 parent_fname = talloc_array(talloc_tos(), char,
3934 dir_name_len+2);
3935 if (parent_fname == NULL) {
3936 status = NT_STATUS_NO_MEMORY;
3937 goto out;
3939 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3940 dir_name_len+1);
3943 * Ensure it ends in a '/'.
3944 * We used TALLOC_SIZE +2 to add space for the '/'.
3947 if(dir_name_len
3948 && (parent_fname[dir_name_len-1] != '\\')
3949 && (parent_fname[dir_name_len-1] != '/')) {
3950 parent_fname[dir_name_len] = '/';
3951 parent_fname[dir_name_len+1] = '\0';
3955 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3956 smb_fname->base_name);
3957 if (new_base_name == NULL) {
3958 status = NT_STATUS_NO_MEMORY;
3959 goto out;
3962 status = filename_convert(req,
3963 conn,
3964 req->flags2 & FLAGS2_DFS_PATHNAMES,
3965 new_base_name,
3967 NULL,
3968 smb_fname_out);
3969 if (!NT_STATUS_IS_OK(status)) {
3970 goto out;
3973 out:
3974 TALLOC_FREE(parent_fname);
3975 TALLOC_FREE(new_base_name);
3976 return status;
3979 NTSTATUS create_file_default(connection_struct *conn,
3980 struct smb_request *req,
3981 uint16_t root_dir_fid,
3982 struct smb_filename *smb_fname,
3983 uint32_t access_mask,
3984 uint32_t share_access,
3985 uint32_t create_disposition,
3986 uint32_t create_options,
3987 uint32_t file_attributes,
3988 uint32_t oplock_request,
3989 uint64_t allocation_size,
3990 uint32_t private_flags,
3991 struct security_descriptor *sd,
3992 struct ea_list *ea_list,
3993 files_struct **result,
3994 int *pinfo)
3996 int info = FILE_WAS_OPENED;
3997 files_struct *fsp = NULL;
3998 NTSTATUS status;
3999 bool stream_name = false;
4001 DEBUG(10,("create_file: access_mask = 0x%x "
4002 "file_attributes = 0x%x, share_access = 0x%x, "
4003 "create_disposition = 0x%x create_options = 0x%x "
4004 "oplock_request = 0x%x "
4005 "private_flags = 0x%x "
4006 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4007 "fname = %s\n",
4008 (unsigned int)access_mask,
4009 (unsigned int)file_attributes,
4010 (unsigned int)share_access,
4011 (unsigned int)create_disposition,
4012 (unsigned int)create_options,
4013 (unsigned int)oplock_request,
4014 (unsigned int)private_flags,
4015 (unsigned int)root_dir_fid,
4016 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4019 * Calculate the filename from the root_dir_if if necessary.
4022 if (root_dir_fid != 0) {
4023 struct smb_filename *smb_fname_out = NULL;
4024 status = get_relative_fid_filename(conn, req, root_dir_fid,
4025 smb_fname, &smb_fname_out);
4026 if (!NT_STATUS_IS_OK(status)) {
4027 goto fail;
4029 smb_fname = smb_fname_out;
4033 * Check to see if this is a mac fork of some kind.
4036 stream_name = is_ntfs_stream_smb_fname(smb_fname);
4037 if (stream_name) {
4038 enum FAKE_FILE_TYPE fake_file_type;
4040 fake_file_type = is_fake_file(smb_fname);
4042 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
4045 * Here we go! support for changing the disk quotas
4046 * --metze
4048 * We need to fake up to open this MAGIC QUOTA file
4049 * and return a valid FID.
4051 * w2k close this file directly after openening xp
4052 * also tries a QUERY_FILE_INFO on the file and then
4053 * close it
4055 status = open_fake_file(req, conn, req->vuid,
4056 fake_file_type, smb_fname,
4057 access_mask, &fsp);
4058 if (!NT_STATUS_IS_OK(status)) {
4059 goto fail;
4062 ZERO_STRUCT(smb_fname->st);
4063 goto done;
4066 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
4067 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4068 goto fail;
4072 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
4073 int ret;
4074 smb_fname->stream_name = NULL;
4075 /* We have to handle this error here. */
4076 if (create_options & FILE_DIRECTORY_FILE) {
4077 status = NT_STATUS_NOT_A_DIRECTORY;
4078 goto fail;
4080 if (lp_posix_pathnames()) {
4081 ret = SMB_VFS_LSTAT(conn, smb_fname);
4082 } else {
4083 ret = SMB_VFS_STAT(conn, smb_fname);
4086 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
4087 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4088 goto fail;
4092 status = create_file_unixpath(
4093 conn, req, smb_fname, access_mask, share_access,
4094 create_disposition, create_options, file_attributes,
4095 oplock_request, allocation_size, private_flags,
4096 sd, ea_list,
4097 &fsp, &info);
4099 if (!NT_STATUS_IS_OK(status)) {
4100 goto fail;
4103 done:
4104 DEBUG(10, ("create_file: info=%d\n", info));
4106 *result = fsp;
4107 if (pinfo != NULL) {
4108 *pinfo = info;
4110 return NT_STATUS_OK;
4112 fail:
4113 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
4115 if (fsp != NULL) {
4116 close_file(req, fsp, ERROR_CLOSE);
4117 fsp = NULL;
4119 return status;