s3:smbd: make smbd_calculate_access_mask() non-static
[Samba.git] / source3 / smbd / open.c
blob58102e4c9ad0f3463b53ee4bfbbc12e9eded4745
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 goto chdir;
371 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
372 "directory %s to parent directory uid %u.\n",
373 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
375 chdir:
376 vfs_ChDir(conn,saved_dir);
377 out:
378 TALLOC_FREE(smb_fname_parent);
379 TALLOC_FREE(smb_fname_cwd);
380 return status;
383 /****************************************************************************
384 Open a file.
385 ****************************************************************************/
387 static NTSTATUS open_file(files_struct *fsp,
388 connection_struct *conn,
389 struct smb_request *req,
390 const char *parent_dir,
391 int flags,
392 mode_t unx_mode,
393 uint32 access_mask, /* client requested access mask. */
394 uint32 open_access_mask) /* what we're actually using in the open. */
396 struct smb_filename *smb_fname = fsp->fsp_name;
397 NTSTATUS status = NT_STATUS_OK;
398 int accmode = (flags & O_ACCMODE);
399 int local_flags = flags;
400 bool file_existed = VALID_STAT(fsp->fsp_name->st);
401 bool file_created = false;
403 fsp->fh->fd = -1;
404 errno = EPERM;
406 /* Check permissions */
409 * This code was changed after seeing a client open request
410 * containing the open mode of (DENY_WRITE/read-only) with
411 * the 'create if not exist' bit set. The previous code
412 * would fail to open the file read only on a read-only share
413 * as it was checking the flags parameter directly against O_RDONLY,
414 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
415 * JRA.
418 if (!CAN_WRITE(conn)) {
419 /* It's a read-only share - fail if we wanted to write. */
420 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
421 DEBUG(3,("Permission denied opening %s\n",
422 smb_fname_str_dbg(smb_fname)));
423 return NT_STATUS_ACCESS_DENIED;
424 } else if(flags & O_CREAT) {
425 /* We don't want to write - but we must make sure that
426 O_CREAT doesn't create the file if we have write
427 access into the directory.
429 flags &= ~(O_CREAT|O_EXCL);
430 local_flags &= ~(O_CREAT|O_EXCL);
435 * This little piece of insanity is inspired by the
436 * fact that an NT client can open a file for O_RDONLY,
437 * but set the create disposition to FILE_EXISTS_TRUNCATE.
438 * If the client *can* write to the file, then it expects to
439 * truncate the file, even though it is opening for readonly.
440 * Quicken uses this stupid trick in backup file creation...
441 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
442 * for helping track this one down. It didn't bite us in 2.0.x
443 * as we always opened files read-write in that release. JRA.
446 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
447 DEBUG(10,("open_file: truncate requested on read-only open "
448 "for file %s\n", smb_fname_str_dbg(smb_fname)));
449 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
452 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
453 (!file_existed && (local_flags & O_CREAT)) ||
454 ((local_flags & O_TRUNC) == O_TRUNC) ) {
455 const char *wild;
458 * We can't actually truncate here as the file may be locked.
459 * open_file_ntcreate will take care of the truncate later. JRA.
462 local_flags &= ~O_TRUNC;
464 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
466 * We would block on opening a FIFO with no one else on the
467 * other end. Do what we used to do and add O_NONBLOCK to the
468 * open flags. JRA.
471 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
472 local_flags |= O_NONBLOCK;
474 #endif
476 /* Don't create files with Microsoft wildcard characters. */
477 if (fsp->base_fsp) {
479 * wildcard characters are allowed in stream names
480 * only test the basefilename
482 wild = fsp->base_fsp->fsp_name->base_name;
483 } else {
484 wild = smb_fname->base_name;
486 if ((local_flags & O_CREAT) && !file_existed &&
487 ms_has_wild(wild)) {
488 return NT_STATUS_OBJECT_NAME_INVALID;
491 /* Actually do the open */
492 status = fd_open(conn, fsp, local_flags, unx_mode);
493 if (!NT_STATUS_IS_OK(status)) {
494 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
495 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
496 nt_errstr(status),local_flags,flags));
497 return status;
500 if ((local_flags & O_CREAT) && !file_existed) {
501 file_created = true;
504 } else {
505 fsp->fh->fd = -1; /* What we used to call a stat open. */
506 if (file_existed) {
507 uint32_t access_granted = 0;
509 status = smbd_check_open_rights(conn,
510 smb_fname,
511 access_mask,
512 &access_granted);
513 if (!NT_STATUS_IS_OK(status)) {
514 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
516 * On NT_STATUS_ACCESS_DENIED, access_granted
517 * contains the denied bits.
520 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
521 (access_granted & FILE_WRITE_ATTRIBUTES) &&
522 (lp_map_readonly(SNUM(conn)) ||
523 lp_map_archive(SNUM(conn)) ||
524 lp_map_hidden(SNUM(conn)) ||
525 lp_map_system(SNUM(conn)))) {
526 access_granted &= ~FILE_WRITE_ATTRIBUTES;
528 DEBUG(10,("open_file: "
529 "overrode "
530 "FILE_WRITE_"
531 "ATTRIBUTES "
532 "on file %s\n",
533 smb_fname_str_dbg(
534 smb_fname)));
537 if ((access_mask & DELETE_ACCESS) &&
538 (access_granted & DELETE_ACCESS) &&
539 can_delete_file_in_directory(conn,
540 smb_fname)) {
541 /* Were we trying to do a stat open
542 * for delete and didn't get DELETE
543 * access (only) ? Check if the
544 * directory allows DELETE_CHILD.
545 * See here:
546 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
547 * for details. */
549 access_granted &= ~DELETE_ACCESS;
551 DEBUG(10,("open_file: "
552 "overrode "
553 "DELETE_ACCESS on "
554 "file %s\n",
555 smb_fname_str_dbg(
556 smb_fname)));
559 if (access_granted != 0) {
560 DEBUG(10,("open_file: Access "
561 "denied on file "
562 "%s\n",
563 smb_fname_str_dbg(
564 smb_fname)));
565 return status;
567 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
568 fsp->posix_open &&
569 S_ISLNK(smb_fname->st.st_ex_mode)) {
570 /* This is a POSIX stat open for delete
571 * or rename on a symlink that points
572 * nowhere. Allow. */
573 DEBUG(10,("open_file: allowing POSIX "
574 "open on bad symlink %s\n",
575 smb_fname_str_dbg(
576 smb_fname)));
577 } else {
578 DEBUG(10,("open_file: "
579 "smbd_check_open_rights on file "
580 "%s returned %s\n",
581 smb_fname_str_dbg(smb_fname),
582 nt_errstr(status) ));
583 return status;
589 if (!file_existed) {
590 int ret;
592 if (fsp->fh->fd == -1) {
593 ret = SMB_VFS_STAT(conn, smb_fname);
594 } else {
595 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
596 /* If we have an fd, this stat should succeed. */
597 if (ret == -1) {
598 DEBUG(0,("Error doing fstat on open file %s "
599 "(%s)\n",
600 smb_fname_str_dbg(smb_fname),
601 strerror(errno) ));
605 /* For a non-io open, this stat failing means file not found. JRA */
606 if (ret == -1) {
607 status = map_nt_error_from_unix(errno);
608 fd_close(fsp);
609 return status;
612 if (file_created) {
613 bool need_re_stat = false;
614 /* Do all inheritance work after we've
615 done a successful stat call and filled
616 in the stat struct in fsp->fsp_name. */
618 /* Inherit the ACL if required */
619 if (lp_inherit_perms(SNUM(conn))) {
620 inherit_access_posix_acl(conn, parent_dir,
621 smb_fname->base_name,
622 unx_mode);
623 need_re_stat = true;
626 /* Change the owner if required. */
627 if (lp_inherit_owner(SNUM(conn))) {
628 change_file_owner_to_parent(conn, parent_dir,
629 fsp);
630 need_re_stat = true;
633 if (need_re_stat) {
634 if (fsp->fh->fd == -1) {
635 ret = SMB_VFS_STAT(conn, smb_fname);
636 } else {
637 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
638 /* If we have an fd, this stat should succeed. */
639 if (ret == -1) {
640 DEBUG(0,("Error doing fstat on open file %s "
641 "(%s)\n",
642 smb_fname_str_dbg(smb_fname),
643 strerror(errno) ));
648 notify_fname(conn, NOTIFY_ACTION_ADDED,
649 FILE_NOTIFY_CHANGE_FILE_NAME,
650 smb_fname->base_name);
655 * POSIX allows read-only opens of directories. We don't
656 * want to do this (we use a different code path for this)
657 * so catch a directory open and return an EISDIR. JRA.
660 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
661 fd_close(fsp);
662 errno = EISDIR;
663 return NT_STATUS_FILE_IS_A_DIRECTORY;
666 fsp->mode = smb_fname->st.st_ex_mode;
667 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
668 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
669 fsp->file_pid = req ? req->smbpid : 0;
670 fsp->can_lock = True;
671 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
672 if (!CAN_WRITE(conn)) {
673 fsp->can_write = False;
674 } else {
675 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
676 True : False;
678 fsp->print_file = NULL;
679 fsp->modified = False;
680 fsp->sent_oplock_break = NO_BREAK_SENT;
681 fsp->is_directory = False;
682 if (conn->aio_write_behind_list &&
683 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
684 conn->case_sensitive)) {
685 fsp->aio_write_behind = True;
688 fsp->wcp = NULL; /* Write cache pointer. */
690 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
691 conn->session_info->unix_name,
692 smb_fname_str_dbg(smb_fname),
693 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
694 conn->num_files_open));
696 errno = 0;
697 return NT_STATUS_OK;
700 /*******************************************************************
701 Return True if the filename is one of the special executable types.
702 ********************************************************************/
704 bool is_executable(const char *fname)
706 if ((fname = strrchr_m(fname,'.'))) {
707 if (strequal(fname,".com") ||
708 strequal(fname,".dll") ||
709 strequal(fname,".exe") ||
710 strequal(fname,".sym")) {
711 return True;
714 return False;
717 /****************************************************************************
718 Check if we can open a file with a share mode.
719 Returns True if conflict, False if not.
720 ****************************************************************************/
722 static bool share_conflict(struct share_mode_entry *entry,
723 uint32 access_mask,
724 uint32 share_access)
726 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
727 "entry->share_access = 0x%x, "
728 "entry->private_options = 0x%x\n",
729 (unsigned int)entry->access_mask,
730 (unsigned int)entry->share_access,
731 (unsigned int)entry->private_options));
733 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
734 (unsigned int)access_mask, (unsigned int)share_access));
736 if ((entry->access_mask & (FILE_WRITE_DATA|
737 FILE_APPEND_DATA|
738 FILE_READ_DATA|
739 FILE_EXECUTE|
740 DELETE_ACCESS)) == 0) {
741 DEBUG(10,("share_conflict: No conflict due to "
742 "entry->access_mask = 0x%x\n",
743 (unsigned int)entry->access_mask ));
744 return False;
747 if ((access_mask & (FILE_WRITE_DATA|
748 FILE_APPEND_DATA|
749 FILE_READ_DATA|
750 FILE_EXECUTE|
751 DELETE_ACCESS)) == 0) {
752 DEBUG(10,("share_conflict: No conflict due to "
753 "access_mask = 0x%x\n",
754 (unsigned int)access_mask ));
755 return False;
758 #if 1 /* JRA TEST - Superdebug. */
759 #define CHECK_MASK(num, am, right, sa, share) \
760 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
761 (unsigned int)(num), (unsigned int)(am), \
762 (unsigned int)(right), (unsigned int)(am)&(right) )); \
763 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
764 (unsigned int)(num), (unsigned int)(sa), \
765 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
766 if (((am) & (right)) && !((sa) & (share))) { \
767 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
768 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
769 (unsigned int)(share) )); \
770 return True; \
772 #else
773 #define CHECK_MASK(num, am, right, sa, share) \
774 if (((am) & (right)) && !((sa) & (share))) { \
775 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
776 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
777 (unsigned int)(share) )); \
778 return True; \
780 #endif
782 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
783 share_access, FILE_SHARE_WRITE);
784 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
785 entry->share_access, FILE_SHARE_WRITE);
787 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
788 share_access, FILE_SHARE_READ);
789 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
790 entry->share_access, FILE_SHARE_READ);
792 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
793 share_access, FILE_SHARE_DELETE);
794 CHECK_MASK(6, access_mask, DELETE_ACCESS,
795 entry->share_access, FILE_SHARE_DELETE);
797 DEBUG(10,("share_conflict: No conflict.\n"));
798 return False;
801 #if defined(DEVELOPER)
802 static void validate_my_share_entries(struct smbd_server_connection *sconn,
803 int num,
804 struct share_mode_entry *share_entry)
806 files_struct *fsp;
808 if (!procid_is_me(&share_entry->pid)) {
809 return;
812 if (is_deferred_open_entry(share_entry) &&
813 !open_was_deferred(share_entry->op_mid)) {
814 char *str = talloc_asprintf(talloc_tos(),
815 "Got a deferred entry without a request: "
816 "PANIC: %s\n",
817 share_mode_str(talloc_tos(), num, share_entry));
818 smb_panic(str);
821 if (!is_valid_share_mode_entry(share_entry)) {
822 return;
825 fsp = file_find_dif(sconn, share_entry->id,
826 share_entry->share_file_id);
827 if (!fsp) {
828 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
829 share_mode_str(talloc_tos(), num, share_entry) ));
830 smb_panic("validate_my_share_entries: Cannot match a "
831 "share entry with an open file\n");
834 if (is_deferred_open_entry(share_entry) ||
835 is_unused_share_mode_entry(share_entry)) {
836 goto panic;
839 if ((share_entry->op_type == NO_OPLOCK) &&
840 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
841 /* Someone has already written to it, but I haven't yet
842 * noticed */
843 return;
846 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
847 goto panic;
850 return;
852 panic:
854 char *str;
855 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
856 share_mode_str(talloc_tos(), num, share_entry) ));
857 str = talloc_asprintf(talloc_tos(),
858 "validate_my_share_entries: "
859 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
860 fsp->fsp_name->base_name,
861 (unsigned int)fsp->oplock_type,
862 (unsigned int)share_entry->op_type );
863 smb_panic(str);
866 #endif
868 bool is_stat_open(uint32 access_mask)
870 return (access_mask &&
871 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
872 FILE_WRITE_ATTRIBUTES))==0) &&
873 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
874 FILE_WRITE_ATTRIBUTES)) != 0));
877 /****************************************************************************
878 Deal with share modes
879 Invarient: Share mode must be locked on entry and exit.
880 Returns -1 on error, or number of share modes on success (may be zero).
881 ****************************************************************************/
883 static NTSTATUS open_mode_check(connection_struct *conn,
884 struct share_mode_lock *lck,
885 uint32_t name_hash,
886 uint32 access_mask,
887 uint32 share_access,
888 uint32 create_options,
889 bool *file_existed)
891 int i;
893 if(lck->num_share_modes == 0) {
894 return NT_STATUS_OK;
897 *file_existed = True;
899 /* A delete on close prohibits everything */
901 if (is_delete_on_close_set(lck, name_hash)) {
902 return NT_STATUS_DELETE_PENDING;
905 if (is_stat_open(access_mask)) {
906 /* Stat open that doesn't trigger oplock breaks or share mode
907 * checks... ! JRA. */
908 return NT_STATUS_OK;
912 * Check if the share modes will give us access.
915 #if defined(DEVELOPER)
916 for(i = 0; i < lck->num_share_modes; i++) {
917 validate_my_share_entries(conn->sconn, i,
918 &lck->share_modes[i]);
920 #endif
922 if (!lp_share_modes(SNUM(conn))) {
923 return NT_STATUS_OK;
926 /* Now we check the share modes, after any oplock breaks. */
927 for(i = 0; i < lck->num_share_modes; i++) {
929 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
930 continue;
933 /* someone else has a share lock on it, check to see if we can
934 * too */
935 if (share_conflict(&lck->share_modes[i],
936 access_mask, share_access)) {
937 return NT_STATUS_SHARING_VIOLATION;
941 return NT_STATUS_OK;
944 static bool is_delete_request(files_struct *fsp) {
945 return ((fsp->access_mask == DELETE_ACCESS) &&
946 (fsp->oplock_type == NO_OPLOCK));
950 * Send a break message to the oplock holder and delay the open for
951 * our client.
954 static NTSTATUS send_break_message(files_struct *fsp,
955 struct share_mode_entry *exclusive,
956 uint64_t mid,
957 int oplock_request)
959 NTSTATUS status;
960 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
962 DEBUG(10, ("Sending break request to PID %s\n",
963 procid_str_static(&exclusive->pid)));
964 exclusive->op_mid = mid;
966 /* Create the message. */
967 share_mode_entry_to_message(msg, exclusive);
969 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
970 don't want this set in the share mode struct pointed to by lck. */
972 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
973 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
974 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
977 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
978 MSG_SMB_BREAK_REQUEST,
979 (uint8 *)msg,
980 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
981 if (!NT_STATUS_IS_OK(status)) {
982 DEBUG(3, ("Could not send oplock break message: %s\n",
983 nt_errstr(status)));
986 return status;
990 * Return share_mode_entry pointers for :
991 * 1). Batch oplock entry.
992 * 2). Batch or exclusive oplock entry (may be identical to #1).
993 * bool have_level2_oplock
994 * bool have_no_oplock.
995 * Do internal consistency checks on the share mode for a file.
998 static void find_oplock_types(files_struct *fsp,
999 int oplock_request,
1000 struct share_mode_lock *lck,
1001 struct share_mode_entry **pp_batch,
1002 struct share_mode_entry **pp_ex_or_batch,
1003 bool *got_level2,
1004 bool *got_no_oplock)
1006 int i;
1008 *pp_batch = NULL;
1009 *pp_ex_or_batch = NULL;
1010 *got_level2 = false;
1011 *got_no_oplock = false;
1013 /* Ignore stat or internal opens, as is done in
1014 delay_for_batch_oplocks() and
1015 delay_for_exclusive_oplocks().
1017 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1018 return;
1021 for (i=0; i<lck->num_share_modes; i++) {
1022 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
1023 continue;
1026 if (lck->share_modes[i].op_type == NO_OPLOCK &&
1027 is_stat_open(lck->share_modes[i].access_mask)) {
1028 /* We ignore stat opens in the table - they
1029 always have NO_OPLOCK and never get or
1030 cause breaks. JRA. */
1031 continue;
1034 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1035 /* batch - can only be one. */
1036 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1037 smb_panic("Bad batch oplock entry.");
1039 *pp_batch = &lck->share_modes[i];
1042 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1043 /* Exclusive or batch - can only be one. */
1044 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1045 smb_panic("Bad exclusive or batch oplock entry.");
1047 *pp_ex_or_batch = &lck->share_modes[i];
1050 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1051 if (*pp_batch || *pp_ex_or_batch) {
1052 smb_panic("Bad levelII oplock entry.");
1054 *got_level2 = true;
1057 if (lck->share_modes[i].op_type == NO_OPLOCK) {
1058 if (*pp_batch || *pp_ex_or_batch) {
1059 smb_panic("Bad no oplock entry.");
1061 *got_no_oplock = true;
1066 static bool delay_for_batch_oplocks(files_struct *fsp,
1067 uint64_t mid,
1068 int oplock_request,
1069 struct share_mode_entry *batch_entry)
1071 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1072 return false;
1075 if (batch_entry != NULL) {
1076 /* Found a batch oplock */
1077 send_break_message(fsp, batch_entry, mid, oplock_request);
1078 return true;
1080 return false;
1083 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1084 uint64_t mid,
1085 int oplock_request,
1086 struct share_mode_entry *ex_entry)
1088 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1089 return false;
1092 if (ex_entry != NULL) {
1093 /* Found an exclusive or batch oplock */
1094 bool delay_it = is_delete_request(fsp) ?
1095 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1096 if (delay_it) {
1097 send_break_message(fsp, ex_entry, mid, oplock_request);
1098 return true;
1101 return false;
1104 static void grant_fsp_oplock_type(files_struct *fsp,
1105 const struct byte_range_lock *br_lck,
1106 int oplock_request,
1107 bool got_level2_oplock,
1108 bool got_a_none_oplock)
1110 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1111 lp_level2_oplocks(SNUM(fsp->conn));
1113 /* Start by granting what the client asked for,
1114 but ensure no SAMBA_PRIVATE bits can be set. */
1115 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1117 if (oplock_request & INTERNAL_OPEN_ONLY) {
1118 /* No oplocks on internal open. */
1119 fsp->oplock_type = NO_OPLOCK;
1120 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1121 fsp->oplock_type, fsp_str_dbg(fsp)));
1122 return;
1123 } else if (br_lck && br_lck->num_locks > 0) {
1124 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1125 fsp_str_dbg(fsp)));
1126 fsp->oplock_type = NO_OPLOCK;
1129 if (is_stat_open(fsp->access_mask)) {
1130 /* Leave the value already set. */
1131 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1132 fsp->oplock_type, fsp_str_dbg(fsp)));
1133 return;
1137 * Match what was requested (fsp->oplock_type) with
1138 * what was found in the existing share modes.
1141 if (got_a_none_oplock) {
1142 fsp->oplock_type = NO_OPLOCK;
1143 } else if (got_level2_oplock) {
1144 if (fsp->oplock_type == NO_OPLOCK ||
1145 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1146 /* Store a level2 oplock, but don't tell the client */
1147 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1148 } else {
1149 fsp->oplock_type = LEVEL_II_OPLOCK;
1151 } else {
1152 /* All share_mode_entries are placeholders or deferred.
1153 * Silently upgrade to fake levelII if the client didn't
1154 * ask for an oplock. */
1155 if (fsp->oplock_type == NO_OPLOCK) {
1156 /* Store a level2 oplock, but don't tell the client */
1157 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1162 * Don't grant level2 to clients that don't want them
1163 * or if we've turned them off.
1165 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1166 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1169 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1170 fsp->oplock_type, fsp_str_dbg(fsp)));
1173 bool request_timed_out(struct timeval request_time,
1174 struct timeval timeout)
1176 struct timeval now, end_time;
1177 GetTimeOfDay(&now);
1178 end_time = timeval_sum(&request_time, &timeout);
1179 return (timeval_compare(&end_time, &now) < 0);
1182 /****************************************************************************
1183 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1184 ****************************************************************************/
1186 static void defer_open(struct share_mode_lock *lck,
1187 struct timeval request_time,
1188 struct timeval timeout,
1189 struct smb_request *req,
1190 struct deferred_open_record *state)
1192 int i;
1194 /* Paranoia check */
1196 for (i=0; i<lck->num_share_modes; i++) {
1197 struct share_mode_entry *e = &lck->share_modes[i];
1199 if (!is_deferred_open_entry(e)) {
1200 continue;
1203 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
1204 DEBUG(0, ("Trying to defer an already deferred "
1205 "request: mid=%llu, exiting\n",
1206 (unsigned long long)req->mid));
1207 exit_server("attempt to defer a deferred request");
1211 /* End paranoia check */
1213 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1214 "open entry for mid %llu\n",
1215 (unsigned int)request_time.tv_sec,
1216 (unsigned int)request_time.tv_usec,
1217 (unsigned long long)req->mid));
1219 if (!push_deferred_open_message_smb(req, request_time, timeout,
1220 state->id, (char *)state, sizeof(*state))) {
1221 exit_server("push_deferred_open_message_smb failed");
1223 add_deferred_open(lck, req->mid, request_time,
1224 sconn_server_id(req->sconn), state->id);
1228 /****************************************************************************
1229 On overwrite open ensure that the attributes match.
1230 ****************************************************************************/
1232 bool open_match_attributes(connection_struct *conn,
1233 uint32 old_dos_attr,
1234 uint32 new_dos_attr,
1235 mode_t existing_unx_mode,
1236 mode_t new_unx_mode,
1237 mode_t *returned_unx_mode)
1239 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1241 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1242 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1244 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1245 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1246 *returned_unx_mode = new_unx_mode;
1247 } else {
1248 *returned_unx_mode = (mode_t)0;
1251 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1252 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1253 "returned_unx_mode = 0%o\n",
1254 (unsigned int)old_dos_attr,
1255 (unsigned int)existing_unx_mode,
1256 (unsigned int)new_dos_attr,
1257 (unsigned int)*returned_unx_mode ));
1259 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1260 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1261 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1262 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1263 return False;
1266 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1267 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1268 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1269 return False;
1272 return True;
1275 /****************************************************************************
1276 Special FCB or DOS processing in the case of a sharing violation.
1277 Try and find a duplicated file handle.
1278 ****************************************************************************/
1280 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1281 connection_struct *conn,
1282 files_struct *fsp_to_dup_into,
1283 const struct smb_filename *smb_fname,
1284 struct file_id id,
1285 uint16 file_pid,
1286 uint16 vuid,
1287 uint32 access_mask,
1288 uint32 share_access,
1289 uint32 create_options)
1291 files_struct *fsp;
1293 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1294 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1296 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1297 fsp = file_find_di_next(fsp)) {
1299 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1300 "vuid = %u, file_pid = %u, private_options = 0x%x "
1301 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1302 fsp->fh->fd, (unsigned int)fsp->vuid,
1303 (unsigned int)fsp->file_pid,
1304 (unsigned int)fsp->fh->private_options,
1305 (unsigned int)fsp->access_mask ));
1307 if (fsp->fh->fd != -1 &&
1308 fsp->vuid == vuid &&
1309 fsp->file_pid == file_pid &&
1310 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1311 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1312 (fsp->access_mask & FILE_WRITE_DATA) &&
1313 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1314 strequal(fsp->fsp_name->stream_name,
1315 smb_fname->stream_name)) {
1316 DEBUG(10,("fcb_or_dos_open: file match\n"));
1317 break;
1321 if (!fsp) {
1322 return NT_STATUS_NOT_FOUND;
1325 /* quite an insane set of semantics ... */
1326 if (is_executable(smb_fname->base_name) &&
1327 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1328 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1329 return NT_STATUS_INVALID_PARAMETER;
1332 /* We need to duplicate this fsp. */
1333 return dup_file_fsp(req, fsp, access_mask, share_access,
1334 create_options, fsp_to_dup_into);
1337 /****************************************************************************
1338 Open a file with a share mode - old openX method - map into NTCreate.
1339 ****************************************************************************/
1341 bool map_open_params_to_ntcreate(const struct smb_filename *smb_fname,
1342 int deny_mode, int open_func,
1343 uint32 *paccess_mask,
1344 uint32 *pshare_mode,
1345 uint32 *pcreate_disposition,
1346 uint32 *pcreate_options,
1347 uint32_t *pprivate_flags)
1349 uint32 access_mask;
1350 uint32 share_mode;
1351 uint32 create_disposition;
1352 uint32 create_options = FILE_NON_DIRECTORY_FILE;
1353 uint32_t private_flags = 0;
1355 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1356 "open_func = 0x%x\n",
1357 smb_fname_str_dbg(smb_fname), (unsigned int)deny_mode,
1358 (unsigned int)open_func ));
1360 /* Create the NT compatible access_mask. */
1361 switch (GET_OPENX_MODE(deny_mode)) {
1362 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1363 case DOS_OPEN_RDONLY:
1364 access_mask = FILE_GENERIC_READ;
1365 break;
1366 case DOS_OPEN_WRONLY:
1367 access_mask = FILE_GENERIC_WRITE;
1368 break;
1369 case DOS_OPEN_RDWR:
1370 case DOS_OPEN_FCB:
1371 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1372 break;
1373 default:
1374 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1375 (unsigned int)GET_OPENX_MODE(deny_mode)));
1376 return False;
1379 /* Create the NT compatible create_disposition. */
1380 switch (open_func) {
1381 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1382 create_disposition = FILE_CREATE;
1383 break;
1385 case OPENX_FILE_EXISTS_OPEN:
1386 create_disposition = FILE_OPEN;
1387 break;
1389 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1390 create_disposition = FILE_OPEN_IF;
1391 break;
1393 case OPENX_FILE_EXISTS_TRUNCATE:
1394 create_disposition = FILE_OVERWRITE;
1395 break;
1397 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1398 create_disposition = FILE_OVERWRITE_IF;
1399 break;
1401 default:
1402 /* From samba4 - to be confirmed. */
1403 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1404 create_disposition = FILE_CREATE;
1405 break;
1407 DEBUG(10,("map_open_params_to_ntcreate: bad "
1408 "open_func 0x%x\n", (unsigned int)open_func));
1409 return False;
1412 /* Create the NT compatible share modes. */
1413 switch (GET_DENY_MODE(deny_mode)) {
1414 case DENY_ALL:
1415 share_mode = FILE_SHARE_NONE;
1416 break;
1418 case DENY_WRITE:
1419 share_mode = FILE_SHARE_READ;
1420 break;
1422 case DENY_READ:
1423 share_mode = FILE_SHARE_WRITE;
1424 break;
1426 case DENY_NONE:
1427 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1428 break;
1430 case DENY_DOS:
1431 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1432 if (is_executable(smb_fname->base_name)) {
1433 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1434 } else {
1435 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1436 share_mode = FILE_SHARE_READ;
1437 } else {
1438 share_mode = FILE_SHARE_NONE;
1441 break;
1443 case DENY_FCB:
1444 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1445 share_mode = FILE_SHARE_NONE;
1446 break;
1448 default:
1449 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1450 (unsigned int)GET_DENY_MODE(deny_mode) ));
1451 return False;
1454 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1455 "share_mode = 0x%x, create_disposition = 0x%x, "
1456 "create_options = 0x%x private_flags = 0x%x\n",
1457 smb_fname_str_dbg(smb_fname),
1458 (unsigned int)access_mask,
1459 (unsigned int)share_mode,
1460 (unsigned int)create_disposition,
1461 (unsigned int)create_options,
1462 (unsigned int)private_flags));
1464 if (paccess_mask) {
1465 *paccess_mask = access_mask;
1467 if (pshare_mode) {
1468 *pshare_mode = share_mode;
1470 if (pcreate_disposition) {
1471 *pcreate_disposition = create_disposition;
1473 if (pcreate_options) {
1474 *pcreate_options = create_options;
1476 if (pprivate_flags) {
1477 *pprivate_flags = private_flags;
1480 return True;
1484 static void schedule_defer_open(struct share_mode_lock *lck,
1485 struct timeval request_time,
1486 struct smb_request *req)
1488 struct deferred_open_record state;
1490 /* This is a relative time, added to the absolute
1491 request_time value to get the absolute timeout time.
1492 Note that if this is the second or greater time we enter
1493 this codepath for this particular request mid then
1494 request_time is left as the absolute time of the *first*
1495 time this request mid was processed. This is what allows
1496 the request to eventually time out. */
1498 struct timeval timeout;
1500 /* Normally the smbd we asked should respond within
1501 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1502 * the client did, give twice the timeout as a safety
1503 * measure here in case the other smbd is stuck
1504 * somewhere else. */
1506 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1508 /* Nothing actually uses state.delayed_for_oplocks
1509 but it's handy to differentiate in debug messages
1510 between a 30 second delay due to oplock break, and
1511 a 1 second delay for share mode conflicts. */
1513 state.delayed_for_oplocks = True;
1514 state.id = lck->id;
1516 if (!request_timed_out(request_time, timeout)) {
1517 defer_open(lck, request_time, timeout, req, &state);
1521 /****************************************************************************
1522 Work out what access_mask to use from what the client sent us.
1523 ****************************************************************************/
1525 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1526 const struct smb_filename *smb_fname,
1527 bool file_existed,
1528 uint32_t access_mask,
1529 uint32_t *access_mask_out)
1531 NTSTATUS status;
1534 * Convert GENERIC bits to specific bits.
1537 se_map_generic(&access_mask, &file_generic_mapping);
1539 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1540 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1541 if (file_existed) {
1543 struct security_descriptor *sd;
1544 uint32_t access_granted = 0;
1546 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1547 (SECINFO_OWNER |
1548 SECINFO_GROUP |
1549 SECINFO_DACL),&sd);
1551 if (!NT_STATUS_IS_OK(status)) {
1552 DEBUG(10,("smbd_calculate_access_mask: "
1553 "Could not get acl on file %s: %s\n",
1554 smb_fname_str_dbg(smb_fname),
1555 nt_errstr(status)));
1556 return NT_STATUS_ACCESS_DENIED;
1559 status = smb1_file_se_access_check(conn,
1561 get_current_nttok(conn),
1562 access_mask,
1563 &access_granted);
1565 TALLOC_FREE(sd);
1567 if (!NT_STATUS_IS_OK(status)) {
1568 DEBUG(10, ("smbd_calculate_access_mask: "
1569 "Access denied on file %s: "
1570 "when calculating maximum access\n",
1571 smb_fname_str_dbg(smb_fname)));
1572 return NT_STATUS_ACCESS_DENIED;
1575 access_mask = access_granted;
1576 } else {
1577 access_mask = FILE_GENERIC_ALL;
1581 *access_mask_out = access_mask;
1582 return NT_STATUS_OK;
1585 /****************************************************************************
1586 Remove the deferred open entry under lock.
1587 ****************************************************************************/
1589 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1590 struct server_id pid)
1592 struct share_mode_lock *lck = get_share_mode_lock(talloc_tos(), id,
1593 NULL, NULL, NULL);
1594 if (lck == NULL) {
1595 DEBUG(0, ("could not get share mode lock\n"));
1596 } else {
1597 del_deferred_open_entry(lck, mid, pid);
1598 TALLOC_FREE(lck);
1602 /****************************************************************
1603 Ensure we get the brlock lock followed by the share mode lock
1604 in the correct order to prevent deadlocks if other smbd's are
1605 using the brlock database on this file simultaneously with this open
1606 (that code also gets the locks in brlock -> share mode lock order).
1607 ****************************************************************/
1609 static bool acquire_ordered_locks(TALLOC_CTX *mem_ctx,
1610 files_struct *fsp,
1611 const struct file_id id,
1612 const char *connectpath,
1613 const struct smb_filename *smb_fname,
1614 const struct timespec *p_old_write_time,
1615 struct share_mode_lock **p_lck,
1616 struct byte_range_lock **p_br_lck)
1618 /* Ordering - we must get the br_lck for this
1619 file before the share mode. */
1620 if (lp_locking(fsp->conn->params)) {
1621 *p_br_lck = brl_get_locks_readonly(fsp);
1622 if (*p_br_lck == NULL) {
1623 DEBUG(0, ("Could not get br_lock\n"));
1624 return false;
1626 /* Note - we don't need to free the returned
1627 br_lck explicitly as it was allocated on talloc_tos()
1628 and so will be autofreed (and release the lock)
1629 once the frame context disappears.
1631 If it was set to fsp->brlock_rec then it was
1632 talloc_move'd to hang off the fsp pointer and
1633 in this case is guarenteed to not be holding the
1634 lock on the brlock database. */
1637 *p_lck = get_share_mode_lock(mem_ctx,
1639 connectpath,
1640 smb_fname,
1641 p_old_write_time);
1643 if (*p_lck == NULL) {
1644 DEBUG(0, ("Could not get share mode lock\n"));
1645 TALLOC_FREE(*p_br_lck);
1646 return false;
1648 return true;
1651 /****************************************************************************
1652 Open a file with a share mode. Passed in an already created files_struct *.
1653 ****************************************************************************/
1655 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1656 struct smb_request *req,
1657 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1658 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1659 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1660 uint32 create_options, /* options such as delete on close. */
1661 uint32 new_dos_attributes, /* attributes used for new file. */
1662 int oplock_request, /* internal Samba oplock codes. */
1663 /* Information (FILE_EXISTS etc.) */
1664 uint32_t private_flags, /* Samba specific flags. */
1665 int *pinfo,
1666 files_struct *fsp)
1668 struct smb_filename *smb_fname = fsp->fsp_name;
1669 int flags=0;
1670 int flags2=0;
1671 bool file_existed = VALID_STAT(smb_fname->st);
1672 bool def_acl = False;
1673 bool posix_open = False;
1674 bool new_file_created = False;
1675 bool clear_ads = false;
1676 struct file_id id;
1677 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1678 mode_t new_unx_mode = (mode_t)0;
1679 mode_t unx_mode = (mode_t)0;
1680 int info;
1681 uint32 existing_dos_attributes = 0;
1682 struct timeval request_time = timeval_zero();
1683 struct share_mode_lock *lck = NULL;
1684 uint32 open_access_mask = access_mask;
1685 NTSTATUS status;
1686 char *parent_dir;
1688 ZERO_STRUCT(id);
1690 /* Windows allows a new file to be created and
1691 silently removes a FILE_ATTRIBUTE_DIRECTORY
1692 sent by the client. Do the same. */
1694 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1696 if (conn->printer) {
1698 * Printers are handled completely differently.
1699 * Most of the passed parameters are ignored.
1702 if (pinfo) {
1703 *pinfo = FILE_WAS_CREATED;
1706 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1707 smb_fname_str_dbg(smb_fname)));
1709 if (!req) {
1710 DEBUG(0,("open_file_ntcreate: printer open without "
1711 "an SMB request!\n"));
1712 return NT_STATUS_INTERNAL_ERROR;
1715 return print_spool_open(fsp, smb_fname->base_name,
1716 req->vuid);
1719 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1720 NULL)) {
1721 return NT_STATUS_NO_MEMORY;
1724 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1725 posix_open = True;
1726 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1727 new_dos_attributes = 0;
1728 } else {
1729 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1730 * created new. */
1731 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1732 smb_fname, parent_dir);
1735 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1736 "access_mask=0x%x share_access=0x%x "
1737 "create_disposition = 0x%x create_options=0x%x "
1738 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1739 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1740 access_mask, share_access, create_disposition,
1741 create_options, (unsigned int)unx_mode, oplock_request,
1742 (unsigned int)private_flags));
1744 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1745 DEBUG(0, ("No smb request but not an internal only open!\n"));
1746 return NT_STATUS_INTERNAL_ERROR;
1750 * Only non-internal opens can be deferred at all
1753 if (req) {
1754 void *ptr;
1755 if (get_deferred_open_message_state(req,
1756 &request_time,
1757 &ptr)) {
1759 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1760 /* Remember the absolute time of the original
1761 request with this mid. We'll use it later to
1762 see if this has timed out. */
1764 /* Remove the deferred open entry under lock. */
1765 remove_deferred_open_entry(
1766 state->id, req->mid,
1767 sconn_server_id(req->sconn));
1769 /* Ensure we don't reprocess this message. */
1770 remove_deferred_open_message_smb(req->mid);
1774 status = check_name(conn, smb_fname->base_name);
1775 if (!NT_STATUS_IS_OK(status)) {
1776 return status;
1779 if (!posix_open) {
1780 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1781 if (file_existed) {
1782 existing_dos_attributes = dos_mode(conn, smb_fname);
1786 /* ignore any oplock requests if oplocks are disabled */
1787 if (!lp_oplocks(SNUM(conn)) ||
1788 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1789 /* Mask off everything except the private Samba bits. */
1790 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1793 /* this is for OS/2 long file names - say we don't support them */
1794 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1795 /* OS/2 Workplace shell fix may be main code stream in a later
1796 * release. */
1797 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1798 "supported.\n"));
1799 if (use_nt_status()) {
1800 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1802 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1805 switch( create_disposition ) {
1807 * Currently we're using FILE_SUPERSEDE as the same as
1808 * FILE_OVERWRITE_IF but they really are
1809 * different. FILE_SUPERSEDE deletes an existing file
1810 * (requiring delete access) then recreates it.
1812 case FILE_SUPERSEDE:
1813 /* If file exists replace/overwrite. If file doesn't
1814 * exist create. */
1815 flags2 |= (O_CREAT | O_TRUNC);
1816 clear_ads = true;
1817 break;
1819 case FILE_OVERWRITE_IF:
1820 /* If file exists replace/overwrite. If file doesn't
1821 * exist create. */
1822 flags2 |= (O_CREAT | O_TRUNC);
1823 clear_ads = true;
1824 break;
1826 case FILE_OPEN:
1827 /* If file exists open. If file doesn't exist error. */
1828 if (!file_existed) {
1829 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1830 "requested for file %s and file "
1831 "doesn't exist.\n",
1832 smb_fname_str_dbg(smb_fname)));
1833 errno = ENOENT;
1834 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1836 break;
1838 case FILE_OVERWRITE:
1839 /* If file exists overwrite. If file doesn't exist
1840 * error. */
1841 if (!file_existed) {
1842 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1843 "requested for file %s and file "
1844 "doesn't exist.\n",
1845 smb_fname_str_dbg(smb_fname) ));
1846 errno = ENOENT;
1847 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1849 flags2 |= O_TRUNC;
1850 clear_ads = true;
1851 break;
1853 case FILE_CREATE:
1854 /* If file exists error. If file doesn't exist
1855 * create. */
1856 if (file_existed) {
1857 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1858 "requested for file %s and file "
1859 "already exists.\n",
1860 smb_fname_str_dbg(smb_fname)));
1861 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1862 errno = EISDIR;
1863 } else {
1864 errno = EEXIST;
1866 return map_nt_error_from_unix(errno);
1868 flags2 |= (O_CREAT|O_EXCL);
1869 break;
1871 case FILE_OPEN_IF:
1872 /* If file exists open. If file doesn't exist
1873 * create. */
1874 flags2 |= O_CREAT;
1875 break;
1877 default:
1878 return NT_STATUS_INVALID_PARAMETER;
1881 /* We only care about matching attributes on file exists and
1882 * overwrite. */
1884 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1885 (create_disposition == FILE_OVERWRITE_IF))) {
1886 if (!open_match_attributes(conn, existing_dos_attributes,
1887 new_dos_attributes,
1888 smb_fname->st.st_ex_mode,
1889 unx_mode, &new_unx_mode)) {
1890 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1891 "for file %s (%x %x) (0%o, 0%o)\n",
1892 smb_fname_str_dbg(smb_fname),
1893 existing_dos_attributes,
1894 new_dos_attributes,
1895 (unsigned int)smb_fname->st.st_ex_mode,
1896 (unsigned int)unx_mode ));
1897 errno = EACCES;
1898 return NT_STATUS_ACCESS_DENIED;
1902 status = smbd_calculate_access_mask(conn, smb_fname, file_existed,
1903 access_mask,
1904 &access_mask);
1905 if (!NT_STATUS_IS_OK(status)) {
1906 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1907 "on file %s returned %s\n",
1908 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1909 return status;
1912 open_access_mask = access_mask;
1914 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1915 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1918 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1919 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1920 access_mask));
1923 * Note that we ignore the append flag as append does not
1924 * mean the same thing under DOS and Unix.
1927 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1928 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1929 /* DENY_DOS opens are always underlying read-write on the
1930 file handle, no matter what the requested access mask
1931 says. */
1932 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1933 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1934 flags = O_RDWR;
1935 } else {
1936 flags = O_WRONLY;
1938 } else {
1939 flags = O_RDONLY;
1943 * Currently we only look at FILE_WRITE_THROUGH for create options.
1946 #if defined(O_SYNC)
1947 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1948 flags2 |= O_SYNC;
1950 #endif /* O_SYNC */
1952 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1953 flags2 |= O_APPEND;
1956 if (!posix_open && !CAN_WRITE(conn)) {
1958 * We should really return a permission denied error if either
1959 * O_CREAT or O_TRUNC are set, but for compatibility with
1960 * older versions of Samba we just AND them out.
1962 flags2 &= ~(O_CREAT|O_TRUNC);
1966 * Ensure we can't write on a read-only share or file.
1969 if (flags != O_RDONLY && file_existed &&
1970 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1971 DEBUG(5,("open_file_ntcreate: write access requested for "
1972 "file %s on read only %s\n",
1973 smb_fname_str_dbg(smb_fname),
1974 !CAN_WRITE(conn) ? "share" : "file" ));
1975 errno = EACCES;
1976 return NT_STATUS_ACCESS_DENIED;
1979 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1980 fsp->share_access = share_access;
1981 fsp->fh->private_options = private_flags;
1982 fsp->access_mask = open_access_mask; /* We change this to the
1983 * requested access_mask after
1984 * the open is done. */
1985 fsp->posix_open = posix_open;
1987 /* Ensure no SAMBA_PRIVATE bits can be set. */
1988 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1990 if (timeval_is_zero(&request_time)) {
1991 request_time = fsp->open_time;
1994 if (file_existed) {
1995 struct byte_range_lock *br_lck = NULL;
1996 struct share_mode_entry *batch_entry = NULL;
1997 struct share_mode_entry *exclusive_entry = NULL;
1998 bool got_level2_oplock = false;
1999 bool got_a_none_oplock = false;
2001 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2002 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2004 if (!acquire_ordered_locks(talloc_tos(),
2005 fsp,
2007 conn->connectpath,
2008 smb_fname,
2009 &old_write_time,
2010 &lck,
2011 &br_lck)) {
2012 return NT_STATUS_SHARING_VIOLATION;
2015 /* Get the types we need to examine. */
2016 find_oplock_types(fsp,
2017 oplock_request,
2018 lck,
2019 &batch_entry,
2020 &exclusive_entry,
2021 &got_level2_oplock,
2022 &got_a_none_oplock);
2024 /* First pass - send break only on batch oplocks. */
2025 if ((req != NULL) &&
2026 delay_for_batch_oplocks(fsp,
2027 req->mid,
2028 oplock_request,
2029 batch_entry)) {
2030 schedule_defer_open(lck, request_time, req);
2031 TALLOC_FREE(lck);
2032 return NT_STATUS_SHARING_VIOLATION;
2035 /* Use the client requested access mask here, not the one we
2036 * open with. */
2037 status = open_mode_check(conn, lck, fsp->name_hash,
2038 access_mask, share_access,
2039 create_options, &file_existed);
2041 if (NT_STATUS_IS_OK(status)) {
2042 /* We might be going to allow this open. Check oplock
2043 * status again. */
2044 /* Second pass - send break for both batch or
2045 * exclusive oplocks. */
2046 if ((req != NULL) &&
2047 delay_for_exclusive_oplocks(
2048 fsp,
2049 req->mid,
2050 oplock_request,
2051 exclusive_entry)) {
2052 schedule_defer_open(lck, request_time, req);
2053 TALLOC_FREE(lck);
2054 return NT_STATUS_SHARING_VIOLATION;
2058 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2059 /* DELETE_PENDING is not deferred for a second */
2060 TALLOC_FREE(lck);
2061 return status;
2064 grant_fsp_oplock_type(fsp,
2065 br_lck,
2066 oplock_request,
2067 got_level2_oplock,
2068 got_a_none_oplock);
2070 if (!NT_STATUS_IS_OK(status)) {
2071 uint32 can_access_mask;
2072 bool can_access = True;
2074 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2076 /* Check if this can be done with the deny_dos and fcb
2077 * calls. */
2078 if (private_flags &
2079 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2080 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2081 if (req == NULL) {
2082 DEBUG(0, ("DOS open without an SMB "
2083 "request!\n"));
2084 TALLOC_FREE(lck);
2085 return NT_STATUS_INTERNAL_ERROR;
2088 /* Use the client requested access mask here,
2089 * not the one we open with. */
2090 status = fcb_or_dos_open(req,
2091 conn,
2092 fsp,
2093 smb_fname,
2095 req->smbpid,
2096 req->vuid,
2097 access_mask,
2098 share_access,
2099 create_options);
2101 if (NT_STATUS_IS_OK(status)) {
2102 TALLOC_FREE(lck);
2103 if (pinfo) {
2104 *pinfo = FILE_WAS_OPENED;
2106 return NT_STATUS_OK;
2111 * This next line is a subtlety we need for
2112 * MS-Access. If a file open will fail due to share
2113 * permissions and also for security (access) reasons,
2114 * we need to return the access failed error, not the
2115 * share error. We can't open the file due to kernel
2116 * oplock deadlock (it's possible we failed above on
2117 * the open_mode_check()) so use a userspace check.
2120 if (flags & O_RDWR) {
2121 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2122 } else if (flags & O_WRONLY) {
2123 can_access_mask = FILE_WRITE_DATA;
2124 } else {
2125 can_access_mask = FILE_READ_DATA;
2128 if (((can_access_mask & FILE_WRITE_DATA) &&
2129 !CAN_WRITE(conn)) ||
2130 !can_access_file_data(conn, smb_fname,
2131 can_access_mask)) {
2132 can_access = False;
2136 * If we're returning a share violation, ensure we
2137 * cope with the braindead 1 second delay.
2140 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2141 lp_defer_sharing_violations()) {
2142 struct timeval timeout;
2143 struct deferred_open_record state;
2144 int timeout_usecs;
2146 /* this is a hack to speed up torture tests
2147 in 'make test' */
2148 timeout_usecs = lp_parm_int(SNUM(conn),
2149 "smbd","sharedelay",
2150 SHARING_VIOLATION_USEC_WAIT);
2152 /* This is a relative time, added to the absolute
2153 request_time value to get the absolute timeout time.
2154 Note that if this is the second or greater time we enter
2155 this codepath for this particular request mid then
2156 request_time is left as the absolute time of the *first*
2157 time this request mid was processed. This is what allows
2158 the request to eventually time out. */
2160 timeout = timeval_set(0, timeout_usecs);
2162 /* Nothing actually uses state.delayed_for_oplocks
2163 but it's handy to differentiate in debug messages
2164 between a 30 second delay due to oplock break, and
2165 a 1 second delay for share mode conflicts. */
2167 state.delayed_for_oplocks = False;
2168 state.id = id;
2170 if ((req != NULL)
2171 && !request_timed_out(request_time,
2172 timeout)) {
2173 defer_open(lck, request_time, timeout,
2174 req, &state);
2178 TALLOC_FREE(lck);
2179 if (can_access) {
2181 * We have detected a sharing violation here
2182 * so return the correct error code
2184 status = NT_STATUS_SHARING_VIOLATION;
2185 } else {
2186 status = NT_STATUS_ACCESS_DENIED;
2188 return status;
2192 * We exit this block with the share entry *locked*.....
2196 SMB_ASSERT(!file_existed || (lck != NULL));
2199 * Ensure we pay attention to default ACLs on directories if required.
2202 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2203 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2204 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2207 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2208 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2209 (unsigned int)flags, (unsigned int)flags2,
2210 (unsigned int)unx_mode, (unsigned int)access_mask,
2211 (unsigned int)open_access_mask));
2214 * open_file strips any O_TRUNC flags itself.
2217 fsp_open = open_file(fsp, conn, req, parent_dir,
2218 flags|flags2, unx_mode, access_mask,
2219 open_access_mask);
2221 if (!NT_STATUS_IS_OK(fsp_open)) {
2222 if (lck != NULL) {
2223 TALLOC_FREE(lck);
2225 return fsp_open;
2228 if (!file_existed) {
2229 struct byte_range_lock *br_lck = NULL;
2230 struct share_mode_entry *batch_entry = NULL;
2231 struct share_mode_entry *exclusive_entry = NULL;
2232 bool got_level2_oplock = false;
2233 bool got_a_none_oplock = false;
2234 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2236 * Deal with the race condition where two smbd's detect the
2237 * file doesn't exist and do the create at the same time. One
2238 * of them will win and set a share mode, the other (ie. this
2239 * one) should check if the requested share mode for this
2240 * create is allowed.
2244 * Now the file exists and fsp is successfully opened,
2245 * fsp->dev and fsp->inode are valid and should replace the
2246 * dev=0,inode=0 from a non existent file. Spotted by
2247 * Nadav Danieli <nadavd@exanet.com>. JRA.
2250 id = fsp->file_id;
2252 if (!acquire_ordered_locks(talloc_tos(),
2253 fsp,
2255 conn->connectpath,
2256 smb_fname,
2257 &old_write_time,
2258 &lck,
2259 &br_lck)) {
2260 return NT_STATUS_SHARING_VIOLATION;
2263 /* Get the types we need to examine. */
2264 find_oplock_types(fsp,
2265 oplock_request,
2266 lck,
2267 &batch_entry,
2268 &exclusive_entry,
2269 &got_level2_oplock,
2270 &got_a_none_oplock);
2272 /* First pass - send break only on batch oplocks. */
2273 if ((req != NULL) &&
2274 delay_for_batch_oplocks(fsp,
2275 req->mid,
2276 oplock_request,
2277 batch_entry)) {
2278 schedule_defer_open(lck, request_time, req);
2279 TALLOC_FREE(lck);
2280 fd_close(fsp);
2281 return NT_STATUS_SHARING_VIOLATION;
2284 status = open_mode_check(conn, lck, fsp->name_hash,
2285 access_mask, share_access,
2286 create_options, &file_existed);
2288 if (NT_STATUS_IS_OK(status)) {
2289 /* We might be going to allow this open. Check oplock
2290 * status again. */
2291 /* Second pass - send break for both batch or
2292 * exclusive oplocks. */
2293 if ((req != NULL) &&
2294 delay_for_exclusive_oplocks(
2295 fsp,
2296 req->mid,
2297 oplock_request,
2298 exclusive_entry)) {
2299 schedule_defer_open(lck, request_time, req);
2300 TALLOC_FREE(lck);
2301 fd_close(fsp);
2302 return NT_STATUS_SHARING_VIOLATION;
2306 if (!NT_STATUS_IS_OK(status)) {
2307 struct deferred_open_record state;
2309 fd_close(fsp);
2311 state.delayed_for_oplocks = False;
2312 state.id = id;
2314 /* Do it all over again immediately. In the second
2315 * round we will find that the file existed and handle
2316 * the DELETE_PENDING and FCB cases correctly. No need
2317 * to duplicate the code here. Essentially this is a
2318 * "goto top of this function", but don't tell
2319 * anybody... */
2321 if (req != NULL) {
2322 defer_open(lck, request_time, timeval_zero(),
2323 req, &state);
2325 TALLOC_FREE(lck);
2326 return status;
2329 grant_fsp_oplock_type(fsp,
2330 br_lck,
2331 oplock_request,
2332 got_level2_oplock,
2333 got_a_none_oplock);
2336 * We exit this block with the share entry *locked*.....
2341 SMB_ASSERT(lck != NULL);
2343 /* Delete streams if create_disposition requires it */
2344 if (file_existed && clear_ads &&
2345 !is_ntfs_stream_smb_fname(smb_fname)) {
2346 status = delete_all_streams(conn, smb_fname->base_name);
2347 if (!NT_STATUS_IS_OK(status)) {
2348 TALLOC_FREE(lck);
2349 fd_close(fsp);
2350 return status;
2354 /* note that we ignore failure for the following. It is
2355 basically a hack for NFS, and NFS will never set one of
2356 these only read them. Nobody but Samba can ever set a deny
2357 mode and we have already checked our more authoritative
2358 locking database for permission to set this deny mode. If
2359 the kernel refuses the operations then the kernel is wrong.
2360 note that GPFS supports it as well - jmcd */
2362 if (fsp->fh->fd != -1) {
2363 int ret_flock;
2364 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2365 if(ret_flock == -1 ){
2367 TALLOC_FREE(lck);
2368 fd_close(fsp);
2370 return NT_STATUS_SHARING_VIOLATION;
2375 * At this point onwards, we can guarentee that the share entry
2376 * is locked, whether we created the file or not, and that the
2377 * deny mode is compatible with all current opens.
2381 * If requested, truncate the file.
2384 if (file_existed && (flags2&O_TRUNC)) {
2386 * We are modifing the file after open - update the stat
2387 * struct..
2389 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2390 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2391 status = map_nt_error_from_unix(errno);
2392 TALLOC_FREE(lck);
2393 fd_close(fsp);
2394 return status;
2399 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2400 * but we don't have to store this - just ignore it on access check.
2402 if (conn->sconn->using_smb2) {
2404 * SMB2 doesn't return it (according to Microsoft tests).
2405 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2406 * File created with access = 0x7 (Read, Write, Delete)
2407 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2409 fsp->access_mask = access_mask;
2410 } else {
2411 /* But SMB1 does. */
2412 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2415 if (file_existed) {
2416 /* stat opens on existing files don't get oplocks. */
2417 if (is_stat_open(open_access_mask)) {
2418 fsp->oplock_type = NO_OPLOCK;
2421 if (!(flags2 & O_TRUNC)) {
2422 info = FILE_WAS_OPENED;
2423 } else {
2424 info = FILE_WAS_OVERWRITTEN;
2426 } else {
2427 info = FILE_WAS_CREATED;
2430 if (pinfo) {
2431 *pinfo = info;
2435 * Setup the oplock info in both the shared memory and
2436 * file structs.
2439 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2441 * Could not get the kernel oplock or there are byte-range
2442 * locks on the file.
2444 fsp->oplock_type = NO_OPLOCK;
2447 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
2448 new_file_created = True;
2451 set_share_mode(lck, fsp, get_current_uid(conn),
2452 req ? req->mid : 0,
2453 fsp->oplock_type);
2455 /* Handle strange delete on close create semantics. */
2456 if (create_options & FILE_DELETE_ON_CLOSE) {
2458 status = can_set_delete_on_close(fsp, new_dos_attributes);
2460 if (!NT_STATUS_IS_OK(status)) {
2461 /* Remember to delete the mode we just added. */
2462 del_share_mode(lck, fsp);
2463 TALLOC_FREE(lck);
2464 fd_close(fsp);
2465 return status;
2467 /* Note that here we set the *inital* delete on close flag,
2468 not the regular one. The magic gets handled in close. */
2469 fsp->initial_delete_on_close = True;
2472 if (new_file_created) {
2473 /* Files should be initially set as archive */
2474 if (lp_map_archive(SNUM(conn)) ||
2475 lp_store_dos_attributes(SNUM(conn))) {
2476 if (!posix_open) {
2477 if (file_set_dosmode(conn, smb_fname,
2478 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2479 parent_dir, true) == 0) {
2480 unx_mode = smb_fname->st.st_ex_mode;
2486 /* Determine sparse flag. */
2487 if (posix_open) {
2488 /* POSIX opens are sparse by default. */
2489 fsp->is_sparse = true;
2490 } else {
2491 fsp->is_sparse = (file_existed &&
2492 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2496 * Take care of inherited ACLs on created files - if default ACL not
2497 * selected.
2500 if (!posix_open && !file_existed && !def_acl) {
2502 int saved_errno = errno; /* We might get ENOSYS in the next
2503 * call.. */
2505 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2506 errno == ENOSYS) {
2507 errno = saved_errno; /* Ignore ENOSYS */
2510 } else if (new_unx_mode) {
2512 int ret = -1;
2514 /* Attributes need changing. File already existed. */
2517 int saved_errno = errno; /* We might get ENOSYS in the
2518 * next call.. */
2519 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2521 if (ret == -1 && errno == ENOSYS) {
2522 errno = saved_errno; /* Ignore ENOSYS */
2523 } else {
2524 DEBUG(5, ("open_file_ntcreate: reset "
2525 "attributes of file %s to 0%o\n",
2526 smb_fname_str_dbg(smb_fname),
2527 (unsigned int)new_unx_mode));
2528 ret = 0; /* Don't do the fchmod below. */
2532 if ((ret == -1) &&
2533 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2534 DEBUG(5, ("open_file_ntcreate: failed to reset "
2535 "attributes of file %s to 0%o\n",
2536 smb_fname_str_dbg(smb_fname),
2537 (unsigned int)new_unx_mode));
2540 /* If this is a successful open, we must remove any deferred open
2541 * records. */
2542 if (req != NULL) {
2543 del_deferred_open_entry(lck, req->mid,
2544 sconn_server_id(req->sconn));
2546 TALLOC_FREE(lck);
2548 return NT_STATUS_OK;
2552 /****************************************************************************
2553 Open a file for for write to ensure that we can fchmod it.
2554 ****************************************************************************/
2556 NTSTATUS open_file_fchmod(connection_struct *conn,
2557 struct smb_filename *smb_fname,
2558 files_struct **result)
2560 if (!VALID_STAT(smb_fname->st)) {
2561 return NT_STATUS_INVALID_PARAMETER;
2564 return SMB_VFS_CREATE_FILE(
2565 conn, /* conn */
2566 NULL, /* req */
2567 0, /* root_dir_fid */
2568 smb_fname, /* fname */
2569 FILE_WRITE_DATA, /* access_mask */
2570 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2571 FILE_SHARE_DELETE),
2572 FILE_OPEN, /* create_disposition*/
2573 0, /* create_options */
2574 0, /* file_attributes */
2575 INTERNAL_OPEN_ONLY, /* oplock_request */
2576 0, /* allocation_size */
2577 0, /* private_flags */
2578 NULL, /* sd */
2579 NULL, /* ea_list */
2580 result, /* result */
2581 NULL); /* pinfo */
2584 static NTSTATUS mkdir_internal(connection_struct *conn,
2585 struct smb_filename *smb_dname,
2586 uint32 file_attributes)
2588 mode_t mode;
2589 char *parent_dir;
2590 NTSTATUS status;
2591 bool posix_open = false;
2592 bool need_re_stat = false;
2594 if(!CAN_WRITE(conn)) {
2595 DEBUG(5,("mkdir_internal: failing create on read-only share "
2596 "%s\n", lp_servicename(SNUM(conn))));
2597 return NT_STATUS_ACCESS_DENIED;
2600 status = check_name(conn, smb_dname->base_name);
2601 if (!NT_STATUS_IS_OK(status)) {
2602 return status;
2605 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2606 NULL)) {
2607 return NT_STATUS_NO_MEMORY;
2610 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2611 posix_open = true;
2612 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2613 } else {
2614 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2617 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2618 return map_nt_error_from_unix(errno);
2621 /* Ensure we're checking for a symlink here.... */
2622 /* We don't want to get caught by a symlink racer. */
2624 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2625 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2626 smb_fname_str_dbg(smb_dname), strerror(errno)));
2627 return map_nt_error_from_unix(errno);
2630 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2631 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2632 smb_fname_str_dbg(smb_dname)));
2633 return NT_STATUS_ACCESS_DENIED;
2636 if (lp_store_dos_attributes(SNUM(conn))) {
2637 if (!posix_open) {
2638 file_set_dosmode(conn, smb_dname,
2639 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2640 parent_dir, true);
2644 if (lp_inherit_perms(SNUM(conn))) {
2645 inherit_access_posix_acl(conn, parent_dir,
2646 smb_dname->base_name, mode);
2647 need_re_stat = true;
2650 if (!posix_open) {
2652 * Check if high bits should have been set,
2653 * then (if bits are missing): add them.
2654 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2655 * dir.
2657 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2658 (mode & ~smb_dname->st.st_ex_mode)) {
2659 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2660 (smb_dname->st.st_ex_mode |
2661 (mode & ~smb_dname->st.st_ex_mode)));
2662 need_re_stat = true;
2666 /* Change the owner if required. */
2667 if (lp_inherit_owner(SNUM(conn))) {
2668 change_dir_owner_to_parent(conn, parent_dir,
2669 smb_dname->base_name,
2670 &smb_dname->st);
2671 need_re_stat = true;
2674 if (need_re_stat) {
2675 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2676 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2677 smb_fname_str_dbg(smb_dname), strerror(errno)));
2678 return map_nt_error_from_unix(errno);
2682 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2683 smb_dname->base_name);
2685 return NT_STATUS_OK;
2688 /****************************************************************************
2689 Ensure we didn't get symlink raced on opening a directory.
2690 ****************************************************************************/
2692 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2693 const SMB_STRUCT_STAT *sbuf2)
2695 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2696 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2697 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2698 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2699 return false;
2701 return true;
2704 /****************************************************************************
2705 Open a directory from an NT SMB call.
2706 ****************************************************************************/
2708 static NTSTATUS open_directory(connection_struct *conn,
2709 struct smb_request *req,
2710 struct smb_filename *smb_dname,
2711 uint32 access_mask,
2712 uint32 share_access,
2713 uint32 create_disposition,
2714 uint32 create_options,
2715 uint32 file_attributes,
2716 int *pinfo,
2717 files_struct **result)
2719 files_struct *fsp = NULL;
2720 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2721 struct share_mode_lock *lck = NULL;
2722 NTSTATUS status;
2723 struct timespec mtimespec;
2724 int info = 0;
2726 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
2728 /* Ensure we have a directory attribute. */
2729 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2731 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2732 "share_access = 0x%x create_options = 0x%x, "
2733 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2734 smb_fname_str_dbg(smb_dname),
2735 (unsigned int)access_mask,
2736 (unsigned int)share_access,
2737 (unsigned int)create_options,
2738 (unsigned int)create_disposition,
2739 (unsigned int)file_attributes));
2741 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2742 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2743 is_ntfs_stream_smb_fname(smb_dname)) {
2744 DEBUG(2, ("open_directory: %s is a stream name!\n",
2745 smb_fname_str_dbg(smb_dname)));
2746 return NT_STATUS_NOT_A_DIRECTORY;
2749 status = smbd_calculate_access_mask(conn, smb_dname, dir_existed,
2750 access_mask, &access_mask);
2751 if (!NT_STATUS_IS_OK(status)) {
2752 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2753 "on file %s returned %s\n",
2754 smb_fname_str_dbg(smb_dname),
2755 nt_errstr(status)));
2756 return status;
2759 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2760 !security_token_has_privilege(get_current_nttok(conn),
2761 SEC_PRIV_SECURITY)) {
2762 DEBUG(10, ("open_directory: open on %s "
2763 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2764 smb_fname_str_dbg(smb_dname)));
2765 return NT_STATUS_PRIVILEGE_NOT_HELD;
2768 switch( create_disposition ) {
2769 case FILE_OPEN:
2771 if (!dir_existed) {
2772 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2775 info = FILE_WAS_OPENED;
2776 break;
2778 case FILE_CREATE:
2780 /* If directory exists error. If directory doesn't
2781 * exist create. */
2783 status = mkdir_internal(conn, smb_dname,
2784 file_attributes);
2786 if (!NT_STATUS_IS_OK(status)) {
2787 DEBUG(2, ("open_directory: unable to create "
2788 "%s. Error was %s\n",
2789 smb_fname_str_dbg(smb_dname),
2790 nt_errstr(status)));
2791 return status;
2794 info = FILE_WAS_CREATED;
2795 break;
2797 case FILE_OPEN_IF:
2799 * If directory exists open. If directory doesn't
2800 * exist create.
2803 status = mkdir_internal(conn, smb_dname,
2804 file_attributes);
2806 if (NT_STATUS_IS_OK(status)) {
2807 info = FILE_WAS_CREATED;
2810 if (NT_STATUS_EQUAL(status,
2811 NT_STATUS_OBJECT_NAME_COLLISION)) {
2812 info = FILE_WAS_OPENED;
2813 status = NT_STATUS_OK;
2815 break;
2817 case FILE_SUPERSEDE:
2818 case FILE_OVERWRITE:
2819 case FILE_OVERWRITE_IF:
2820 default:
2821 DEBUG(5,("open_directory: invalid create_disposition "
2822 "0x%x for directory %s\n",
2823 (unsigned int)create_disposition,
2824 smb_fname_str_dbg(smb_dname)));
2825 return NT_STATUS_INVALID_PARAMETER;
2828 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2829 DEBUG(5,("open_directory: %s is not a directory !\n",
2830 smb_fname_str_dbg(smb_dname)));
2831 return NT_STATUS_NOT_A_DIRECTORY;
2834 if (info == FILE_WAS_OPENED) {
2835 uint32_t access_granted = 0;
2836 status = smbd_check_open_rights(conn, smb_dname, access_mask,
2837 &access_granted);
2839 /* Were we trying to do a directory open
2840 * for delete and didn't get DELETE
2841 * access (only) ? Check if the
2842 * directory allows DELETE_CHILD.
2843 * See here:
2844 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
2845 * for details. */
2847 if ((NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
2848 (access_mask & DELETE_ACCESS) &&
2849 (access_granted == DELETE_ACCESS) &&
2850 can_delete_file_in_directory(conn, smb_dname))) {
2851 DEBUG(10,("open_directory: overrode ACCESS_DENIED "
2852 "on directory %s\n",
2853 smb_fname_str_dbg(smb_dname)));
2854 status = NT_STATUS_OK;
2857 if (!NT_STATUS_IS_OK(status)) {
2858 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2859 "file %s failed with %s\n",
2860 smb_fname_str_dbg(smb_dname),
2861 nt_errstr(status)));
2862 return status;
2866 status = file_new(req, conn, &fsp);
2867 if(!NT_STATUS_IS_OK(status)) {
2868 return status;
2872 * Setup the files_struct for it.
2875 fsp->mode = smb_dname->st.st_ex_mode;
2876 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2877 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2878 fsp->file_pid = req ? req->smbpid : 0;
2879 fsp->can_lock = False;
2880 fsp->can_read = False;
2881 fsp->can_write = False;
2883 fsp->share_access = share_access;
2884 fsp->fh->private_options = 0;
2886 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2888 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2889 fsp->print_file = NULL;
2890 fsp->modified = False;
2891 fsp->oplock_type = NO_OPLOCK;
2892 fsp->sent_oplock_break = NO_BREAK_SENT;
2893 fsp->is_directory = True;
2894 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2895 status = fsp_set_smb_fname(fsp, smb_dname);
2896 if (!NT_STATUS_IS_OK(status)) {
2897 file_free(req, fsp);
2898 return status;
2901 mtimespec = smb_dname->st.st_ex_mtime;
2903 #ifdef O_DIRECTORY
2904 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2905 #else
2906 /* POSIX allows us to open a directory with O_RDONLY. */
2907 status = fd_open(conn, fsp, O_RDONLY, 0);
2908 #endif
2909 if (!NT_STATUS_IS_OK(status)) {
2910 DEBUG(5, ("open_directory: Could not open fd for "
2911 "%s (%s)\n",
2912 smb_fname_str_dbg(smb_dname),
2913 nt_errstr(status)));
2914 file_free(req, fsp);
2915 return status;
2918 status = vfs_stat_fsp(fsp);
2919 if (!NT_STATUS_IS_OK(status)) {
2920 fd_close(fsp);
2921 file_free(req, fsp);
2922 return status;
2925 /* Ensure there was no race condition. */
2926 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2927 DEBUG(5,("open_directory: stat struct differs for "
2928 "directory %s.\n",
2929 smb_fname_str_dbg(smb_dname)));
2930 fd_close(fsp);
2931 file_free(req, fsp);
2932 return NT_STATUS_ACCESS_DENIED;
2935 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2936 conn->connectpath, smb_dname, &mtimespec);
2938 if (lck == NULL) {
2939 DEBUG(0, ("open_directory: Could not get share mode lock for "
2940 "%s\n", smb_fname_str_dbg(smb_dname)));
2941 fd_close(fsp);
2942 file_free(req, fsp);
2943 return NT_STATUS_SHARING_VIOLATION;
2946 status = open_mode_check(conn, lck, fsp->name_hash,
2947 access_mask, share_access,
2948 create_options, &dir_existed);
2950 if (!NT_STATUS_IS_OK(status)) {
2951 TALLOC_FREE(lck);
2952 fd_close(fsp);
2953 file_free(req, fsp);
2954 return status;
2957 set_share_mode(lck, fsp, get_current_uid(conn),
2958 req ? req->mid : 0, NO_OPLOCK);
2960 /* For directories the delete on close bit at open time seems
2961 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2962 if (create_options & FILE_DELETE_ON_CLOSE) {
2963 status = can_set_delete_on_close(fsp, 0);
2964 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2965 TALLOC_FREE(lck);
2966 fd_close(fsp);
2967 file_free(req, fsp);
2968 return status;
2971 if (NT_STATUS_IS_OK(status)) {
2972 /* Note that here we set the *inital* delete on close flag,
2973 not the regular one. The magic gets handled in close. */
2974 fsp->initial_delete_on_close = True;
2978 TALLOC_FREE(lck);
2980 if (pinfo) {
2981 *pinfo = info;
2984 *result = fsp;
2985 return NT_STATUS_OK;
2988 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2989 struct smb_filename *smb_dname)
2991 NTSTATUS status;
2992 files_struct *fsp;
2994 status = SMB_VFS_CREATE_FILE(
2995 conn, /* conn */
2996 req, /* req */
2997 0, /* root_dir_fid */
2998 smb_dname, /* fname */
2999 FILE_READ_ATTRIBUTES, /* access_mask */
3000 FILE_SHARE_NONE, /* share_access */
3001 FILE_CREATE, /* create_disposition*/
3002 FILE_DIRECTORY_FILE, /* create_options */
3003 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3004 0, /* oplock_request */
3005 0, /* allocation_size */
3006 0, /* private_flags */
3007 NULL, /* sd */
3008 NULL, /* ea_list */
3009 &fsp, /* result */
3010 NULL); /* pinfo */
3012 if (NT_STATUS_IS_OK(status)) {
3013 close_file(req, fsp, NORMAL_CLOSE);
3016 return status;
3019 /****************************************************************************
3020 Receive notification that one of our open files has been renamed by another
3021 smbd process.
3022 ****************************************************************************/
3024 void msg_file_was_renamed(struct messaging_context *msg,
3025 void *private_data,
3026 uint32_t msg_type,
3027 struct server_id server_id,
3028 DATA_BLOB *data)
3030 struct smbd_server_connection *sconn;
3031 files_struct *fsp;
3032 char *frm = (char *)data->data;
3033 struct file_id id;
3034 const char *sharepath;
3035 const char *base_name;
3036 const char *stream_name;
3037 struct smb_filename *smb_fname = NULL;
3038 size_t sp_len, bn_len;
3039 NTSTATUS status;
3041 sconn = msg_ctx_to_sconn(msg);
3042 if (sconn == NULL) {
3043 DEBUG(1, ("could not find sconn\n"));
3044 return;
3047 if (data->data == NULL
3048 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3049 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3050 (int)data->length));
3051 return;
3054 /* Unpack the message. */
3055 pull_file_id_24(frm, &id);
3056 sharepath = &frm[24];
3057 sp_len = strlen(sharepath);
3058 base_name = sharepath + sp_len + 1;
3059 bn_len = strlen(base_name);
3060 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3062 /* stream_name must always be NULL if there is no stream. */
3063 if (stream_name[0] == '\0') {
3064 stream_name = NULL;
3067 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3068 stream_name, NULL, &smb_fname);
3069 if (!NT_STATUS_IS_OK(status)) {
3070 return;
3073 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3074 "file_id %s\n",
3075 sharepath, smb_fname_str_dbg(smb_fname),
3076 file_id_string_tos(&id)));
3078 for(fsp = file_find_di_first(sconn, id); fsp;
3079 fsp = file_find_di_next(fsp)) {
3080 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3082 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3083 fsp->fnum, fsp_str_dbg(fsp),
3084 smb_fname_str_dbg(smb_fname)));
3085 status = fsp_set_smb_fname(fsp, smb_fname);
3086 if (!NT_STATUS_IS_OK(status)) {
3087 goto out;
3089 } else {
3090 /* TODO. JRA. */
3091 /* Now we have the complete path we can work out if this is
3092 actually within this share and adjust newname accordingly. */
3093 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3094 "not sharepath %s) "
3095 "fnum %d from %s -> %s\n",
3096 fsp->conn->connectpath,
3097 sharepath,
3098 fsp->fnum,
3099 fsp_str_dbg(fsp),
3100 smb_fname_str_dbg(smb_fname)));
3103 out:
3104 TALLOC_FREE(smb_fname);
3105 return;
3109 * If a main file is opened for delete, all streams need to be checked for
3110 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3111 * If that works, delete them all by setting the delete on close and close.
3114 NTSTATUS open_streams_for_delete(connection_struct *conn,
3115 const char *fname)
3117 struct stream_struct *stream_info;
3118 files_struct **streams;
3119 int i;
3120 unsigned int num_streams;
3121 TALLOC_CTX *frame = talloc_stackframe();
3122 NTSTATUS status;
3124 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
3125 &num_streams, &stream_info);
3127 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3128 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3129 DEBUG(10, ("no streams around\n"));
3130 TALLOC_FREE(frame);
3131 return NT_STATUS_OK;
3134 if (!NT_STATUS_IS_OK(status)) {
3135 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
3136 nt_errstr(status)));
3137 goto fail;
3140 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3141 num_streams));
3143 if (num_streams == 0) {
3144 TALLOC_FREE(frame);
3145 return NT_STATUS_OK;
3148 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
3149 if (streams == NULL) {
3150 DEBUG(0, ("talloc failed\n"));
3151 status = NT_STATUS_NO_MEMORY;
3152 goto fail;
3155 for (i=0; i<num_streams; i++) {
3156 struct smb_filename *smb_fname = NULL;
3158 if (strequal(stream_info[i].name, "::$DATA")) {
3159 streams[i] = NULL;
3160 continue;
3163 status = create_synthetic_smb_fname(talloc_tos(), fname,
3164 stream_info[i].name,
3165 NULL, &smb_fname);
3166 if (!NT_STATUS_IS_OK(status)) {
3167 goto fail;
3170 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3171 DEBUG(10, ("Unable to stat stream: %s\n",
3172 smb_fname_str_dbg(smb_fname)));
3175 status = SMB_VFS_CREATE_FILE(
3176 conn, /* conn */
3177 NULL, /* req */
3178 0, /* root_dir_fid */
3179 smb_fname, /* fname */
3180 DELETE_ACCESS, /* access_mask */
3181 (FILE_SHARE_READ | /* share_access */
3182 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3183 FILE_OPEN, /* create_disposition*/
3184 0, /* create_options */
3185 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3186 0, /* oplock_request */
3187 0, /* allocation_size */
3188 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3189 NULL, /* sd */
3190 NULL, /* ea_list */
3191 &streams[i], /* result */
3192 NULL); /* pinfo */
3194 if (!NT_STATUS_IS_OK(status)) {
3195 DEBUG(10, ("Could not open stream %s: %s\n",
3196 smb_fname_str_dbg(smb_fname),
3197 nt_errstr(status)));
3199 TALLOC_FREE(smb_fname);
3200 break;
3202 TALLOC_FREE(smb_fname);
3206 * don't touch the variable "status" beyond this point :-)
3209 for (i -= 1 ; i >= 0; i--) {
3210 if (streams[i] == NULL) {
3211 continue;
3214 DEBUG(10, ("Closing stream # %d, %s\n", i,
3215 fsp_str_dbg(streams[i])));
3216 close_file(NULL, streams[i], NORMAL_CLOSE);
3219 fail:
3220 TALLOC_FREE(frame);
3221 return status;
3225 * Wrapper around open_file_ntcreate and open_directory
3228 static NTSTATUS create_file_unixpath(connection_struct *conn,
3229 struct smb_request *req,
3230 struct smb_filename *smb_fname,
3231 uint32_t access_mask,
3232 uint32_t share_access,
3233 uint32_t create_disposition,
3234 uint32_t create_options,
3235 uint32_t file_attributes,
3236 uint32_t oplock_request,
3237 uint64_t allocation_size,
3238 uint32_t private_flags,
3239 struct security_descriptor *sd,
3240 struct ea_list *ea_list,
3242 files_struct **result,
3243 int *pinfo)
3245 int info = FILE_WAS_OPENED;
3246 files_struct *base_fsp = NULL;
3247 files_struct *fsp = NULL;
3248 NTSTATUS status;
3250 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3251 "file_attributes = 0x%x, share_access = 0x%x, "
3252 "create_disposition = 0x%x create_options = 0x%x "
3253 "oplock_request = 0x%x private_flags = 0x%x "
3254 "ea_list = 0x%p, sd = 0x%p, "
3255 "fname = %s\n",
3256 (unsigned int)access_mask,
3257 (unsigned int)file_attributes,
3258 (unsigned int)share_access,
3259 (unsigned int)create_disposition,
3260 (unsigned int)create_options,
3261 (unsigned int)oplock_request,
3262 (unsigned int)private_flags,
3263 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3265 if (create_options & FILE_OPEN_BY_FILE_ID) {
3266 status = NT_STATUS_NOT_SUPPORTED;
3267 goto fail;
3270 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3271 status = NT_STATUS_INVALID_PARAMETER;
3272 goto fail;
3275 if (req == NULL) {
3276 oplock_request |= INTERNAL_OPEN_ONLY;
3279 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3280 && (access_mask & DELETE_ACCESS)
3281 && !is_ntfs_stream_smb_fname(smb_fname)) {
3283 * We can't open a file with DELETE access if any of the
3284 * streams is open without FILE_SHARE_DELETE
3286 status = open_streams_for_delete(conn, smb_fname->base_name);
3288 if (!NT_STATUS_IS_OK(status)) {
3289 goto fail;
3293 /* This is the correct thing to do (check every time) but can_delete
3294 * is expensive (it may have to read the parent directory
3295 * permissions). So for now we're not doing it unless we have a strong
3296 * hint the client is really going to delete this file. If the client
3297 * is forcing FILE_CREATE let the filesystem take care of the
3298 * permissions. */
3300 /* Setting FILE_SHARE_DELETE is the hint. */
3302 if ((create_disposition != FILE_CREATE)
3303 && (access_mask & DELETE_ACCESS)
3304 && (!(can_delete_file_in_directory(conn, smb_fname) ||
3305 can_access_file_acl(conn, smb_fname, DELETE_ACCESS)))) {
3306 status = NT_STATUS_ACCESS_DENIED;
3307 DEBUG(10,("create_file_unixpath: open file %s "
3308 "for delete ACCESS_DENIED\n",
3309 smb_fname_str_dbg(smb_fname)));
3310 goto fail;
3313 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3314 !security_token_has_privilege(get_current_nttok(conn),
3315 SEC_PRIV_SECURITY)) {
3316 DEBUG(10, ("create_file_unixpath: open on %s "
3317 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3318 smb_fname_str_dbg(smb_fname)));
3319 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3320 goto fail;
3323 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3324 && is_ntfs_stream_smb_fname(smb_fname)
3325 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3326 uint32 base_create_disposition;
3327 struct smb_filename *smb_fname_base = NULL;
3329 if (create_options & FILE_DIRECTORY_FILE) {
3330 status = NT_STATUS_NOT_A_DIRECTORY;
3331 goto fail;
3334 switch (create_disposition) {
3335 case FILE_OPEN:
3336 base_create_disposition = FILE_OPEN;
3337 break;
3338 default:
3339 base_create_disposition = FILE_OPEN_IF;
3340 break;
3343 /* Create an smb_filename with stream_name == NULL. */
3344 status = create_synthetic_smb_fname(talloc_tos(),
3345 smb_fname->base_name,
3346 NULL, NULL,
3347 &smb_fname_base);
3348 if (!NT_STATUS_IS_OK(status)) {
3349 goto fail;
3352 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3353 DEBUG(10, ("Unable to stat stream: %s\n",
3354 smb_fname_str_dbg(smb_fname_base)));
3357 /* Open the base file. */
3358 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3359 FILE_SHARE_READ
3360 | FILE_SHARE_WRITE
3361 | FILE_SHARE_DELETE,
3362 base_create_disposition,
3363 0, 0, 0, 0, 0, NULL, NULL,
3364 &base_fsp, NULL);
3365 TALLOC_FREE(smb_fname_base);
3367 if (!NT_STATUS_IS_OK(status)) {
3368 DEBUG(10, ("create_file_unixpath for base %s failed: "
3369 "%s\n", smb_fname->base_name,
3370 nt_errstr(status)));
3371 goto fail;
3373 /* we don't need to low level fd */
3374 fd_close(base_fsp);
3378 * If it's a request for a directory open, deal with it separately.
3381 if (create_options & FILE_DIRECTORY_FILE) {
3383 if (create_options & FILE_NON_DIRECTORY_FILE) {
3384 status = NT_STATUS_INVALID_PARAMETER;
3385 goto fail;
3388 /* Can't open a temp directory. IFS kit test. */
3389 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3390 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3391 status = NT_STATUS_INVALID_PARAMETER;
3392 goto fail;
3396 * We will get a create directory here if the Win32
3397 * app specified a security descriptor in the
3398 * CreateDirectory() call.
3401 oplock_request = 0;
3402 status = open_directory(
3403 conn, req, smb_fname, access_mask, share_access,
3404 create_disposition, create_options, file_attributes,
3405 &info, &fsp);
3406 } else {
3409 * Ordinary file case.
3412 status = file_new(req, conn, &fsp);
3413 if(!NT_STATUS_IS_OK(status)) {
3414 goto fail;
3417 status = fsp_set_smb_fname(fsp, smb_fname);
3418 if (!NT_STATUS_IS_OK(status)) {
3419 goto fail;
3423 * We're opening the stream element of a base_fsp
3424 * we already opened. Set up the base_fsp pointer.
3426 if (base_fsp) {
3427 fsp->base_fsp = base_fsp;
3430 status = open_file_ntcreate(conn,
3431 req,
3432 access_mask,
3433 share_access,
3434 create_disposition,
3435 create_options,
3436 file_attributes,
3437 oplock_request,
3438 private_flags,
3439 &info,
3440 fsp);
3442 if(!NT_STATUS_IS_OK(status)) {
3443 file_free(req, fsp);
3444 fsp = NULL;
3447 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3449 /* A stream open never opens a directory */
3451 if (base_fsp) {
3452 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3453 goto fail;
3457 * Fail the open if it was explicitly a non-directory
3458 * file.
3461 if (create_options & FILE_NON_DIRECTORY_FILE) {
3462 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3463 goto fail;
3466 oplock_request = 0;
3467 status = open_directory(
3468 conn, req, smb_fname, access_mask,
3469 share_access, create_disposition,
3470 create_options, file_attributes,
3471 &info, &fsp);
3475 if (!NT_STATUS_IS_OK(status)) {
3476 goto fail;
3479 fsp->base_fsp = base_fsp;
3482 * According to the MS documentation, the only time the security
3483 * descriptor is applied to the opened file is iff we *created* the
3484 * file; an existing file stays the same.
3486 * Also, it seems (from observation) that you can open the file with
3487 * any access mask but you can still write the sd. We need to override
3488 * the granted access before we call set_sd
3489 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3492 if ((sd != NULL) && (info == FILE_WAS_CREATED)
3493 && lp_nt_acl_support(SNUM(conn))) {
3495 uint32_t sec_info_sent;
3496 uint32_t saved_access_mask = fsp->access_mask;
3498 sec_info_sent = get_sec_info(sd);
3500 fsp->access_mask = FILE_GENERIC_ALL;
3502 /* Convert all the generic bits. */
3503 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3504 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3506 if (sec_info_sent & (SECINFO_OWNER|
3507 SECINFO_GROUP|
3508 SECINFO_DACL|
3509 SECINFO_SACL)) {
3510 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3513 fsp->access_mask = saved_access_mask;
3515 if (!NT_STATUS_IS_OK(status)) {
3516 goto fail;
3520 if ((ea_list != NULL) &&
3521 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3522 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3523 if (!NT_STATUS_IS_OK(status)) {
3524 goto fail;
3528 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3529 status = NT_STATUS_ACCESS_DENIED;
3530 goto fail;
3533 /* Save the requested allocation size. */
3534 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3535 if (allocation_size
3536 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3537 fsp->initial_allocation_size = smb_roundup(
3538 fsp->conn, allocation_size);
3539 if (fsp->is_directory) {
3540 /* Can't set allocation size on a directory. */
3541 status = NT_STATUS_ACCESS_DENIED;
3542 goto fail;
3544 if (vfs_allocate_file_space(
3545 fsp, fsp->initial_allocation_size) == -1) {
3546 status = NT_STATUS_DISK_FULL;
3547 goto fail;
3549 } else {
3550 fsp->initial_allocation_size = smb_roundup(
3551 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3555 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3557 *result = fsp;
3558 if (pinfo != NULL) {
3559 *pinfo = info;
3562 smb_fname->st = fsp->fsp_name->st;
3564 return NT_STATUS_OK;
3566 fail:
3567 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3569 if (fsp != NULL) {
3570 if (base_fsp && fsp->base_fsp == base_fsp) {
3572 * The close_file below will close
3573 * fsp->base_fsp.
3575 base_fsp = NULL;
3577 close_file(req, fsp, ERROR_CLOSE);
3578 fsp = NULL;
3580 if (base_fsp != NULL) {
3581 close_file(req, base_fsp, ERROR_CLOSE);
3582 base_fsp = NULL;
3584 return status;
3588 * Calculate the full path name given a relative fid.
3590 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3591 struct smb_request *req,
3592 uint16_t root_dir_fid,
3593 const struct smb_filename *smb_fname,
3594 struct smb_filename **smb_fname_out)
3596 files_struct *dir_fsp;
3597 char *parent_fname = NULL;
3598 char *new_base_name = NULL;
3599 NTSTATUS status;
3601 if (root_dir_fid == 0 || !smb_fname) {
3602 status = NT_STATUS_INTERNAL_ERROR;
3603 goto out;
3606 dir_fsp = file_fsp(req, root_dir_fid);
3608 if (dir_fsp == NULL) {
3609 status = NT_STATUS_INVALID_HANDLE;
3610 goto out;
3613 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3614 status = NT_STATUS_INVALID_HANDLE;
3615 goto out;
3618 if (!dir_fsp->is_directory) {
3621 * Check to see if this is a mac fork of some kind.
3624 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3625 is_ntfs_stream_smb_fname(smb_fname)) {
3626 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3627 goto out;
3631 we need to handle the case when we get a
3632 relative open relative to a file and the
3633 pathname is blank - this is a reopen!
3634 (hint from demyn plantenberg)
3637 status = NT_STATUS_INVALID_HANDLE;
3638 goto out;
3641 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3643 * We're at the toplevel dir, the final file name
3644 * must not contain ./, as this is filtered out
3645 * normally by srvstr_get_path and unix_convert
3646 * explicitly rejects paths containing ./.
3648 parent_fname = talloc_strdup(talloc_tos(), "");
3649 if (parent_fname == NULL) {
3650 status = NT_STATUS_NO_MEMORY;
3651 goto out;
3653 } else {
3654 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3657 * Copy in the base directory name.
3660 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3661 dir_name_len+2);
3662 if (parent_fname == NULL) {
3663 status = NT_STATUS_NO_MEMORY;
3664 goto out;
3666 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3667 dir_name_len+1);
3670 * Ensure it ends in a '/'.
3671 * We used TALLOC_SIZE +2 to add space for the '/'.
3674 if(dir_name_len
3675 && (parent_fname[dir_name_len-1] != '\\')
3676 && (parent_fname[dir_name_len-1] != '/')) {
3677 parent_fname[dir_name_len] = '/';
3678 parent_fname[dir_name_len+1] = '\0';
3682 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3683 smb_fname->base_name);
3684 if (new_base_name == NULL) {
3685 status = NT_STATUS_NO_MEMORY;
3686 goto out;
3689 status = filename_convert(req,
3690 conn,
3691 req->flags2 & FLAGS2_DFS_PATHNAMES,
3692 new_base_name,
3694 NULL,
3695 smb_fname_out);
3696 if (!NT_STATUS_IS_OK(status)) {
3697 goto out;
3700 out:
3701 TALLOC_FREE(parent_fname);
3702 TALLOC_FREE(new_base_name);
3703 return status;
3706 NTSTATUS create_file_default(connection_struct *conn,
3707 struct smb_request *req,
3708 uint16_t root_dir_fid,
3709 struct smb_filename *smb_fname,
3710 uint32_t access_mask,
3711 uint32_t share_access,
3712 uint32_t create_disposition,
3713 uint32_t create_options,
3714 uint32_t file_attributes,
3715 uint32_t oplock_request,
3716 uint64_t allocation_size,
3717 uint32_t private_flags,
3718 struct security_descriptor *sd,
3719 struct ea_list *ea_list,
3720 files_struct **result,
3721 int *pinfo)
3723 int info = FILE_WAS_OPENED;
3724 files_struct *fsp = NULL;
3725 NTSTATUS status;
3726 bool stream_name = false;
3728 DEBUG(10,("create_file: access_mask = 0x%x "
3729 "file_attributes = 0x%x, share_access = 0x%x, "
3730 "create_disposition = 0x%x create_options = 0x%x "
3731 "oplock_request = 0x%x "
3732 "private_flags = 0x%x "
3733 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3734 "fname = %s\n",
3735 (unsigned int)access_mask,
3736 (unsigned int)file_attributes,
3737 (unsigned int)share_access,
3738 (unsigned int)create_disposition,
3739 (unsigned int)create_options,
3740 (unsigned int)oplock_request,
3741 (unsigned int)private_flags,
3742 (unsigned int)root_dir_fid,
3743 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3746 * Calculate the filename from the root_dir_if if necessary.
3749 if (root_dir_fid != 0) {
3750 struct smb_filename *smb_fname_out = NULL;
3751 status = get_relative_fid_filename(conn, req, root_dir_fid,
3752 smb_fname, &smb_fname_out);
3753 if (!NT_STATUS_IS_OK(status)) {
3754 goto fail;
3756 smb_fname = smb_fname_out;
3760 * Check to see if this is a mac fork of some kind.
3763 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3764 if (stream_name) {
3765 enum FAKE_FILE_TYPE fake_file_type;
3767 fake_file_type = is_fake_file(smb_fname);
3769 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3772 * Here we go! support for changing the disk quotas
3773 * --metze
3775 * We need to fake up to open this MAGIC QUOTA file
3776 * and return a valid FID.
3778 * w2k close this file directly after openening xp
3779 * also tries a QUERY_FILE_INFO on the file and then
3780 * close it
3782 status = open_fake_file(req, conn, req->vuid,
3783 fake_file_type, smb_fname,
3784 access_mask, &fsp);
3785 if (!NT_STATUS_IS_OK(status)) {
3786 goto fail;
3789 ZERO_STRUCT(smb_fname->st);
3790 goto done;
3793 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3794 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3795 goto fail;
3799 /* All file access must go through check_name() */
3801 status = check_name(conn, smb_fname->base_name);
3802 if (!NT_STATUS_IS_OK(status)) {
3803 goto fail;
3806 if (stream_name && is_ntfs_default_stream_smb_fname(smb_fname)) {
3807 int ret;
3808 smb_fname->stream_name = NULL;
3809 /* We have to handle this error here. */
3810 if (create_options & FILE_DIRECTORY_FILE) {
3811 status = NT_STATUS_NOT_A_DIRECTORY;
3812 goto fail;
3814 if (lp_posix_pathnames()) {
3815 ret = SMB_VFS_LSTAT(conn, smb_fname);
3816 } else {
3817 ret = SMB_VFS_STAT(conn, smb_fname);
3820 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3821 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3822 goto fail;
3826 status = create_file_unixpath(
3827 conn, req, smb_fname, access_mask, share_access,
3828 create_disposition, create_options, file_attributes,
3829 oplock_request, allocation_size, private_flags,
3830 sd, ea_list,
3831 &fsp, &info);
3833 if (!NT_STATUS_IS_OK(status)) {
3834 goto fail;
3837 done:
3838 DEBUG(10, ("create_file: info=%d\n", info));
3840 *result = fsp;
3841 if (pinfo != NULL) {
3842 *pinfo = info;
3844 return NT_STATUS_OK;
3846 fail:
3847 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3849 if (fsp != NULL) {
3850 close_file(req, fsp, ERROR_CLOSE);
3851 fsp = NULL;
3853 return status;