s3-torture/denytest.c: replace cli_read_old() with cli_read()
[Samba/gebeck_regimport.git] / source3 / smbd / open.c
blobd81c2781108f9391ef8e9d3b12b2777ae77e8e9f
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 "auth.h"
31 #include "messages.h"
33 extern const struct generic_mapping file_generic_mapping;
35 struct deferred_open_record {
36 bool delayed_for_oplocks;
37 struct file_id id;
40 /****************************************************************************
41 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
42 ****************************************************************************/
44 NTSTATUS smb1_file_se_access_check(struct connection_struct *conn,
45 const struct security_descriptor *sd,
46 const struct security_token *token,
47 uint32_t access_desired,
48 uint32_t *access_granted)
50 *access_granted = 0;
52 if (get_current_uid(conn) == (uid_t)0) {
53 /* I'm sorry sir, I didn't know you were root... */
54 *access_granted = access_desired;
55 if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
56 *access_granted |= FILE_GENERIC_ALL;
58 return NT_STATUS_OK;
61 return se_access_check(sd,
62 token,
63 (access_desired & ~FILE_READ_ATTRIBUTES),
64 access_granted);
67 /****************************************************************************
68 Check if we have open rights.
69 ****************************************************************************/
71 NTSTATUS smbd_check_open_rights(struct connection_struct *conn,
72 const struct smb_filename *smb_fname,
73 uint32_t access_mask,
74 uint32_t *access_granted)
76 /* Check if we have rights to open. */
77 NTSTATUS status;
78 struct security_descriptor *sd = NULL;
79 uint32_t rejected_share_access;
81 rejected_share_access = access_mask & ~(conn->share_access);
83 if (rejected_share_access) {
84 *access_granted = rejected_share_access;
85 return NT_STATUS_ACCESS_DENIED;
88 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
89 *access_granted = access_mask;
91 DEBUG(10,("smbd_check_open_rights: not checking ACL "
92 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
93 smb_fname_str_dbg(smb_fname),
94 (unsigned int)*access_granted ));
95 return NT_STATUS_OK;
98 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
99 (SECINFO_OWNER |
100 SECINFO_GROUP |
101 SECINFO_DACL),&sd);
103 if (!NT_STATUS_IS_OK(status)) {
104 DEBUG(10, ("smbd_check_open_rights: Could not get acl "
105 "on %s: %s\n",
106 smb_fname_str_dbg(smb_fname),
107 nt_errstr(status)));
108 return status;
111 status = smb1_file_se_access_check(conn,
113 get_current_nttok(conn),
114 access_mask,
115 access_granted);
117 DEBUG(10,("smbd_check_open_rights: file %s requesting "
118 "0x%x returning 0x%x (%s)\n",
119 smb_fname_str_dbg(smb_fname),
120 (unsigned int)access_mask,
121 (unsigned int)*access_granted,
122 nt_errstr(status) ));
124 if (!NT_STATUS_IS_OK(status)) {
125 if (DEBUGLEVEL >= 10) {
126 DEBUG(10,("smbd_check_open_rights: acl for %s is:\n",
127 smb_fname_str_dbg(smb_fname) ));
128 NDR_PRINT_DEBUG(security_descriptor, sd);
132 TALLOC_FREE(sd);
134 return status;
137 /****************************************************************************
138 fd support routines - attempt to do a dos_open.
139 ****************************************************************************/
141 static NTSTATUS fd_open(struct connection_struct *conn,
142 files_struct *fsp,
143 int flags,
144 mode_t mode)
146 struct smb_filename *smb_fname = fsp->fsp_name;
147 NTSTATUS status = NT_STATUS_OK;
149 #ifdef O_NOFOLLOW
151 * Never follow symlinks on a POSIX client. The
152 * client should be doing this.
155 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
156 flags |= O_NOFOLLOW;
158 #endif
160 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
161 if (fsp->fh->fd == -1) {
162 status = map_nt_error_from_unix(errno);
163 if (errno == EMFILE) {
164 static time_t last_warned = 0L;
166 if (time((time_t *) NULL) > last_warned) {
167 DEBUG(0,("Too many open files, unable "
168 "to open more! smbd's max "
169 "open files = %d\n",
170 lp_max_open_files()));
171 last_warned = time((time_t *) NULL);
177 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
178 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
179 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
181 return status;
184 /****************************************************************************
185 Close the file associated with a fsp.
186 ****************************************************************************/
188 NTSTATUS fd_close(files_struct *fsp)
190 int ret;
192 if (fsp->dptr) {
193 dptr_CloseDir(fsp);
195 if (fsp->fh->fd == -1) {
196 return NT_STATUS_OK; /* What we used to call a stat open. */
198 if (fsp->fh->ref_count > 1) {
199 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
202 ret = SMB_VFS_CLOSE(fsp);
203 fsp->fh->fd = -1;
204 if (ret == -1) {
205 return map_nt_error_from_unix(errno);
207 return NT_STATUS_OK;
210 /****************************************************************************
211 Change the ownership of a file to that of the parent directory.
212 Do this by fd if possible.
213 ****************************************************************************/
215 void change_file_owner_to_parent(connection_struct *conn,
216 const char *inherit_from_dir,
217 files_struct *fsp)
219 struct smb_filename *smb_fname_parent = NULL;
220 NTSTATUS status;
221 int ret;
223 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
224 NULL, NULL, &smb_fname_parent);
225 if (!NT_STATUS_IS_OK(status)) {
226 return;
229 ret = SMB_VFS_STAT(conn, smb_fname_parent);
230 if (ret == -1) {
231 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
232 "directory %s. Error was %s\n",
233 smb_fname_str_dbg(smb_fname_parent),
234 strerror(errno)));
235 TALLOC_FREE(smb_fname_parent);
236 return;
239 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
240 /* Already this uid - no need to change. */
241 DEBUG(10,("change_file_owner_to_parent: file %s "
242 "is already owned by uid %d\n",
243 fsp_str_dbg(fsp),
244 (int)fsp->fsp_name->st.st_ex_uid ));
245 TALLOC_FREE(smb_fname_parent);
246 return;
249 become_root();
250 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
251 unbecome_root();
252 if (ret == -1) {
253 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
254 "file %s to parent directory uid %u. Error "
255 "was %s\n", fsp_str_dbg(fsp),
256 (unsigned int)smb_fname_parent->st.st_ex_uid,
257 strerror(errno) ));
258 } else {
259 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
260 "parent directory uid %u.\n", fsp_str_dbg(fsp),
261 (unsigned int)smb_fname_parent->st.st_ex_uid));
262 /* Ensure the uid entry is updated. */
263 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
266 TALLOC_FREE(smb_fname_parent);
269 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
270 const char *inherit_from_dir,
271 const char *fname,
272 SMB_STRUCT_STAT *psbuf)
274 struct smb_filename *smb_fname_parent = NULL;
275 struct smb_filename *smb_fname_cwd = NULL;
276 char *saved_dir = NULL;
277 TALLOC_CTX *ctx = talloc_tos();
278 NTSTATUS status = NT_STATUS_OK;
279 int ret;
281 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
282 &smb_fname_parent);
283 if (!NT_STATUS_IS_OK(status)) {
284 return status;
287 ret = SMB_VFS_STAT(conn, smb_fname_parent);
288 if (ret == -1) {
289 status = map_nt_error_from_unix(errno);
290 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
291 "directory %s. Error was %s\n",
292 smb_fname_str_dbg(smb_fname_parent),
293 strerror(errno)));
294 goto out;
297 /* We've already done an lstat into psbuf, and we know it's a
298 directory. If we can cd into the directory and the dev/ino
299 are the same then we can safely chown without races as
300 we're locking the directory in place by being in it. This
301 should work on any UNIX (thanks tridge :-). JRA.
304 saved_dir = vfs_GetWd(ctx,conn);
305 if (!saved_dir) {
306 status = map_nt_error_from_unix(errno);
307 DEBUG(0,("change_dir_owner_to_parent: failed to get "
308 "current working directory. Error was %s\n",
309 strerror(errno)));
310 goto out;
313 /* Chdir into the new path. */
314 if (vfs_ChDir(conn, fname) == -1) {
315 status = map_nt_error_from_unix(errno);
316 DEBUG(0,("change_dir_owner_to_parent: failed to change "
317 "current working directory to %s. Error "
318 "was %s\n", fname, strerror(errno) ));
319 goto chdir;
322 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
323 &smb_fname_cwd);
324 if (!NT_STATUS_IS_OK(status)) {
325 return status;
328 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
329 if (ret == -1) {
330 status = map_nt_error_from_unix(errno);
331 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
332 "directory '.' (%s) Error was %s\n",
333 fname, strerror(errno)));
334 goto chdir;
337 /* Ensure we're pointing at the same place. */
338 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
339 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
340 DEBUG(0,("change_dir_owner_to_parent: "
341 "device/inode on directory %s changed. "
342 "Refusing to chown !\n", fname ));
343 status = NT_STATUS_ACCESS_DENIED;
344 goto chdir;
347 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
348 /* Already this uid - no need to change. */
349 DEBUG(10,("change_dir_owner_to_parent: directory %s "
350 "is already owned by uid %d\n",
351 fname,
352 (int)smb_fname_cwd->st.st_ex_uid ));
353 status = NT_STATUS_OK;
354 goto chdir;
357 become_root();
358 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
359 (gid_t)-1);
360 unbecome_root();
361 if (ret == -1) {
362 status = map_nt_error_from_unix(errno);
363 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
364 "directory %s to parent directory uid %u. "
365 "Error was %s\n", fname,
366 (unsigned int)smb_fname_parent->st.st_ex_uid,
367 strerror(errno) ));
368 } else {
369 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
370 "directory %s to parent directory uid %u.\n",
371 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
372 /* Ensure the uid entry is updated. */
373 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
376 chdir:
377 vfs_ChDir(conn,saved_dir);
378 out:
379 TALLOC_FREE(smb_fname_parent);
380 TALLOC_FREE(smb_fname_cwd);
381 return status;
384 /****************************************************************************
385 Open a file.
386 ****************************************************************************/
388 static NTSTATUS open_file(files_struct *fsp,
389 connection_struct *conn,
390 struct smb_request *req,
391 const char *parent_dir,
392 int flags,
393 mode_t unx_mode,
394 uint32 access_mask, /* client requested access mask. */
395 uint32 open_access_mask) /* what we're actually using in the open. */
397 struct smb_filename *smb_fname = fsp->fsp_name;
398 NTSTATUS status = NT_STATUS_OK;
399 int accmode = (flags & O_ACCMODE);
400 int local_flags = flags;
401 bool file_existed = VALID_STAT(fsp->fsp_name->st);
402 bool file_created = false;
404 fsp->fh->fd = -1;
405 errno = EPERM;
407 /* Check permissions */
410 * This code was changed after seeing a client open request
411 * containing the open mode of (DENY_WRITE/read-only) with
412 * the 'create if not exist' bit set. The previous code
413 * would fail to open the file read only on a read-only share
414 * as it was checking the flags parameter directly against O_RDONLY,
415 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
416 * JRA.
419 if (!CAN_WRITE(conn)) {
420 /* It's a read-only share - fail if we wanted to write. */
421 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
422 DEBUG(3,("Permission denied opening %s\n",
423 smb_fname_str_dbg(smb_fname)));
424 return NT_STATUS_ACCESS_DENIED;
425 } else if(flags & O_CREAT) {
426 /* We don't want to write - but we must make sure that
427 O_CREAT doesn't create the file if we have write
428 access into the directory.
430 flags &= ~(O_CREAT|O_EXCL);
431 local_flags &= ~(O_CREAT|O_EXCL);
436 * This little piece of insanity is inspired by the
437 * fact that an NT client can open a file for O_RDONLY,
438 * but set the create disposition to FILE_EXISTS_TRUNCATE.
439 * If the client *can* write to the file, then it expects to
440 * truncate the file, even though it is opening for readonly.
441 * Quicken uses this stupid trick in backup file creation...
442 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
443 * for helping track this one down. It didn't bite us in 2.0.x
444 * as we always opened files read-write in that release. JRA.
447 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
448 DEBUG(10,("open_file: truncate requested on read-only open "
449 "for file %s\n", smb_fname_str_dbg(smb_fname)));
450 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
453 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
454 (!file_existed && (local_flags & O_CREAT)) ||
455 ((local_flags & O_TRUNC) == O_TRUNC) ) {
456 const char *wild;
459 * We can't actually truncate here as the file may be locked.
460 * open_file_ntcreate will take care of the truncate later. JRA.
463 local_flags &= ~O_TRUNC;
465 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
467 * We would block on opening a FIFO with no one else on the
468 * other end. Do what we used to do and add O_NONBLOCK to the
469 * open flags. JRA.
472 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
473 local_flags |= O_NONBLOCK;
475 #endif
477 /* Don't create files with Microsoft wildcard characters. */
478 if (fsp->base_fsp) {
480 * wildcard characters are allowed in stream names
481 * only test the basefilename
483 wild = fsp->base_fsp->fsp_name->base_name;
484 } else {
485 wild = smb_fname->base_name;
487 if ((local_flags & O_CREAT) && !file_existed &&
488 ms_has_wild(wild)) {
489 return NT_STATUS_OBJECT_NAME_INVALID;
492 /* Actually do the open */
493 status = fd_open(conn, fsp, local_flags, unx_mode);
494 if (!NT_STATUS_IS_OK(status)) {
495 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
496 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
497 nt_errstr(status),local_flags,flags));
498 return status;
501 if ((local_flags & O_CREAT) && !file_existed) {
502 file_created = true;
505 } else {
506 fsp->fh->fd = -1; /* What we used to call a stat open. */
507 if (file_existed) {
508 uint32_t access_granted = 0;
510 status = smbd_check_open_rights(conn,
511 smb_fname,
512 access_mask,
513 &access_granted);
514 if (!NT_STATUS_IS_OK(status)) {
515 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
517 * On NT_STATUS_ACCESS_DENIED, access_granted
518 * contains the denied bits.
521 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
522 (access_granted & FILE_WRITE_ATTRIBUTES) &&
523 (lp_map_readonly(SNUM(conn)) ||
524 lp_map_archive(SNUM(conn)) ||
525 lp_map_hidden(SNUM(conn)) ||
526 lp_map_system(SNUM(conn)))) {
527 access_granted &= ~FILE_WRITE_ATTRIBUTES;
529 DEBUG(10,("open_file: "
530 "overrode "
531 "FILE_WRITE_"
532 "ATTRIBUTES "
533 "on file %s\n",
534 smb_fname_str_dbg(
535 smb_fname)));
538 if ((access_mask & DELETE_ACCESS) &&
539 (access_granted & DELETE_ACCESS) &&
540 can_delete_file_in_directory(conn,
541 smb_fname)) {
542 /* Were we trying to do a stat open
543 * for delete and didn't get DELETE
544 * access (only) ? Check if the
545 * directory allows DELETE_CHILD.
546 * See here:
547 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
548 * for details. */
550 access_granted &= ~DELETE_ACCESS;
552 DEBUG(10,("open_file: "
553 "overrode "
554 "DELETE_ACCESS on "
555 "file %s\n",
556 smb_fname_str_dbg(
557 smb_fname)));
560 if (access_granted != 0) {
561 DEBUG(10,("open_file: Access "
562 "denied on file "
563 "%s\n",
564 smb_fname_str_dbg(
565 smb_fname)));
566 return status;
568 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
569 fsp->posix_open &&
570 S_ISLNK(smb_fname->st.st_ex_mode)) {
571 /* This is a POSIX stat open for delete
572 * or rename on a symlink that points
573 * nowhere. Allow. */
574 DEBUG(10,("open_file: allowing POSIX "
575 "open on bad symlink %s\n",
576 smb_fname_str_dbg(
577 smb_fname)));
578 } else {
579 DEBUG(10,("open_file: "
580 "smbd_check_open_rights on file "
581 "%s returned %s\n",
582 smb_fname_str_dbg(smb_fname),
583 nt_errstr(status) ));
584 return status;
590 if (!file_existed) {
591 int ret;
593 if (fsp->fh->fd == -1) {
594 ret = SMB_VFS_STAT(conn, smb_fname);
595 } else {
596 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
597 /* If we have an fd, this stat should succeed. */
598 if (ret == -1) {
599 DEBUG(0,("Error doing fstat on open file %s "
600 "(%s)\n",
601 smb_fname_str_dbg(smb_fname),
602 strerror(errno) ));
606 /* For a non-io open, this stat failing means file not found. JRA */
607 if (ret == -1) {
608 status = map_nt_error_from_unix(errno);
609 fd_close(fsp);
610 return status;
613 if (file_created) {
614 bool need_re_stat = false;
615 /* Do all inheritance work after we've
616 done a successful stat call and filled
617 in the stat struct in fsp->fsp_name. */
619 /* Inherit the ACL if required */
620 if (lp_inherit_perms(SNUM(conn))) {
621 inherit_access_posix_acl(conn, parent_dir,
622 smb_fname->base_name,
623 unx_mode);
624 need_re_stat = true;
627 /* Change the owner if required. */
628 if (lp_inherit_owner(SNUM(conn))) {
629 change_file_owner_to_parent(conn, parent_dir,
630 fsp);
631 need_re_stat = true;
634 if (need_re_stat) {
635 if (fsp->fh->fd == -1) {
636 ret = SMB_VFS_STAT(conn, smb_fname);
637 } else {
638 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
639 /* If we have an fd, this stat should succeed. */
640 if (ret == -1) {
641 DEBUG(0,("Error doing fstat on open file %s "
642 "(%s)\n",
643 smb_fname_str_dbg(smb_fname),
644 strerror(errno) ));
649 notify_fname(conn, NOTIFY_ACTION_ADDED,
650 FILE_NOTIFY_CHANGE_FILE_NAME,
651 smb_fname->base_name);
656 * POSIX allows read-only opens of directories. We don't
657 * want to do this (we use a different code path for this)
658 * so catch a directory open and return an EISDIR. JRA.
661 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
662 fd_close(fsp);
663 errno = EISDIR;
664 return NT_STATUS_FILE_IS_A_DIRECTORY;
667 fsp->mode = smb_fname->st.st_ex_mode;
668 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
669 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
670 fsp->file_pid = req ? req->smbpid : 0;
671 fsp->can_lock = True;
672 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
673 if (!CAN_WRITE(conn)) {
674 fsp->can_write = False;
675 } else {
676 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
677 True : False;
679 fsp->print_file = NULL;
680 fsp->modified = False;
681 fsp->sent_oplock_break = NO_BREAK_SENT;
682 fsp->is_directory = False;
683 if (conn->aio_write_behind_list &&
684 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
685 conn->case_sensitive)) {
686 fsp->aio_write_behind = True;
689 fsp->wcp = NULL; /* Write cache pointer. */
691 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
692 conn->session_info->unix_info->unix_name,
693 smb_fname_str_dbg(smb_fname),
694 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
695 conn->num_files_open));
697 errno = 0;
698 return NT_STATUS_OK;
701 /*******************************************************************
702 Return True if the filename is one of the special executable types.
703 ********************************************************************/
705 bool is_executable(const char *fname)
707 if ((fname = strrchr_m(fname,'.'))) {
708 if (strequal(fname,".com") ||
709 strequal(fname,".dll") ||
710 strequal(fname,".exe") ||
711 strequal(fname,".sym")) {
712 return True;
715 return False;
718 /****************************************************************************
719 Check if we can open a file with a share mode.
720 Returns True if conflict, False if not.
721 ****************************************************************************/
723 static bool share_conflict(struct share_mode_entry *entry,
724 uint32 access_mask,
725 uint32 share_access)
727 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
728 "entry->share_access = 0x%x, "
729 "entry->private_options = 0x%x\n",
730 (unsigned int)entry->access_mask,
731 (unsigned int)entry->share_access,
732 (unsigned int)entry->private_options));
734 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
735 (unsigned int)access_mask, (unsigned int)share_access));
737 if ((entry->access_mask & (FILE_WRITE_DATA|
738 FILE_APPEND_DATA|
739 FILE_READ_DATA|
740 FILE_EXECUTE|
741 DELETE_ACCESS)) == 0) {
742 DEBUG(10,("share_conflict: No conflict due to "
743 "entry->access_mask = 0x%x\n",
744 (unsigned int)entry->access_mask ));
745 return False;
748 if ((access_mask & (FILE_WRITE_DATA|
749 FILE_APPEND_DATA|
750 FILE_READ_DATA|
751 FILE_EXECUTE|
752 DELETE_ACCESS)) == 0) {
753 DEBUG(10,("share_conflict: No conflict due to "
754 "access_mask = 0x%x\n",
755 (unsigned int)access_mask ));
756 return False;
759 #if 1 /* JRA TEST - Superdebug. */
760 #define CHECK_MASK(num, am, right, sa, share) \
761 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
762 (unsigned int)(num), (unsigned int)(am), \
763 (unsigned int)(right), (unsigned int)(am)&(right) )); \
764 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
765 (unsigned int)(num), (unsigned int)(sa), \
766 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
767 if (((am) & (right)) && !((sa) & (share))) { \
768 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
769 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
770 (unsigned int)(share) )); \
771 return True; \
773 #else
774 #define CHECK_MASK(num, am, right, sa, share) \
775 if (((am) & (right)) && !((sa) & (share))) { \
776 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
777 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
778 (unsigned int)(share) )); \
779 return True; \
781 #endif
783 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
784 share_access, FILE_SHARE_WRITE);
785 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
786 entry->share_access, FILE_SHARE_WRITE);
788 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
789 share_access, FILE_SHARE_READ);
790 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
791 entry->share_access, FILE_SHARE_READ);
793 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
794 share_access, FILE_SHARE_DELETE);
795 CHECK_MASK(6, access_mask, DELETE_ACCESS,
796 entry->share_access, FILE_SHARE_DELETE);
798 DEBUG(10,("share_conflict: No conflict.\n"));
799 return False;
802 #if defined(DEVELOPER)
803 static void validate_my_share_entries(struct smbd_server_connection *sconn,
804 int num,
805 struct share_mode_entry *share_entry)
807 files_struct *fsp;
809 if (!procid_is_me(&share_entry->pid)) {
810 return;
813 if (is_deferred_open_entry(share_entry) &&
814 !open_was_deferred(share_entry->op_mid)) {
815 char *str = talloc_asprintf(talloc_tos(),
816 "Got a deferred entry without a request: "
817 "PANIC: %s\n",
818 share_mode_str(talloc_tos(), num, share_entry));
819 smb_panic(str);
822 if (!is_valid_share_mode_entry(share_entry)) {
823 return;
826 fsp = file_find_dif(sconn, share_entry->id,
827 share_entry->share_file_id);
828 if (!fsp) {
829 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
830 share_mode_str(talloc_tos(), num, share_entry) ));
831 smb_panic("validate_my_share_entries: Cannot match a "
832 "share entry with an open file\n");
835 if (is_deferred_open_entry(share_entry) ||
836 is_unused_share_mode_entry(share_entry)) {
837 goto panic;
840 if ((share_entry->op_type == NO_OPLOCK) &&
841 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
842 /* Someone has already written to it, but I haven't yet
843 * noticed */
844 return;
847 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
848 goto panic;
851 return;
853 panic:
855 char *str;
856 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
857 share_mode_str(talloc_tos(), num, share_entry) ));
858 str = talloc_asprintf(talloc_tos(),
859 "validate_my_share_entries: "
860 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
861 fsp->fsp_name->base_name,
862 (unsigned int)fsp->oplock_type,
863 (unsigned int)share_entry->op_type );
864 smb_panic(str);
867 #endif
869 bool is_stat_open(uint32 access_mask)
871 return (access_mask &&
872 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
873 FILE_WRITE_ATTRIBUTES))==0) &&
874 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
875 FILE_WRITE_ATTRIBUTES)) != 0));
878 /****************************************************************************
879 Deal with share modes
880 Invarient: Share mode must be locked on entry and exit.
881 Returns -1 on error, or number of share modes on success (may be zero).
882 ****************************************************************************/
884 static NTSTATUS open_mode_check(connection_struct *conn,
885 struct share_mode_lock *lck,
886 uint32_t name_hash,
887 uint32 access_mask,
888 uint32 share_access,
889 uint32 create_options,
890 bool *file_existed)
892 int i;
894 if(lck->num_share_modes == 0) {
895 return NT_STATUS_OK;
898 *file_existed = True;
900 /* A delete on close prohibits everything */
902 if (is_delete_on_close_set(lck, name_hash)) {
903 return NT_STATUS_DELETE_PENDING;
906 if (is_stat_open(access_mask)) {
907 /* Stat open that doesn't trigger oplock breaks or share mode
908 * checks... ! JRA. */
909 return NT_STATUS_OK;
913 * Check if the share modes will give us access.
916 #if defined(DEVELOPER)
917 for(i = 0; i < lck->num_share_modes; i++) {
918 validate_my_share_entries(conn->sconn, i,
919 &lck->share_modes[i]);
921 #endif
923 if (!lp_share_modes(SNUM(conn))) {
924 return NT_STATUS_OK;
927 /* Now we check the share modes, after any oplock breaks. */
928 for(i = 0; i < lck->num_share_modes; i++) {
930 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
931 continue;
934 /* someone else has a share lock on it, check to see if we can
935 * too */
936 if (share_conflict(&lck->share_modes[i],
937 access_mask, share_access)) {
938 return NT_STATUS_SHARING_VIOLATION;
942 return NT_STATUS_OK;
945 static bool is_delete_request(files_struct *fsp) {
946 return ((fsp->access_mask == DELETE_ACCESS) &&
947 (fsp->oplock_type == NO_OPLOCK));
951 * Send a break message to the oplock holder and delay the open for
952 * our client.
955 static NTSTATUS send_break_message(files_struct *fsp,
956 struct share_mode_entry *exclusive,
957 uint64_t mid,
958 int oplock_request)
960 NTSTATUS status;
961 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
963 DEBUG(10, ("Sending break request to PID %s\n",
964 procid_str_static(&exclusive->pid)));
965 exclusive->op_mid = mid;
967 /* Create the message. */
968 share_mode_entry_to_message(msg, exclusive);
970 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
971 don't want this set in the share mode struct pointed to by lck. */
973 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
974 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
975 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
978 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
979 MSG_SMB_BREAK_REQUEST,
980 (uint8 *)msg,
981 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
982 if (!NT_STATUS_IS_OK(status)) {
983 DEBUG(3, ("Could not send oplock break message: %s\n",
984 nt_errstr(status)));
987 return status;
991 * Return share_mode_entry pointers for :
992 * 1). Batch oplock entry.
993 * 2). Batch or exclusive oplock entry (may be identical to #1).
994 * bool have_level2_oplock
995 * bool have_no_oplock.
996 * Do internal consistency checks on the share mode for a file.
999 static void find_oplock_types(files_struct *fsp,
1000 int oplock_request,
1001 struct share_mode_lock *lck,
1002 struct share_mode_entry **pp_batch,
1003 struct share_mode_entry **pp_ex_or_batch,
1004 bool *got_level2,
1005 bool *got_no_oplock)
1007 int i;
1009 *pp_batch = NULL;
1010 *pp_ex_or_batch = NULL;
1011 *got_level2 = false;
1012 *got_no_oplock = false;
1014 /* Ignore stat or internal opens, as is done in
1015 delay_for_batch_oplocks() and
1016 delay_for_exclusive_oplocks().
1018 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1019 return;
1022 for (i=0; i<lck->num_share_modes; i++) {
1023 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
1024 continue;
1027 if (lck->share_modes[i].op_type == NO_OPLOCK &&
1028 is_stat_open(lck->share_modes[i].access_mask)) {
1029 /* We ignore stat opens in the table - they
1030 always have NO_OPLOCK and never get or
1031 cause breaks. JRA. */
1032 continue;
1035 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1036 /* batch - can only be one. */
1037 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1038 smb_panic("Bad batch oplock entry.");
1040 *pp_batch = &lck->share_modes[i];
1043 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1044 /* Exclusive or batch - can only be one. */
1045 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1046 smb_panic("Bad exclusive or batch oplock entry.");
1048 *pp_ex_or_batch = &lck->share_modes[i];
1051 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1052 if (*pp_batch || *pp_ex_or_batch) {
1053 smb_panic("Bad levelII oplock entry.");
1055 *got_level2 = true;
1058 if (lck->share_modes[i].op_type == NO_OPLOCK) {
1059 if (*pp_batch || *pp_ex_or_batch) {
1060 smb_panic("Bad no oplock entry.");
1062 *got_no_oplock = true;
1067 static bool delay_for_batch_oplocks(files_struct *fsp,
1068 uint64_t mid,
1069 int oplock_request,
1070 struct share_mode_entry *batch_entry)
1072 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1073 return false;
1076 if (batch_entry != NULL) {
1077 /* Found a batch oplock */
1078 send_break_message(fsp, batch_entry, mid, oplock_request);
1079 return true;
1081 return false;
1084 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1085 uint64_t mid,
1086 int oplock_request,
1087 struct share_mode_entry *ex_entry)
1089 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1090 return false;
1093 if (ex_entry != NULL) {
1094 /* Found an exclusive or batch oplock */
1095 bool delay_it = is_delete_request(fsp) ?
1096 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1097 if (delay_it) {
1098 send_break_message(fsp, ex_entry, mid, oplock_request);
1099 return true;
1102 return false;
1105 static void grant_fsp_oplock_type(files_struct *fsp,
1106 const struct byte_range_lock *br_lck,
1107 int oplock_request,
1108 bool got_level2_oplock,
1109 bool got_a_none_oplock)
1111 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1112 lp_level2_oplocks(SNUM(fsp->conn));
1114 /* Start by granting what the client asked for,
1115 but ensure no SAMBA_PRIVATE bits can be set. */
1116 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1118 if (oplock_request & INTERNAL_OPEN_ONLY) {
1119 /* No oplocks on internal open. */
1120 fsp->oplock_type = NO_OPLOCK;
1121 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1122 fsp->oplock_type, fsp_str_dbg(fsp)));
1123 return;
1124 } else if (br_lck && br_lck->num_locks > 0) {
1125 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1126 fsp_str_dbg(fsp)));
1127 fsp->oplock_type = NO_OPLOCK;
1130 if (is_stat_open(fsp->access_mask)) {
1131 /* Leave the value already set. */
1132 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1133 fsp->oplock_type, fsp_str_dbg(fsp)));
1134 return;
1138 * Match what was requested (fsp->oplock_type) with
1139 * what was found in the existing share modes.
1142 if (got_a_none_oplock) {
1143 fsp->oplock_type = NO_OPLOCK;
1144 } else if (got_level2_oplock) {
1145 if (fsp->oplock_type == NO_OPLOCK ||
1146 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1147 /* Store a level2 oplock, but don't tell the client */
1148 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1149 } else {
1150 fsp->oplock_type = LEVEL_II_OPLOCK;
1152 } else {
1153 /* All share_mode_entries are placeholders or deferred.
1154 * Silently upgrade to fake levelII if the client didn't
1155 * ask for an oplock. */
1156 if (fsp->oplock_type == NO_OPLOCK) {
1157 /* Store a level2 oplock, but don't tell the client */
1158 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1163 * Don't grant level2 to clients that don't want them
1164 * or if we've turned them off.
1166 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1167 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1170 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1171 fsp->oplock_type, fsp_str_dbg(fsp)));
1174 bool request_timed_out(struct timeval request_time,
1175 struct timeval timeout)
1177 struct timeval now, end_time;
1178 GetTimeOfDay(&now);
1179 end_time = timeval_sum(&request_time, &timeout);
1180 return (timeval_compare(&end_time, &now) < 0);
1183 /****************************************************************************
1184 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1185 ****************************************************************************/
1187 static void defer_open(struct share_mode_lock *lck,
1188 struct timeval request_time,
1189 struct timeval timeout,
1190 struct smb_request *req,
1191 struct deferred_open_record *state)
1193 int i;
1195 /* Paranoia check */
1197 for (i=0; i<lck->num_share_modes; i++) {
1198 struct share_mode_entry *e = &lck->share_modes[i];
1200 if (!is_deferred_open_entry(e)) {
1201 continue;
1204 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
1205 DEBUG(0, ("Trying to defer an already deferred "
1206 "request: mid=%llu, exiting\n",
1207 (unsigned long long)req->mid));
1208 exit_server("attempt to defer a deferred request");
1212 /* End paranoia check */
1214 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1215 "open entry for mid %llu\n",
1216 (unsigned int)request_time.tv_sec,
1217 (unsigned int)request_time.tv_usec,
1218 (unsigned long long)req->mid));
1220 if (!push_deferred_open_message_smb(req, request_time, timeout,
1221 state->id, (char *)state, sizeof(*state))) {
1222 exit_server("push_deferred_open_message_smb failed");
1224 add_deferred_open(lck, req->mid, request_time,
1225 sconn_server_id(req->sconn), state->id);
1229 /****************************************************************************
1230 On overwrite open ensure that the attributes match.
1231 ****************************************************************************/
1233 bool open_match_attributes(connection_struct *conn,
1234 uint32 old_dos_attr,
1235 uint32 new_dos_attr,
1236 mode_t existing_unx_mode,
1237 mode_t new_unx_mode,
1238 mode_t *returned_unx_mode)
1240 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1242 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1243 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1245 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1246 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1247 *returned_unx_mode = new_unx_mode;
1248 } else {
1249 *returned_unx_mode = (mode_t)0;
1252 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1253 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1254 "returned_unx_mode = 0%o\n",
1255 (unsigned int)old_dos_attr,
1256 (unsigned int)existing_unx_mode,
1257 (unsigned int)new_dos_attr,
1258 (unsigned int)*returned_unx_mode ));
1260 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1261 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1262 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1263 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1264 return False;
1267 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1268 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1269 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1270 return False;
1273 return True;
1276 /****************************************************************************
1277 Special FCB or DOS processing in the case of a sharing violation.
1278 Try and find a duplicated file handle.
1279 ****************************************************************************/
1281 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1282 connection_struct *conn,
1283 files_struct *fsp_to_dup_into,
1284 const struct smb_filename *smb_fname,
1285 struct file_id id,
1286 uint16 file_pid,
1287 uint16 vuid,
1288 uint32 access_mask,
1289 uint32 share_access,
1290 uint32 create_options)
1292 files_struct *fsp;
1294 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1295 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1297 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1298 fsp = file_find_di_next(fsp)) {
1300 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1301 "vuid = %u, file_pid = %u, private_options = 0x%x "
1302 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1303 fsp->fh->fd, (unsigned int)fsp->vuid,
1304 (unsigned int)fsp->file_pid,
1305 (unsigned int)fsp->fh->private_options,
1306 (unsigned int)fsp->access_mask ));
1308 if (fsp->fh->fd != -1 &&
1309 fsp->vuid == vuid &&
1310 fsp->file_pid == file_pid &&
1311 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1312 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1313 (fsp->access_mask & FILE_WRITE_DATA) &&
1314 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1315 strequal(fsp->fsp_name->stream_name,
1316 smb_fname->stream_name)) {
1317 DEBUG(10,("fcb_or_dos_open: file match\n"));
1318 break;
1322 if (!fsp) {
1323 return NT_STATUS_NOT_FOUND;
1326 /* quite an insane set of semantics ... */
1327 if (is_executable(smb_fname->base_name) &&
1328 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1329 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1330 return NT_STATUS_INVALID_PARAMETER;
1333 /* We need to duplicate this fsp. */
1334 return dup_file_fsp(req, fsp, access_mask, share_access,
1335 create_options, fsp_to_dup_into);
1338 /****************************************************************************
1339 Open a file with a share mode - old openX method - map into NTCreate.
1340 ****************************************************************************/
1342 bool map_open_params_to_ntcreate(const struct smb_filename *smb_fname,
1343 int deny_mode, int open_func,
1344 uint32 *paccess_mask,
1345 uint32 *pshare_mode,
1346 uint32 *pcreate_disposition,
1347 uint32 *pcreate_options,
1348 uint32_t *pprivate_flags)
1350 uint32 access_mask;
1351 uint32 share_mode;
1352 uint32 create_disposition;
1353 uint32 create_options = FILE_NON_DIRECTORY_FILE;
1354 uint32_t private_flags = 0;
1356 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1357 "open_func = 0x%x\n",
1358 smb_fname_str_dbg(smb_fname), (unsigned int)deny_mode,
1359 (unsigned int)open_func ));
1361 /* Create the NT compatible access_mask. */
1362 switch (GET_OPENX_MODE(deny_mode)) {
1363 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1364 case DOS_OPEN_RDONLY:
1365 access_mask = FILE_GENERIC_READ;
1366 break;
1367 case DOS_OPEN_WRONLY:
1368 access_mask = FILE_GENERIC_WRITE;
1369 break;
1370 case DOS_OPEN_RDWR:
1371 case DOS_OPEN_FCB:
1372 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1373 break;
1374 default:
1375 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1376 (unsigned int)GET_OPENX_MODE(deny_mode)));
1377 return False;
1380 /* Create the NT compatible create_disposition. */
1381 switch (open_func) {
1382 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1383 create_disposition = FILE_CREATE;
1384 break;
1386 case OPENX_FILE_EXISTS_OPEN:
1387 create_disposition = FILE_OPEN;
1388 break;
1390 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1391 create_disposition = FILE_OPEN_IF;
1392 break;
1394 case OPENX_FILE_EXISTS_TRUNCATE:
1395 create_disposition = FILE_OVERWRITE;
1396 break;
1398 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1399 create_disposition = FILE_OVERWRITE_IF;
1400 break;
1402 default:
1403 /* From samba4 - to be confirmed. */
1404 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1405 create_disposition = FILE_CREATE;
1406 break;
1408 DEBUG(10,("map_open_params_to_ntcreate: bad "
1409 "open_func 0x%x\n", (unsigned int)open_func));
1410 return False;
1413 /* Create the NT compatible share modes. */
1414 switch (GET_DENY_MODE(deny_mode)) {
1415 case DENY_ALL:
1416 share_mode = FILE_SHARE_NONE;
1417 break;
1419 case DENY_WRITE:
1420 share_mode = FILE_SHARE_READ;
1421 break;
1423 case DENY_READ:
1424 share_mode = FILE_SHARE_WRITE;
1425 break;
1427 case DENY_NONE:
1428 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1429 break;
1431 case DENY_DOS:
1432 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1433 if (is_executable(smb_fname->base_name)) {
1434 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1435 } else {
1436 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1437 share_mode = FILE_SHARE_READ;
1438 } else {
1439 share_mode = FILE_SHARE_NONE;
1442 break;
1444 case DENY_FCB:
1445 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1446 share_mode = FILE_SHARE_NONE;
1447 break;
1449 default:
1450 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1451 (unsigned int)GET_DENY_MODE(deny_mode) ));
1452 return False;
1455 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1456 "share_mode = 0x%x, create_disposition = 0x%x, "
1457 "create_options = 0x%x private_flags = 0x%x\n",
1458 smb_fname_str_dbg(smb_fname),
1459 (unsigned int)access_mask,
1460 (unsigned int)share_mode,
1461 (unsigned int)create_disposition,
1462 (unsigned int)create_options,
1463 (unsigned int)private_flags));
1465 if (paccess_mask) {
1466 *paccess_mask = access_mask;
1468 if (pshare_mode) {
1469 *pshare_mode = share_mode;
1471 if (pcreate_disposition) {
1472 *pcreate_disposition = create_disposition;
1474 if (pcreate_options) {
1475 *pcreate_options = create_options;
1477 if (pprivate_flags) {
1478 *pprivate_flags = private_flags;
1481 return True;
1485 static void schedule_defer_open(struct share_mode_lock *lck,
1486 struct timeval request_time,
1487 struct smb_request *req)
1489 struct deferred_open_record state;
1491 /* This is a relative time, added to the absolute
1492 request_time value to get the absolute timeout time.
1493 Note that if this is the second or greater time we enter
1494 this codepath for this particular request mid then
1495 request_time is left as the absolute time of the *first*
1496 time this request mid was processed. This is what allows
1497 the request to eventually time out. */
1499 struct timeval timeout;
1501 /* Normally the smbd we asked should respond within
1502 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1503 * the client did, give twice the timeout as a safety
1504 * measure here in case the other smbd is stuck
1505 * somewhere else. */
1507 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1509 /* Nothing actually uses state.delayed_for_oplocks
1510 but it's handy to differentiate in debug messages
1511 between a 30 second delay due to oplock break, and
1512 a 1 second delay for share mode conflicts. */
1514 state.delayed_for_oplocks = True;
1515 state.id = lck->id;
1517 if (!request_timed_out(request_time, timeout)) {
1518 defer_open(lck, request_time, timeout, req, &state);
1522 /****************************************************************************
1523 Work out what access_mask to use from what the client sent us.
1524 ****************************************************************************/
1526 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1527 const struct smb_filename *smb_fname,
1528 bool file_existed,
1529 uint32_t access_mask,
1530 uint32_t *access_mask_out)
1532 NTSTATUS status;
1533 uint32_t orig_access_mask = access_mask;
1534 uint32_t rejected_share_access;
1537 * Convert GENERIC bits to specific bits.
1540 se_map_generic(&access_mask, &file_generic_mapping);
1542 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1543 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1544 if (file_existed) {
1546 struct security_descriptor *sd;
1547 uint32_t access_granted = 0;
1549 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1550 (SECINFO_OWNER |
1551 SECINFO_GROUP |
1552 SECINFO_DACL),&sd);
1554 if (!NT_STATUS_IS_OK(status)) {
1555 DEBUG(10,("smbd_calculate_access_mask: "
1556 "Could not get acl on file %s: %s\n",
1557 smb_fname_str_dbg(smb_fname),
1558 nt_errstr(status)));
1559 return NT_STATUS_ACCESS_DENIED;
1562 status = smb1_file_se_access_check(conn,
1564 get_current_nttok(conn),
1565 access_mask,
1566 &access_granted);
1568 TALLOC_FREE(sd);
1570 if (!NT_STATUS_IS_OK(status)) {
1571 DEBUG(10, ("smbd_calculate_access_mask: "
1572 "Access denied on file %s: "
1573 "when calculating maximum access\n",
1574 smb_fname_str_dbg(smb_fname)));
1575 return NT_STATUS_ACCESS_DENIED;
1578 access_mask = access_granted;
1579 } else {
1580 access_mask = FILE_GENERIC_ALL;
1583 access_mask &= conn->share_access;
1586 rejected_share_access = access_mask & ~(conn->share_access);
1588 if (rejected_share_access) {
1589 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1590 "file %s: rejected by share access mask[0x%08X] "
1591 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1592 smb_fname_str_dbg(smb_fname),
1593 conn->share_access,
1594 orig_access_mask, access_mask,
1595 rejected_share_access));
1596 return NT_STATUS_ACCESS_DENIED;
1599 *access_mask_out = access_mask;
1600 return NT_STATUS_OK;
1603 /****************************************************************************
1604 Remove the deferred open entry under lock.
1605 ****************************************************************************/
1607 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1608 struct server_id pid)
1610 struct share_mode_lock *lck = get_share_mode_lock(talloc_tos(), id,
1611 NULL, NULL, NULL);
1612 if (lck == NULL) {
1613 DEBUG(0, ("could not get share mode lock\n"));
1614 } else {
1615 del_deferred_open_entry(lck, mid, pid);
1616 TALLOC_FREE(lck);
1620 /****************************************************************
1621 Ensure we get the brlock lock followed by the share mode lock
1622 in the correct order to prevent deadlocks if other smbd's are
1623 using the brlock database on this file simultaneously with this open
1624 (that code also gets the locks in brlock -> share mode lock order).
1625 ****************************************************************/
1627 static bool acquire_ordered_locks(TALLOC_CTX *mem_ctx,
1628 files_struct *fsp,
1629 const struct file_id id,
1630 const char *connectpath,
1631 const struct smb_filename *smb_fname,
1632 const struct timespec *p_old_write_time,
1633 struct share_mode_lock **p_lck,
1634 struct byte_range_lock **p_br_lck)
1636 /* Ordering - we must get the br_lck for this
1637 file before the share mode. */
1638 if (lp_locking(fsp->conn->params)) {
1639 *p_br_lck = brl_get_locks_readonly(fsp);
1640 if (*p_br_lck == NULL) {
1641 DEBUG(0, ("Could not get br_lock\n"));
1642 return false;
1644 /* Note - we don't need to free the returned
1645 br_lck explicitly as it was allocated on talloc_tos()
1646 and so will be autofreed (and release the lock)
1647 once the frame context disappears.
1649 If it was set to fsp->brlock_rec then it was
1650 talloc_move'd to hang off the fsp pointer and
1651 in this case is guarenteed to not be holding the
1652 lock on the brlock database. */
1655 *p_lck = get_share_mode_lock(mem_ctx,
1657 connectpath,
1658 smb_fname,
1659 p_old_write_time);
1661 if (*p_lck == NULL) {
1662 DEBUG(0, ("Could not get share mode lock\n"));
1663 TALLOC_FREE(*p_br_lck);
1664 return false;
1666 return true;
1669 /****************************************************************************
1670 Open a file with a share mode. Passed in an already created files_struct *.
1671 ****************************************************************************/
1673 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1674 struct smb_request *req,
1675 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1676 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1677 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1678 uint32 create_options, /* options such as delete on close. */
1679 uint32 new_dos_attributes, /* attributes used for new file. */
1680 int oplock_request, /* internal Samba oplock codes. */
1681 /* Information (FILE_EXISTS etc.) */
1682 uint32_t private_flags, /* Samba specific flags. */
1683 int *pinfo,
1684 files_struct *fsp)
1686 struct smb_filename *smb_fname = fsp->fsp_name;
1687 int flags=0;
1688 int flags2=0;
1689 bool file_existed = VALID_STAT(smb_fname->st);
1690 bool def_acl = False;
1691 bool posix_open = False;
1692 bool new_file_created = False;
1693 bool clear_ads = false;
1694 struct file_id id;
1695 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1696 mode_t new_unx_mode = (mode_t)0;
1697 mode_t unx_mode = (mode_t)0;
1698 int info;
1699 uint32 existing_dos_attributes = 0;
1700 struct timeval request_time = timeval_zero();
1701 struct share_mode_lock *lck = NULL;
1702 uint32 open_access_mask = access_mask;
1703 NTSTATUS status;
1704 char *parent_dir;
1706 ZERO_STRUCT(id);
1708 /* Windows allows a new file to be created and
1709 silently removes a FILE_ATTRIBUTE_DIRECTORY
1710 sent by the client. Do the same. */
1712 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1714 if (conn->printer) {
1716 * Printers are handled completely differently.
1717 * Most of the passed parameters are ignored.
1720 if (pinfo) {
1721 *pinfo = FILE_WAS_CREATED;
1724 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1725 smb_fname_str_dbg(smb_fname)));
1727 if (!req) {
1728 DEBUG(0,("open_file_ntcreate: printer open without "
1729 "an SMB request!\n"));
1730 return NT_STATUS_INTERNAL_ERROR;
1733 return print_spool_open(fsp, smb_fname->base_name,
1734 req->vuid);
1737 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1738 NULL)) {
1739 return NT_STATUS_NO_MEMORY;
1742 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1743 posix_open = True;
1744 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1745 new_dos_attributes = 0;
1746 } else {
1747 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1748 * created new. */
1749 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1750 smb_fname, parent_dir);
1753 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1754 "access_mask=0x%x share_access=0x%x "
1755 "create_disposition = 0x%x create_options=0x%x "
1756 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1757 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1758 access_mask, share_access, create_disposition,
1759 create_options, (unsigned int)unx_mode, oplock_request,
1760 (unsigned int)private_flags));
1762 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1763 DEBUG(0, ("No smb request but not an internal only open!\n"));
1764 return NT_STATUS_INTERNAL_ERROR;
1768 * Only non-internal opens can be deferred at all
1771 if (req) {
1772 void *ptr;
1773 if (get_deferred_open_message_state(req,
1774 &request_time,
1775 &ptr)) {
1777 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1778 /* Remember the absolute time of the original
1779 request with this mid. We'll use it later to
1780 see if this has timed out. */
1782 /* Remove the deferred open entry under lock. */
1783 remove_deferred_open_entry(
1784 state->id, req->mid,
1785 sconn_server_id(req->sconn));
1787 /* Ensure we don't reprocess this message. */
1788 remove_deferred_open_message_smb(req->mid);
1792 status = check_name(conn, smb_fname->base_name);
1793 if (!NT_STATUS_IS_OK(status)) {
1794 return status;
1797 if (!posix_open) {
1798 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1799 if (file_existed) {
1800 existing_dos_attributes = dos_mode(conn, smb_fname);
1804 /* ignore any oplock requests if oplocks are disabled */
1805 if (!lp_oplocks(SNUM(conn)) ||
1806 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1807 /* Mask off everything except the private Samba bits. */
1808 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1811 /* this is for OS/2 long file names - say we don't support them */
1812 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1813 /* OS/2 Workplace shell fix may be main code stream in a later
1814 * release. */
1815 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1816 "supported.\n"));
1817 if (use_nt_status()) {
1818 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1820 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1823 switch( create_disposition ) {
1825 * Currently we're using FILE_SUPERSEDE as the same as
1826 * FILE_OVERWRITE_IF but they really are
1827 * different. FILE_SUPERSEDE deletes an existing file
1828 * (requiring delete access) then recreates it.
1830 case FILE_SUPERSEDE:
1831 /* If file exists replace/overwrite. If file doesn't
1832 * exist create. */
1833 flags2 |= (O_CREAT | O_TRUNC);
1834 clear_ads = true;
1835 break;
1837 case FILE_OVERWRITE_IF:
1838 /* If file exists replace/overwrite. If file doesn't
1839 * exist create. */
1840 flags2 |= (O_CREAT | O_TRUNC);
1841 clear_ads = true;
1842 break;
1844 case FILE_OPEN:
1845 /* If file exists open. If file doesn't exist error. */
1846 if (!file_existed) {
1847 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1848 "requested for file %s and file "
1849 "doesn't exist.\n",
1850 smb_fname_str_dbg(smb_fname)));
1851 errno = ENOENT;
1852 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1854 break;
1856 case FILE_OVERWRITE:
1857 /* If file exists overwrite. If file doesn't exist
1858 * error. */
1859 if (!file_existed) {
1860 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1861 "requested for file %s and file "
1862 "doesn't exist.\n",
1863 smb_fname_str_dbg(smb_fname) ));
1864 errno = ENOENT;
1865 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1867 flags2 |= O_TRUNC;
1868 clear_ads = true;
1869 break;
1871 case FILE_CREATE:
1872 /* If file exists error. If file doesn't exist
1873 * create. */
1874 if (file_existed) {
1875 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1876 "requested for file %s and file "
1877 "already exists.\n",
1878 smb_fname_str_dbg(smb_fname)));
1879 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1880 errno = EISDIR;
1881 } else {
1882 errno = EEXIST;
1884 return map_nt_error_from_unix(errno);
1886 flags2 |= (O_CREAT|O_EXCL);
1887 break;
1889 case FILE_OPEN_IF:
1890 /* If file exists open. If file doesn't exist
1891 * create. */
1892 flags2 |= O_CREAT;
1893 break;
1895 default:
1896 return NT_STATUS_INVALID_PARAMETER;
1899 /* We only care about matching attributes on file exists and
1900 * overwrite. */
1902 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1903 (create_disposition == FILE_OVERWRITE_IF))) {
1904 if (!open_match_attributes(conn, existing_dos_attributes,
1905 new_dos_attributes,
1906 smb_fname->st.st_ex_mode,
1907 unx_mode, &new_unx_mode)) {
1908 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1909 "for file %s (%x %x) (0%o, 0%o)\n",
1910 smb_fname_str_dbg(smb_fname),
1911 existing_dos_attributes,
1912 new_dos_attributes,
1913 (unsigned int)smb_fname->st.st_ex_mode,
1914 (unsigned int)unx_mode ));
1915 errno = EACCES;
1916 return NT_STATUS_ACCESS_DENIED;
1920 status = smbd_calculate_access_mask(conn, smb_fname, file_existed,
1921 access_mask,
1922 &access_mask);
1923 if (!NT_STATUS_IS_OK(status)) {
1924 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1925 "on file %s returned %s\n",
1926 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1927 return status;
1930 open_access_mask = access_mask;
1932 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1933 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1936 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1937 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1938 access_mask));
1941 * Note that we ignore the append flag as append does not
1942 * mean the same thing under DOS and Unix.
1945 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1946 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1947 /* DENY_DOS opens are always underlying read-write on the
1948 file handle, no matter what the requested access mask
1949 says. */
1950 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1951 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1952 flags = O_RDWR;
1953 } else {
1954 flags = O_WRONLY;
1956 } else {
1957 flags = O_RDONLY;
1961 * Currently we only look at FILE_WRITE_THROUGH for create options.
1964 #if defined(O_SYNC)
1965 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1966 flags2 |= O_SYNC;
1968 #endif /* O_SYNC */
1970 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1971 flags2 |= O_APPEND;
1974 if (!posix_open && !CAN_WRITE(conn)) {
1976 * We should really return a permission denied error if either
1977 * O_CREAT or O_TRUNC are set, but for compatibility with
1978 * older versions of Samba we just AND them out.
1980 flags2 &= ~(O_CREAT|O_TRUNC);
1984 * Ensure we can't write on a read-only share or file.
1987 if (flags != O_RDONLY && file_existed &&
1988 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1989 DEBUG(5,("open_file_ntcreate: write access requested for "
1990 "file %s on read only %s\n",
1991 smb_fname_str_dbg(smb_fname),
1992 !CAN_WRITE(conn) ? "share" : "file" ));
1993 errno = EACCES;
1994 return NT_STATUS_ACCESS_DENIED;
1997 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1998 fsp->share_access = share_access;
1999 fsp->fh->private_options = private_flags;
2000 fsp->access_mask = open_access_mask; /* We change this to the
2001 * requested access_mask after
2002 * the open is done. */
2003 fsp->posix_open = posix_open;
2005 /* Ensure no SAMBA_PRIVATE bits can be set. */
2006 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2008 if (timeval_is_zero(&request_time)) {
2009 request_time = fsp->open_time;
2012 if (file_existed) {
2013 struct byte_range_lock *br_lck = NULL;
2014 struct share_mode_entry *batch_entry = NULL;
2015 struct share_mode_entry *exclusive_entry = NULL;
2016 bool got_level2_oplock = false;
2017 bool got_a_none_oplock = false;
2019 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2020 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2022 if (!acquire_ordered_locks(talloc_tos(),
2023 fsp,
2025 conn->connectpath,
2026 smb_fname,
2027 &old_write_time,
2028 &lck,
2029 &br_lck)) {
2030 return NT_STATUS_SHARING_VIOLATION;
2033 /* Get the types we need to examine. */
2034 find_oplock_types(fsp,
2035 oplock_request,
2036 lck,
2037 &batch_entry,
2038 &exclusive_entry,
2039 &got_level2_oplock,
2040 &got_a_none_oplock);
2042 /* First pass - send break only on batch oplocks. */
2043 if ((req != NULL) &&
2044 delay_for_batch_oplocks(fsp,
2045 req->mid,
2046 oplock_request,
2047 batch_entry)) {
2048 schedule_defer_open(lck, request_time, req);
2049 TALLOC_FREE(lck);
2050 return NT_STATUS_SHARING_VIOLATION;
2053 /* Use the client requested access mask here, not the one we
2054 * open with. */
2055 status = open_mode_check(conn, lck, fsp->name_hash,
2056 access_mask, share_access,
2057 create_options, &file_existed);
2059 if (NT_STATUS_IS_OK(status)) {
2060 /* We might be going to allow this open. Check oplock
2061 * status again. */
2062 /* Second pass - send break for both batch or
2063 * exclusive oplocks. */
2064 if ((req != NULL) &&
2065 delay_for_exclusive_oplocks(
2066 fsp,
2067 req->mid,
2068 oplock_request,
2069 exclusive_entry)) {
2070 schedule_defer_open(lck, request_time, req);
2071 TALLOC_FREE(lck);
2072 return NT_STATUS_SHARING_VIOLATION;
2076 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2077 /* DELETE_PENDING is not deferred for a second */
2078 TALLOC_FREE(lck);
2079 return status;
2082 grant_fsp_oplock_type(fsp,
2083 br_lck,
2084 oplock_request,
2085 got_level2_oplock,
2086 got_a_none_oplock);
2088 if (!NT_STATUS_IS_OK(status)) {
2089 uint32 can_access_mask;
2090 bool can_access = True;
2092 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2094 /* Check if this can be done with the deny_dos and fcb
2095 * calls. */
2096 if (private_flags &
2097 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2098 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2099 if (req == NULL) {
2100 DEBUG(0, ("DOS open without an SMB "
2101 "request!\n"));
2102 TALLOC_FREE(lck);
2103 return NT_STATUS_INTERNAL_ERROR;
2106 /* Use the client requested access mask here,
2107 * not the one we open with. */
2108 status = fcb_or_dos_open(req,
2109 conn,
2110 fsp,
2111 smb_fname,
2113 req->smbpid,
2114 req->vuid,
2115 access_mask,
2116 share_access,
2117 create_options);
2119 if (NT_STATUS_IS_OK(status)) {
2120 TALLOC_FREE(lck);
2121 if (pinfo) {
2122 *pinfo = FILE_WAS_OPENED;
2124 return NT_STATUS_OK;
2129 * This next line is a subtlety we need for
2130 * MS-Access. If a file open will fail due to share
2131 * permissions and also for security (access) reasons,
2132 * we need to return the access failed error, not the
2133 * share error. We can't open the file due to kernel
2134 * oplock deadlock (it's possible we failed above on
2135 * the open_mode_check()) so use a userspace check.
2138 if (flags & O_RDWR) {
2139 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2140 } else if (flags & O_WRONLY) {
2141 can_access_mask = FILE_WRITE_DATA;
2142 } else {
2143 can_access_mask = FILE_READ_DATA;
2146 if (((can_access_mask & FILE_WRITE_DATA) &&
2147 !CAN_WRITE(conn)) ||
2148 !can_access_file_data(conn, smb_fname,
2149 can_access_mask)) {
2150 can_access = False;
2154 * If we're returning a share violation, ensure we
2155 * cope with the braindead 1 second delay.
2158 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2159 lp_defer_sharing_violations()) {
2160 struct timeval timeout;
2161 struct deferred_open_record state;
2162 int timeout_usecs;
2164 /* this is a hack to speed up torture tests
2165 in 'make test' */
2166 timeout_usecs = lp_parm_int(SNUM(conn),
2167 "smbd","sharedelay",
2168 SHARING_VIOLATION_USEC_WAIT);
2170 /* This is a relative time, added to the absolute
2171 request_time value to get the absolute timeout time.
2172 Note that if this is the second or greater time we enter
2173 this codepath for this particular request mid then
2174 request_time is left as the absolute time of the *first*
2175 time this request mid was processed. This is what allows
2176 the request to eventually time out. */
2178 timeout = timeval_set(0, timeout_usecs);
2180 /* Nothing actually uses state.delayed_for_oplocks
2181 but it's handy to differentiate in debug messages
2182 between a 30 second delay due to oplock break, and
2183 a 1 second delay for share mode conflicts. */
2185 state.delayed_for_oplocks = False;
2186 state.id = id;
2188 if ((req != NULL)
2189 && !request_timed_out(request_time,
2190 timeout)) {
2191 defer_open(lck, request_time, timeout,
2192 req, &state);
2196 TALLOC_FREE(lck);
2197 if (can_access) {
2199 * We have detected a sharing violation here
2200 * so return the correct error code
2202 status = NT_STATUS_SHARING_VIOLATION;
2203 } else {
2204 status = NT_STATUS_ACCESS_DENIED;
2206 return status;
2210 * We exit this block with the share entry *locked*.....
2214 SMB_ASSERT(!file_existed || (lck != NULL));
2217 * Ensure we pay attention to default ACLs on directories if required.
2220 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2221 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2222 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2225 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2226 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2227 (unsigned int)flags, (unsigned int)flags2,
2228 (unsigned int)unx_mode, (unsigned int)access_mask,
2229 (unsigned int)open_access_mask));
2232 * open_file strips any O_TRUNC flags itself.
2235 fsp_open = open_file(fsp, conn, req, parent_dir,
2236 flags|flags2, unx_mode, access_mask,
2237 open_access_mask);
2239 if (!NT_STATUS_IS_OK(fsp_open)) {
2240 TALLOC_FREE(lck);
2241 return fsp_open;
2244 if (!file_existed) {
2245 struct byte_range_lock *br_lck = NULL;
2246 struct share_mode_entry *batch_entry = NULL;
2247 struct share_mode_entry *exclusive_entry = NULL;
2248 bool got_level2_oplock = false;
2249 bool got_a_none_oplock = false;
2250 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2252 * Deal with the race condition where two smbd's detect the
2253 * file doesn't exist and do the create at the same time. One
2254 * of them will win and set a share mode, the other (ie. this
2255 * one) should check if the requested share mode for this
2256 * create is allowed.
2260 * Now the file exists and fsp is successfully opened,
2261 * fsp->dev and fsp->inode are valid and should replace the
2262 * dev=0,inode=0 from a non existent file. Spotted by
2263 * Nadav Danieli <nadavd@exanet.com>. JRA.
2266 id = fsp->file_id;
2268 if (!acquire_ordered_locks(talloc_tos(),
2269 fsp,
2271 conn->connectpath,
2272 smb_fname,
2273 &old_write_time,
2274 &lck,
2275 &br_lck)) {
2276 return NT_STATUS_SHARING_VIOLATION;
2279 /* Get the types we need to examine. */
2280 find_oplock_types(fsp,
2281 oplock_request,
2282 lck,
2283 &batch_entry,
2284 &exclusive_entry,
2285 &got_level2_oplock,
2286 &got_a_none_oplock);
2288 /* First pass - send break only on batch oplocks. */
2289 if ((req != NULL) &&
2290 delay_for_batch_oplocks(fsp,
2291 req->mid,
2292 oplock_request,
2293 batch_entry)) {
2294 schedule_defer_open(lck, request_time, req);
2295 TALLOC_FREE(lck);
2296 fd_close(fsp);
2297 return NT_STATUS_SHARING_VIOLATION;
2300 status = open_mode_check(conn, lck, fsp->name_hash,
2301 access_mask, share_access,
2302 create_options, &file_existed);
2304 if (NT_STATUS_IS_OK(status)) {
2305 /* We might be going to allow this open. Check oplock
2306 * status again. */
2307 /* Second pass - send break for both batch or
2308 * exclusive oplocks. */
2309 if ((req != NULL) &&
2310 delay_for_exclusive_oplocks(
2311 fsp,
2312 req->mid,
2313 oplock_request,
2314 exclusive_entry)) {
2315 schedule_defer_open(lck, request_time, req);
2316 TALLOC_FREE(lck);
2317 fd_close(fsp);
2318 return NT_STATUS_SHARING_VIOLATION;
2322 if (!NT_STATUS_IS_OK(status)) {
2323 struct deferred_open_record state;
2325 state.delayed_for_oplocks = False;
2326 state.id = id;
2328 /* Do it all over again immediately. In the second
2329 * round we will find that the file existed and handle
2330 * the DELETE_PENDING and FCB cases correctly. No need
2331 * to duplicate the code here. Essentially this is a
2332 * "goto top of this function", but don't tell
2333 * anybody... */
2335 if (req != NULL) {
2336 defer_open(lck, request_time, timeval_zero(),
2337 req, &state);
2339 TALLOC_FREE(lck);
2340 fd_close(fsp);
2341 return status;
2344 grant_fsp_oplock_type(fsp,
2345 br_lck,
2346 oplock_request,
2347 got_level2_oplock,
2348 got_a_none_oplock);
2351 * We exit this block with the share entry *locked*.....
2356 SMB_ASSERT(lck != NULL);
2358 /* Delete streams if create_disposition requires it */
2359 if (file_existed && clear_ads &&
2360 !is_ntfs_stream_smb_fname(smb_fname)) {
2361 status = delete_all_streams(conn, smb_fname->base_name);
2362 if (!NT_STATUS_IS_OK(status)) {
2363 TALLOC_FREE(lck);
2364 fd_close(fsp);
2365 return status;
2369 /* note that we ignore failure for the following. It is
2370 basically a hack for NFS, and NFS will never set one of
2371 these only read them. Nobody but Samba can ever set a deny
2372 mode and we have already checked our more authoritative
2373 locking database for permission to set this deny mode. If
2374 the kernel refuses the operations then the kernel is wrong.
2375 note that GPFS supports it as well - jmcd */
2377 if (fsp->fh->fd != -1) {
2378 int ret_flock;
2379 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2380 if(ret_flock == -1 ){
2382 TALLOC_FREE(lck);
2383 fd_close(fsp);
2385 return NT_STATUS_SHARING_VIOLATION;
2390 * At this point onwards, we can guarentee that the share entry
2391 * is locked, whether we created the file or not, and that the
2392 * deny mode is compatible with all current opens.
2396 * If requested, truncate the file.
2399 if (file_existed && (flags2&O_TRUNC)) {
2401 * We are modifing the file after open - update the stat
2402 * struct..
2404 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2405 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2406 status = map_nt_error_from_unix(errno);
2407 TALLOC_FREE(lck);
2408 fd_close(fsp);
2409 return status;
2414 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2415 * but we don't have to store this - just ignore it on access check.
2417 if (conn->sconn->using_smb2) {
2419 * SMB2 doesn't return it (according to Microsoft tests).
2420 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2421 * File created with access = 0x7 (Read, Write, Delete)
2422 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2424 fsp->access_mask = access_mask;
2425 } else {
2426 /* But SMB1 does. */
2427 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2430 if (file_existed) {
2431 /* stat opens on existing files don't get oplocks. */
2432 if (is_stat_open(open_access_mask)) {
2433 fsp->oplock_type = NO_OPLOCK;
2436 if (!(flags2 & O_TRUNC)) {
2437 info = FILE_WAS_OPENED;
2438 } else {
2439 info = FILE_WAS_OVERWRITTEN;
2441 } else {
2442 info = FILE_WAS_CREATED;
2445 if (pinfo) {
2446 *pinfo = info;
2450 * Setup the oplock info in both the shared memory and
2451 * file structs.
2454 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2456 * Could not get the kernel oplock or there are byte-range
2457 * locks on the file.
2459 fsp->oplock_type = NO_OPLOCK;
2462 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
2463 new_file_created = True;
2466 set_share_mode(lck, fsp, get_current_uid(conn),
2467 req ? req->mid : 0,
2468 fsp->oplock_type);
2470 /* Handle strange delete on close create semantics. */
2471 if (create_options & FILE_DELETE_ON_CLOSE) {
2473 status = can_set_delete_on_close(fsp, new_dos_attributes);
2475 if (!NT_STATUS_IS_OK(status)) {
2476 /* Remember to delete the mode we just added. */
2477 del_share_mode(lck, fsp);
2478 TALLOC_FREE(lck);
2479 fd_close(fsp);
2480 return status;
2482 /* Note that here we set the *inital* delete on close flag,
2483 not the regular one. The magic gets handled in close. */
2484 fsp->initial_delete_on_close = True;
2487 if (new_file_created) {
2488 /* Files should be initially set as archive */
2489 if (lp_map_archive(SNUM(conn)) ||
2490 lp_store_dos_attributes(SNUM(conn))) {
2491 if (!posix_open) {
2492 if (file_set_dosmode(conn, smb_fname,
2493 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2494 parent_dir, true) == 0) {
2495 unx_mode = smb_fname->st.st_ex_mode;
2501 /* Determine sparse flag. */
2502 if (posix_open) {
2503 /* POSIX opens are sparse by default. */
2504 fsp->is_sparse = true;
2505 } else {
2506 fsp->is_sparse = (file_existed &&
2507 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2511 * Take care of inherited ACLs on created files - if default ACL not
2512 * selected.
2515 if (!posix_open && !file_existed && !def_acl) {
2517 int saved_errno = errno; /* We might get ENOSYS in the next
2518 * call.. */
2520 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2521 errno == ENOSYS) {
2522 errno = saved_errno; /* Ignore ENOSYS */
2525 } else if (new_unx_mode) {
2527 int ret = -1;
2529 /* Attributes need changing. File already existed. */
2532 int saved_errno = errno; /* We might get ENOSYS in the
2533 * next call.. */
2534 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2536 if (ret == -1 && errno == ENOSYS) {
2537 errno = saved_errno; /* Ignore ENOSYS */
2538 } else {
2539 DEBUG(5, ("open_file_ntcreate: reset "
2540 "attributes of file %s to 0%o\n",
2541 smb_fname_str_dbg(smb_fname),
2542 (unsigned int)new_unx_mode));
2543 ret = 0; /* Don't do the fchmod below. */
2547 if ((ret == -1) &&
2548 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2549 DEBUG(5, ("open_file_ntcreate: failed to reset "
2550 "attributes of file %s to 0%o\n",
2551 smb_fname_str_dbg(smb_fname),
2552 (unsigned int)new_unx_mode));
2555 /* If this is a successful open, we must remove any deferred open
2556 * records. */
2557 if (req != NULL) {
2558 del_deferred_open_entry(lck, req->mid,
2559 sconn_server_id(req->sconn));
2561 TALLOC_FREE(lck);
2563 return NT_STATUS_OK;
2567 /****************************************************************************
2568 Open a file for for write to ensure that we can fchmod it.
2569 ****************************************************************************/
2571 NTSTATUS open_file_fchmod(connection_struct *conn,
2572 struct smb_filename *smb_fname,
2573 files_struct **result)
2575 if (!VALID_STAT(smb_fname->st)) {
2576 return NT_STATUS_INVALID_PARAMETER;
2579 return SMB_VFS_CREATE_FILE(
2580 conn, /* conn */
2581 NULL, /* req */
2582 0, /* root_dir_fid */
2583 smb_fname, /* fname */
2584 FILE_WRITE_DATA, /* access_mask */
2585 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2586 FILE_SHARE_DELETE),
2587 FILE_OPEN, /* create_disposition*/
2588 0, /* create_options */
2589 0, /* file_attributes */
2590 INTERNAL_OPEN_ONLY, /* oplock_request */
2591 0, /* allocation_size */
2592 0, /* private_flags */
2593 NULL, /* sd */
2594 NULL, /* ea_list */
2595 result, /* result */
2596 NULL); /* pinfo */
2599 static NTSTATUS mkdir_internal(connection_struct *conn,
2600 struct smb_filename *smb_dname,
2601 uint32 file_attributes)
2603 mode_t mode;
2604 char *parent_dir;
2605 NTSTATUS status;
2606 bool posix_open = false;
2607 bool need_re_stat = false;
2609 if(!CAN_WRITE(conn)) {
2610 DEBUG(5,("mkdir_internal: failing create on read-only share "
2611 "%s\n", lp_servicename(SNUM(conn))));
2612 return NT_STATUS_ACCESS_DENIED;
2615 status = check_name(conn, smb_dname->base_name);
2616 if (!NT_STATUS_IS_OK(status)) {
2617 return status;
2620 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2621 NULL)) {
2622 return NT_STATUS_NO_MEMORY;
2625 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2626 posix_open = true;
2627 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2628 } else {
2629 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2632 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2633 return map_nt_error_from_unix(errno);
2636 /* Ensure we're checking for a symlink here.... */
2637 /* We don't want to get caught by a symlink racer. */
2639 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2640 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2641 smb_fname_str_dbg(smb_dname), strerror(errno)));
2642 return map_nt_error_from_unix(errno);
2645 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2646 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2647 smb_fname_str_dbg(smb_dname)));
2648 return NT_STATUS_ACCESS_DENIED;
2651 if (lp_store_dos_attributes(SNUM(conn))) {
2652 if (!posix_open) {
2653 file_set_dosmode(conn, smb_dname,
2654 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2655 parent_dir, true);
2659 if (lp_inherit_perms(SNUM(conn))) {
2660 inherit_access_posix_acl(conn, parent_dir,
2661 smb_dname->base_name, mode);
2662 need_re_stat = true;
2665 if (!posix_open) {
2667 * Check if high bits should have been set,
2668 * then (if bits are missing): add them.
2669 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2670 * dir.
2672 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2673 (mode & ~smb_dname->st.st_ex_mode)) {
2674 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2675 (smb_dname->st.st_ex_mode |
2676 (mode & ~smb_dname->st.st_ex_mode)));
2677 need_re_stat = true;
2681 /* Change the owner if required. */
2682 if (lp_inherit_owner(SNUM(conn))) {
2683 change_dir_owner_to_parent(conn, parent_dir,
2684 smb_dname->base_name,
2685 &smb_dname->st);
2686 need_re_stat = true;
2689 if (need_re_stat) {
2690 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2691 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2692 smb_fname_str_dbg(smb_dname), strerror(errno)));
2693 return map_nt_error_from_unix(errno);
2697 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2698 smb_dname->base_name);
2700 return NT_STATUS_OK;
2703 /****************************************************************************
2704 Ensure we didn't get symlink raced on opening a directory.
2705 ****************************************************************************/
2707 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2708 const SMB_STRUCT_STAT *sbuf2)
2710 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2711 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2712 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2713 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2714 return false;
2716 return true;
2719 /****************************************************************************
2720 Open a directory from an NT SMB call.
2721 ****************************************************************************/
2723 static NTSTATUS open_directory(connection_struct *conn,
2724 struct smb_request *req,
2725 struct smb_filename *smb_dname,
2726 uint32 access_mask,
2727 uint32 share_access,
2728 uint32 create_disposition,
2729 uint32 create_options,
2730 uint32 file_attributes,
2731 int *pinfo,
2732 files_struct **result)
2734 files_struct *fsp = NULL;
2735 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2736 struct share_mode_lock *lck = NULL;
2737 NTSTATUS status;
2738 struct timespec mtimespec;
2739 int info = 0;
2741 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
2743 /* Ensure we have a directory attribute. */
2744 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2746 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2747 "share_access = 0x%x create_options = 0x%x, "
2748 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2749 smb_fname_str_dbg(smb_dname),
2750 (unsigned int)access_mask,
2751 (unsigned int)share_access,
2752 (unsigned int)create_options,
2753 (unsigned int)create_disposition,
2754 (unsigned int)file_attributes));
2756 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2757 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2758 is_ntfs_stream_smb_fname(smb_dname)) {
2759 DEBUG(2, ("open_directory: %s is a stream name!\n",
2760 smb_fname_str_dbg(smb_dname)));
2761 return NT_STATUS_NOT_A_DIRECTORY;
2764 status = smbd_calculate_access_mask(conn, smb_dname, dir_existed,
2765 access_mask, &access_mask);
2766 if (!NT_STATUS_IS_OK(status)) {
2767 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2768 "on file %s returned %s\n",
2769 smb_fname_str_dbg(smb_dname),
2770 nt_errstr(status)));
2771 return status;
2774 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2775 !security_token_has_privilege(get_current_nttok(conn),
2776 SEC_PRIV_SECURITY)) {
2777 DEBUG(10, ("open_directory: open on %s "
2778 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2779 smb_fname_str_dbg(smb_dname)));
2780 return NT_STATUS_PRIVILEGE_NOT_HELD;
2783 switch( create_disposition ) {
2784 case FILE_OPEN:
2786 if (!dir_existed) {
2787 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2790 info = FILE_WAS_OPENED;
2791 break;
2793 case FILE_CREATE:
2795 /* If directory exists error. If directory doesn't
2796 * exist create. */
2798 status = mkdir_internal(conn, smb_dname,
2799 file_attributes);
2801 if (!NT_STATUS_IS_OK(status)) {
2802 DEBUG(2, ("open_directory: unable to create "
2803 "%s. Error was %s\n",
2804 smb_fname_str_dbg(smb_dname),
2805 nt_errstr(status)));
2806 return status;
2809 info = FILE_WAS_CREATED;
2810 break;
2812 case FILE_OPEN_IF:
2814 * If directory exists open. If directory doesn't
2815 * exist create.
2818 status = mkdir_internal(conn, smb_dname,
2819 file_attributes);
2821 if (NT_STATUS_IS_OK(status)) {
2822 info = FILE_WAS_CREATED;
2825 if (NT_STATUS_EQUAL(status,
2826 NT_STATUS_OBJECT_NAME_COLLISION)) {
2827 info = FILE_WAS_OPENED;
2828 status = NT_STATUS_OK;
2830 break;
2832 case FILE_SUPERSEDE:
2833 case FILE_OVERWRITE:
2834 case FILE_OVERWRITE_IF:
2835 default:
2836 DEBUG(5,("open_directory: invalid create_disposition "
2837 "0x%x for directory %s\n",
2838 (unsigned int)create_disposition,
2839 smb_fname_str_dbg(smb_dname)));
2840 return NT_STATUS_INVALID_PARAMETER;
2843 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2844 DEBUG(5,("open_directory: %s is not a directory !\n",
2845 smb_fname_str_dbg(smb_dname)));
2846 return NT_STATUS_NOT_A_DIRECTORY;
2849 if (info == FILE_WAS_OPENED) {
2850 uint32_t access_granted = 0;
2851 status = smbd_check_open_rights(conn, smb_dname, access_mask,
2852 &access_granted);
2854 /* Were we trying to do a directory open
2855 * for delete and didn't get DELETE
2856 * access (only) ? Check if the
2857 * directory allows DELETE_CHILD.
2858 * See here:
2859 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
2860 * for details. */
2862 if ((NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
2863 (access_mask & DELETE_ACCESS) &&
2864 (access_granted == DELETE_ACCESS) &&
2865 can_delete_file_in_directory(conn, smb_dname))) {
2866 DEBUG(10,("open_directory: overrode ACCESS_DENIED "
2867 "on directory %s\n",
2868 smb_fname_str_dbg(smb_dname)));
2869 status = NT_STATUS_OK;
2872 if (!NT_STATUS_IS_OK(status)) {
2873 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2874 "file %s failed with %s\n",
2875 smb_fname_str_dbg(smb_dname),
2876 nt_errstr(status)));
2877 return status;
2881 status = file_new(req, conn, &fsp);
2882 if(!NT_STATUS_IS_OK(status)) {
2883 return status;
2887 * Setup the files_struct for it.
2890 fsp->mode = smb_dname->st.st_ex_mode;
2891 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2892 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2893 fsp->file_pid = req ? req->smbpid : 0;
2894 fsp->can_lock = False;
2895 fsp->can_read = False;
2896 fsp->can_write = False;
2898 fsp->share_access = share_access;
2899 fsp->fh->private_options = 0;
2901 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2903 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2904 fsp->print_file = NULL;
2905 fsp->modified = False;
2906 fsp->oplock_type = NO_OPLOCK;
2907 fsp->sent_oplock_break = NO_BREAK_SENT;
2908 fsp->is_directory = True;
2909 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2910 status = fsp_set_smb_fname(fsp, smb_dname);
2911 if (!NT_STATUS_IS_OK(status)) {
2912 file_free(req, fsp);
2913 return status;
2916 mtimespec = smb_dname->st.st_ex_mtime;
2918 #ifdef O_DIRECTORY
2919 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2920 #else
2921 /* POSIX allows us to open a directory with O_RDONLY. */
2922 status = fd_open(conn, fsp, O_RDONLY, 0);
2923 #endif
2924 if (!NT_STATUS_IS_OK(status)) {
2925 DEBUG(5, ("open_directory: Could not open fd for "
2926 "%s (%s)\n",
2927 smb_fname_str_dbg(smb_dname),
2928 nt_errstr(status)));
2929 file_free(req, fsp);
2930 return status;
2933 status = vfs_stat_fsp(fsp);
2934 if (!NT_STATUS_IS_OK(status)) {
2935 fd_close(fsp);
2936 file_free(req, fsp);
2937 return status;
2940 /* Ensure there was no race condition. */
2941 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2942 DEBUG(5,("open_directory: stat struct differs for "
2943 "directory %s.\n",
2944 smb_fname_str_dbg(smb_dname)));
2945 fd_close(fsp);
2946 file_free(req, fsp);
2947 return NT_STATUS_ACCESS_DENIED;
2950 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2951 conn->connectpath, smb_dname, &mtimespec);
2953 if (lck == NULL) {
2954 DEBUG(0, ("open_directory: Could not get share mode lock for "
2955 "%s\n", smb_fname_str_dbg(smb_dname)));
2956 fd_close(fsp);
2957 file_free(req, fsp);
2958 return NT_STATUS_SHARING_VIOLATION;
2961 status = open_mode_check(conn, lck, fsp->name_hash,
2962 access_mask, share_access,
2963 create_options, &dir_existed);
2965 if (!NT_STATUS_IS_OK(status)) {
2966 TALLOC_FREE(lck);
2967 fd_close(fsp);
2968 file_free(req, fsp);
2969 return status;
2972 set_share_mode(lck, fsp, get_current_uid(conn),
2973 req ? req->mid : 0, NO_OPLOCK);
2975 /* For directories the delete on close bit at open time seems
2976 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2977 if (create_options & FILE_DELETE_ON_CLOSE) {
2978 status = can_set_delete_on_close(fsp, 0);
2979 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2980 TALLOC_FREE(lck);
2981 fd_close(fsp);
2982 file_free(req, fsp);
2983 return status;
2986 if (NT_STATUS_IS_OK(status)) {
2987 /* Note that here we set the *inital* delete on close flag,
2988 not the regular one. The magic gets handled in close. */
2989 fsp->initial_delete_on_close = True;
2993 TALLOC_FREE(lck);
2995 if (pinfo) {
2996 *pinfo = info;
2999 *result = fsp;
3000 return NT_STATUS_OK;
3003 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3004 struct smb_filename *smb_dname)
3006 NTSTATUS status;
3007 files_struct *fsp;
3009 status = SMB_VFS_CREATE_FILE(
3010 conn, /* conn */
3011 req, /* req */
3012 0, /* root_dir_fid */
3013 smb_dname, /* fname */
3014 FILE_READ_ATTRIBUTES, /* access_mask */
3015 FILE_SHARE_NONE, /* share_access */
3016 FILE_CREATE, /* create_disposition*/
3017 FILE_DIRECTORY_FILE, /* create_options */
3018 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3019 0, /* oplock_request */
3020 0, /* allocation_size */
3021 0, /* private_flags */
3022 NULL, /* sd */
3023 NULL, /* ea_list */
3024 &fsp, /* result */
3025 NULL); /* pinfo */
3027 if (NT_STATUS_IS_OK(status)) {
3028 close_file(req, fsp, NORMAL_CLOSE);
3031 return status;
3034 /****************************************************************************
3035 Receive notification that one of our open files has been renamed by another
3036 smbd process.
3037 ****************************************************************************/
3039 void msg_file_was_renamed(struct messaging_context *msg,
3040 void *private_data,
3041 uint32_t msg_type,
3042 struct server_id server_id,
3043 DATA_BLOB *data)
3045 struct smbd_server_connection *sconn;
3046 files_struct *fsp;
3047 char *frm = (char *)data->data;
3048 struct file_id id;
3049 const char *sharepath;
3050 const char *base_name;
3051 const char *stream_name;
3052 struct smb_filename *smb_fname = NULL;
3053 size_t sp_len, bn_len;
3054 NTSTATUS status;
3056 sconn = msg_ctx_to_sconn(msg);
3057 if (sconn == NULL) {
3058 DEBUG(1, ("could not find sconn\n"));
3059 return;
3062 if (data->data == NULL
3063 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3064 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3065 (int)data->length));
3066 return;
3069 /* Unpack the message. */
3070 pull_file_id_24(frm, &id);
3071 sharepath = &frm[24];
3072 sp_len = strlen(sharepath);
3073 base_name = sharepath + sp_len + 1;
3074 bn_len = strlen(base_name);
3075 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3077 /* stream_name must always be NULL if there is no stream. */
3078 if (stream_name[0] == '\0') {
3079 stream_name = NULL;
3082 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3083 stream_name, NULL, &smb_fname);
3084 if (!NT_STATUS_IS_OK(status)) {
3085 return;
3088 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3089 "file_id %s\n",
3090 sharepath, smb_fname_str_dbg(smb_fname),
3091 file_id_string_tos(&id)));
3093 for(fsp = file_find_di_first(sconn, id); fsp;
3094 fsp = file_find_di_next(fsp)) {
3095 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3097 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3098 fsp->fnum, fsp_str_dbg(fsp),
3099 smb_fname_str_dbg(smb_fname)));
3100 status = fsp_set_smb_fname(fsp, smb_fname);
3101 if (!NT_STATUS_IS_OK(status)) {
3102 goto out;
3104 } else {
3105 /* TODO. JRA. */
3106 /* Now we have the complete path we can work out if this is
3107 actually within this share and adjust newname accordingly. */
3108 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3109 "not sharepath %s) "
3110 "fnum %d from %s -> %s\n",
3111 fsp->conn->connectpath,
3112 sharepath,
3113 fsp->fnum,
3114 fsp_str_dbg(fsp),
3115 smb_fname_str_dbg(smb_fname)));
3118 out:
3119 TALLOC_FREE(smb_fname);
3120 return;
3124 * If a main file is opened for delete, all streams need to be checked for
3125 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3126 * If that works, delete them all by setting the delete on close and close.
3129 NTSTATUS open_streams_for_delete(connection_struct *conn,
3130 const char *fname)
3132 struct stream_struct *stream_info;
3133 files_struct **streams;
3134 int i;
3135 unsigned int num_streams;
3136 TALLOC_CTX *frame = talloc_stackframe();
3137 NTSTATUS status;
3139 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
3140 &num_streams, &stream_info);
3142 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3143 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3144 DEBUG(10, ("no streams around\n"));
3145 TALLOC_FREE(frame);
3146 return NT_STATUS_OK;
3149 if (!NT_STATUS_IS_OK(status)) {
3150 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
3151 nt_errstr(status)));
3152 goto fail;
3155 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3156 num_streams));
3158 if (num_streams == 0) {
3159 TALLOC_FREE(frame);
3160 return NT_STATUS_OK;
3163 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3164 if (streams == NULL) {
3165 DEBUG(0, ("talloc failed\n"));
3166 status = NT_STATUS_NO_MEMORY;
3167 goto fail;
3170 for (i=0; i<num_streams; i++) {
3171 struct smb_filename *smb_fname = NULL;
3173 if (strequal(stream_info[i].name, "::$DATA")) {
3174 streams[i] = NULL;
3175 continue;
3178 status = create_synthetic_smb_fname(talloc_tos(), fname,
3179 stream_info[i].name,
3180 NULL, &smb_fname);
3181 if (!NT_STATUS_IS_OK(status)) {
3182 goto fail;
3185 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3186 DEBUG(10, ("Unable to stat stream: %s\n",
3187 smb_fname_str_dbg(smb_fname)));
3190 status = SMB_VFS_CREATE_FILE(
3191 conn, /* conn */
3192 NULL, /* req */
3193 0, /* root_dir_fid */
3194 smb_fname, /* fname */
3195 DELETE_ACCESS, /* access_mask */
3196 (FILE_SHARE_READ | /* share_access */
3197 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3198 FILE_OPEN, /* create_disposition*/
3199 0, /* create_options */
3200 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3201 0, /* oplock_request */
3202 0, /* allocation_size */
3203 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3204 NULL, /* sd */
3205 NULL, /* ea_list */
3206 &streams[i], /* result */
3207 NULL); /* pinfo */
3209 if (!NT_STATUS_IS_OK(status)) {
3210 DEBUG(10, ("Could not open stream %s: %s\n",
3211 smb_fname_str_dbg(smb_fname),
3212 nt_errstr(status)));
3214 TALLOC_FREE(smb_fname);
3215 break;
3217 TALLOC_FREE(smb_fname);
3221 * don't touch the variable "status" beyond this point :-)
3224 for (i -= 1 ; i >= 0; i--) {
3225 if (streams[i] == NULL) {
3226 continue;
3229 DEBUG(10, ("Closing stream # %d, %s\n", i,
3230 fsp_str_dbg(streams[i])));
3231 close_file(NULL, streams[i], NORMAL_CLOSE);
3234 fail:
3235 TALLOC_FREE(frame);
3236 return status;
3240 * Wrapper around open_file_ntcreate and open_directory
3243 static NTSTATUS create_file_unixpath(connection_struct *conn,
3244 struct smb_request *req,
3245 struct smb_filename *smb_fname,
3246 uint32_t access_mask,
3247 uint32_t share_access,
3248 uint32_t create_disposition,
3249 uint32_t create_options,
3250 uint32_t file_attributes,
3251 uint32_t oplock_request,
3252 uint64_t allocation_size,
3253 uint32_t private_flags,
3254 struct security_descriptor *sd,
3255 struct ea_list *ea_list,
3257 files_struct **result,
3258 int *pinfo)
3260 int info = FILE_WAS_OPENED;
3261 files_struct *base_fsp = NULL;
3262 files_struct *fsp = NULL;
3263 NTSTATUS status;
3265 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3266 "file_attributes = 0x%x, share_access = 0x%x, "
3267 "create_disposition = 0x%x create_options = 0x%x "
3268 "oplock_request = 0x%x private_flags = 0x%x "
3269 "ea_list = 0x%p, sd = 0x%p, "
3270 "fname = %s\n",
3271 (unsigned int)access_mask,
3272 (unsigned int)file_attributes,
3273 (unsigned int)share_access,
3274 (unsigned int)create_disposition,
3275 (unsigned int)create_options,
3276 (unsigned int)oplock_request,
3277 (unsigned int)private_flags,
3278 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3280 if (create_options & FILE_OPEN_BY_FILE_ID) {
3281 status = NT_STATUS_NOT_SUPPORTED;
3282 goto fail;
3285 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3286 status = NT_STATUS_INVALID_PARAMETER;
3287 goto fail;
3290 if (req == NULL) {
3291 oplock_request |= INTERNAL_OPEN_ONLY;
3294 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3295 && (access_mask & DELETE_ACCESS)
3296 && !is_ntfs_stream_smb_fname(smb_fname)) {
3298 * We can't open a file with DELETE access if any of the
3299 * streams is open without FILE_SHARE_DELETE
3301 status = open_streams_for_delete(conn, smb_fname->base_name);
3303 if (!NT_STATUS_IS_OK(status)) {
3304 goto fail;
3308 /* This is the correct thing to do (check every time) but can_delete
3309 * is expensive (it may have to read the parent directory
3310 * permissions). So for now we're not doing it unless we have a strong
3311 * hint the client is really going to delete this file. If the client
3312 * is forcing FILE_CREATE let the filesystem take care of the
3313 * permissions. */
3315 /* Setting FILE_SHARE_DELETE is the hint. */
3317 if ((create_disposition != FILE_CREATE)
3318 && (access_mask & DELETE_ACCESS)
3319 && (!(can_delete_file_in_directory(conn, smb_fname) ||
3320 can_access_file_acl(conn, smb_fname, DELETE_ACCESS)))) {
3321 status = NT_STATUS_ACCESS_DENIED;
3322 DEBUG(10,("create_file_unixpath: open file %s "
3323 "for delete ACCESS_DENIED\n",
3324 smb_fname_str_dbg(smb_fname)));
3325 goto fail;
3328 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3329 !security_token_has_privilege(get_current_nttok(conn),
3330 SEC_PRIV_SECURITY)) {
3331 DEBUG(10, ("create_file_unixpath: open on %s "
3332 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3333 smb_fname_str_dbg(smb_fname)));
3334 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3335 goto fail;
3338 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3339 && is_ntfs_stream_smb_fname(smb_fname)
3340 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3341 uint32 base_create_disposition;
3342 struct smb_filename *smb_fname_base = NULL;
3344 if (create_options & FILE_DIRECTORY_FILE) {
3345 status = NT_STATUS_NOT_A_DIRECTORY;
3346 goto fail;
3349 switch (create_disposition) {
3350 case FILE_OPEN:
3351 base_create_disposition = FILE_OPEN;
3352 break;
3353 default:
3354 base_create_disposition = FILE_OPEN_IF;
3355 break;
3358 /* Create an smb_filename with stream_name == NULL. */
3359 status = create_synthetic_smb_fname(talloc_tos(),
3360 smb_fname->base_name,
3361 NULL, NULL,
3362 &smb_fname_base);
3363 if (!NT_STATUS_IS_OK(status)) {
3364 goto fail;
3367 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3368 DEBUG(10, ("Unable to stat stream: %s\n",
3369 smb_fname_str_dbg(smb_fname_base)));
3372 /* Open the base file. */
3373 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3374 FILE_SHARE_READ
3375 | FILE_SHARE_WRITE
3376 | FILE_SHARE_DELETE,
3377 base_create_disposition,
3378 0, 0, 0, 0, 0, NULL, NULL,
3379 &base_fsp, NULL);
3380 TALLOC_FREE(smb_fname_base);
3382 if (!NT_STATUS_IS_OK(status)) {
3383 DEBUG(10, ("create_file_unixpath for base %s failed: "
3384 "%s\n", smb_fname->base_name,
3385 nt_errstr(status)));
3386 goto fail;
3388 /* we don't need to low level fd */
3389 fd_close(base_fsp);
3393 * If it's a request for a directory open, deal with it separately.
3396 if (create_options & FILE_DIRECTORY_FILE) {
3398 if (create_options & FILE_NON_DIRECTORY_FILE) {
3399 status = NT_STATUS_INVALID_PARAMETER;
3400 goto fail;
3403 /* Can't open a temp directory. IFS kit test. */
3404 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3405 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3406 status = NT_STATUS_INVALID_PARAMETER;
3407 goto fail;
3411 * We will get a create directory here if the Win32
3412 * app specified a security descriptor in the
3413 * CreateDirectory() call.
3416 oplock_request = 0;
3417 status = open_directory(
3418 conn, req, smb_fname, access_mask, share_access,
3419 create_disposition, create_options, file_attributes,
3420 &info, &fsp);
3421 } else {
3424 * Ordinary file case.
3427 status = file_new(req, conn, &fsp);
3428 if(!NT_STATUS_IS_OK(status)) {
3429 goto fail;
3432 status = fsp_set_smb_fname(fsp, smb_fname);
3433 if (!NT_STATUS_IS_OK(status)) {
3434 goto fail;
3438 * We're opening the stream element of a base_fsp
3439 * we already opened. Set up the base_fsp pointer.
3441 if (base_fsp) {
3442 fsp->base_fsp = base_fsp;
3445 status = open_file_ntcreate(conn,
3446 req,
3447 access_mask,
3448 share_access,
3449 create_disposition,
3450 create_options,
3451 file_attributes,
3452 oplock_request,
3453 private_flags,
3454 &info,
3455 fsp);
3457 if(!NT_STATUS_IS_OK(status)) {
3458 file_free(req, fsp);
3459 fsp = NULL;
3462 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3464 /* A stream open never opens a directory */
3466 if (base_fsp) {
3467 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3468 goto fail;
3472 * Fail the open if it was explicitly a non-directory
3473 * file.
3476 if (create_options & FILE_NON_DIRECTORY_FILE) {
3477 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3478 goto fail;
3481 oplock_request = 0;
3482 status = open_directory(
3483 conn, req, smb_fname, access_mask,
3484 share_access, create_disposition,
3485 create_options, file_attributes,
3486 &info, &fsp);
3490 if (!NT_STATUS_IS_OK(status)) {
3491 goto fail;
3494 fsp->base_fsp = base_fsp;
3497 * According to the MS documentation, the only time the security
3498 * descriptor is applied to the opened file is iff we *created* the
3499 * file; an existing file stays the same.
3501 * Also, it seems (from observation) that you can open the file with
3502 * any access mask but you can still write the sd. We need to override
3503 * the granted access before we call set_sd
3504 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3507 if ((sd != NULL) && (info == FILE_WAS_CREATED)
3508 && lp_nt_acl_support(SNUM(conn))) {
3510 uint32_t sec_info_sent;
3511 uint32_t saved_access_mask = fsp->access_mask;
3513 sec_info_sent = get_sec_info(sd);
3515 fsp->access_mask = FILE_GENERIC_ALL;
3517 /* Convert all the generic bits. */
3518 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3519 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3521 if (sec_info_sent & (SECINFO_OWNER|
3522 SECINFO_GROUP|
3523 SECINFO_DACL|
3524 SECINFO_SACL)) {
3525 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3528 fsp->access_mask = saved_access_mask;
3530 if (!NT_STATUS_IS_OK(status)) {
3531 goto fail;
3535 if ((ea_list != NULL) &&
3536 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3537 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3538 if (!NT_STATUS_IS_OK(status)) {
3539 goto fail;
3543 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3544 status = NT_STATUS_ACCESS_DENIED;
3545 goto fail;
3548 /* Save the requested allocation size. */
3549 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3550 if (allocation_size
3551 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3552 fsp->initial_allocation_size = smb_roundup(
3553 fsp->conn, allocation_size);
3554 if (fsp->is_directory) {
3555 /* Can't set allocation size on a directory. */
3556 status = NT_STATUS_ACCESS_DENIED;
3557 goto fail;
3559 if (vfs_allocate_file_space(
3560 fsp, fsp->initial_allocation_size) == -1) {
3561 status = NT_STATUS_DISK_FULL;
3562 goto fail;
3564 } else {
3565 fsp->initial_allocation_size = smb_roundup(
3566 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3570 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3572 *result = fsp;
3573 if (pinfo != NULL) {
3574 *pinfo = info;
3577 smb_fname->st = fsp->fsp_name->st;
3579 return NT_STATUS_OK;
3581 fail:
3582 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3584 if (fsp != NULL) {
3585 if (base_fsp && fsp->base_fsp == base_fsp) {
3587 * The close_file below will close
3588 * fsp->base_fsp.
3590 base_fsp = NULL;
3592 close_file(req, fsp, ERROR_CLOSE);
3593 fsp = NULL;
3595 if (base_fsp != NULL) {
3596 close_file(req, base_fsp, ERROR_CLOSE);
3597 base_fsp = NULL;
3599 return status;
3603 * Calculate the full path name given a relative fid.
3605 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3606 struct smb_request *req,
3607 uint16_t root_dir_fid,
3608 const struct smb_filename *smb_fname,
3609 struct smb_filename **smb_fname_out)
3611 files_struct *dir_fsp;
3612 char *parent_fname = NULL;
3613 char *new_base_name = NULL;
3614 NTSTATUS status;
3616 if (root_dir_fid == 0 || !smb_fname) {
3617 status = NT_STATUS_INTERNAL_ERROR;
3618 goto out;
3621 dir_fsp = file_fsp(req, root_dir_fid);
3623 if (dir_fsp == NULL) {
3624 status = NT_STATUS_INVALID_HANDLE;
3625 goto out;
3628 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3629 status = NT_STATUS_INVALID_HANDLE;
3630 goto out;
3633 if (!dir_fsp->is_directory) {
3636 * Check to see if this is a mac fork of some kind.
3639 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3640 is_ntfs_stream_smb_fname(smb_fname)) {
3641 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3642 goto out;
3646 we need to handle the case when we get a
3647 relative open relative to a file and the
3648 pathname is blank - this is a reopen!
3649 (hint from demyn plantenberg)
3652 status = NT_STATUS_INVALID_HANDLE;
3653 goto out;
3656 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3658 * We're at the toplevel dir, the final file name
3659 * must not contain ./, as this is filtered out
3660 * normally by srvstr_get_path and unix_convert
3661 * explicitly rejects paths containing ./.
3663 parent_fname = talloc_strdup(talloc_tos(), "");
3664 if (parent_fname == NULL) {
3665 status = NT_STATUS_NO_MEMORY;
3666 goto out;
3668 } else {
3669 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3672 * Copy in the base directory name.
3675 parent_fname = talloc_array(talloc_tos(), char,
3676 dir_name_len+2);
3677 if (parent_fname == NULL) {
3678 status = NT_STATUS_NO_MEMORY;
3679 goto out;
3681 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3682 dir_name_len+1);
3685 * Ensure it ends in a '/'.
3686 * We used TALLOC_SIZE +2 to add space for the '/'.
3689 if(dir_name_len
3690 && (parent_fname[dir_name_len-1] != '\\')
3691 && (parent_fname[dir_name_len-1] != '/')) {
3692 parent_fname[dir_name_len] = '/';
3693 parent_fname[dir_name_len+1] = '\0';
3697 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3698 smb_fname->base_name);
3699 if (new_base_name == NULL) {
3700 status = NT_STATUS_NO_MEMORY;
3701 goto out;
3704 status = filename_convert(req,
3705 conn,
3706 req->flags2 & FLAGS2_DFS_PATHNAMES,
3707 new_base_name,
3709 NULL,
3710 smb_fname_out);
3711 if (!NT_STATUS_IS_OK(status)) {
3712 goto out;
3715 out:
3716 TALLOC_FREE(parent_fname);
3717 TALLOC_FREE(new_base_name);
3718 return status;
3721 NTSTATUS create_file_default(connection_struct *conn,
3722 struct smb_request *req,
3723 uint16_t root_dir_fid,
3724 struct smb_filename *smb_fname,
3725 uint32_t access_mask,
3726 uint32_t share_access,
3727 uint32_t create_disposition,
3728 uint32_t create_options,
3729 uint32_t file_attributes,
3730 uint32_t oplock_request,
3731 uint64_t allocation_size,
3732 uint32_t private_flags,
3733 struct security_descriptor *sd,
3734 struct ea_list *ea_list,
3735 files_struct **result,
3736 int *pinfo)
3738 int info = FILE_WAS_OPENED;
3739 files_struct *fsp = NULL;
3740 NTSTATUS status;
3741 bool stream_name = false;
3743 DEBUG(10,("create_file: access_mask = 0x%x "
3744 "file_attributes = 0x%x, share_access = 0x%x, "
3745 "create_disposition = 0x%x create_options = 0x%x "
3746 "oplock_request = 0x%x "
3747 "private_flags = 0x%x "
3748 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3749 "fname = %s\n",
3750 (unsigned int)access_mask,
3751 (unsigned int)file_attributes,
3752 (unsigned int)share_access,
3753 (unsigned int)create_disposition,
3754 (unsigned int)create_options,
3755 (unsigned int)oplock_request,
3756 (unsigned int)private_flags,
3757 (unsigned int)root_dir_fid,
3758 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3761 * Calculate the filename from the root_dir_if if necessary.
3764 if (root_dir_fid != 0) {
3765 struct smb_filename *smb_fname_out = NULL;
3766 status = get_relative_fid_filename(conn, req, root_dir_fid,
3767 smb_fname, &smb_fname_out);
3768 if (!NT_STATUS_IS_OK(status)) {
3769 goto fail;
3771 smb_fname = smb_fname_out;
3775 * Check to see if this is a mac fork of some kind.
3778 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3779 if (stream_name) {
3780 enum FAKE_FILE_TYPE fake_file_type;
3782 fake_file_type = is_fake_file(smb_fname);
3784 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3787 * Here we go! support for changing the disk quotas
3788 * --metze
3790 * We need to fake up to open this MAGIC QUOTA file
3791 * and return a valid FID.
3793 * w2k close this file directly after openening xp
3794 * also tries a QUERY_FILE_INFO on the file and then
3795 * close it
3797 status = open_fake_file(req, conn, req->vuid,
3798 fake_file_type, smb_fname,
3799 access_mask, &fsp);
3800 if (!NT_STATUS_IS_OK(status)) {
3801 goto fail;
3804 ZERO_STRUCT(smb_fname->st);
3805 goto done;
3808 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3809 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3810 goto fail;
3814 /* All file access must go through check_name() */
3816 status = check_name(conn, smb_fname->base_name);
3817 if (!NT_STATUS_IS_OK(status)) {
3818 goto fail;
3821 if (stream_name && is_ntfs_default_stream_smb_fname(smb_fname)) {
3822 int ret;
3823 smb_fname->stream_name = NULL;
3824 /* We have to handle this error here. */
3825 if (create_options & FILE_DIRECTORY_FILE) {
3826 status = NT_STATUS_NOT_A_DIRECTORY;
3827 goto fail;
3829 if (lp_posix_pathnames()) {
3830 ret = SMB_VFS_LSTAT(conn, smb_fname);
3831 } else {
3832 ret = SMB_VFS_STAT(conn, smb_fname);
3835 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3836 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3837 goto fail;
3841 status = create_file_unixpath(
3842 conn, req, smb_fname, access_mask, share_access,
3843 create_disposition, create_options, file_attributes,
3844 oplock_request, allocation_size, private_flags,
3845 sd, ea_list,
3846 &fsp, &info);
3848 if (!NT_STATUS_IS_OK(status)) {
3849 goto fail;
3852 done:
3853 DEBUG(10, ("create_file: info=%d\n", info));
3855 *result = fsp;
3856 if (pinfo != NULL) {
3857 *pinfo = info;
3859 return NT_STATUS_OK;
3861 fail:
3862 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3864 if (fsp != NULL) {
3865 close_file(req, fsp, ERROR_CLOSE);
3866 fsp = NULL;
3868 return status;