Ensure we always write the correct incoming mid into the share mode
[Samba.git] / source3 / smbd / open.c
blobb8984ee44176fa02d21f0f9eb1005885c2db92c7
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;
80 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
81 (SECINFO_OWNER |
82 SECINFO_GROUP |
83 SECINFO_DACL),&sd);
85 if (!NT_STATUS_IS_OK(status)) {
86 DEBUG(10, ("smbd_check_open_rights: Could not get acl "
87 "on %s: %s\n",
88 smb_fname_str_dbg(smb_fname),
89 nt_errstr(status)));
90 return status;
93 status = smb1_file_se_access_check(conn,
94 sd,
95 get_current_nttok(conn),
96 access_mask,
97 access_granted);
99 DEBUG(10,("smbd_check_open_rights: file %s requesting "
100 "0x%x returning 0x%x (%s)\n",
101 smb_fname_str_dbg(smb_fname),
102 (unsigned int)access_mask,
103 (unsigned int)*access_granted,
104 nt_errstr(status) ));
106 if (!NT_STATUS_IS_OK(status)) {
107 if (DEBUGLEVEL >= 10) {
108 DEBUG(10,("smbd_check_open_rights: acl for %s is:\n",
109 smb_fname_str_dbg(smb_fname) ));
110 NDR_PRINT_DEBUG(security_descriptor, sd);
114 TALLOC_FREE(sd);
116 return status;
119 /****************************************************************************
120 fd support routines - attempt to do a dos_open.
121 ****************************************************************************/
123 static NTSTATUS fd_open(struct connection_struct *conn,
124 files_struct *fsp,
125 int flags,
126 mode_t mode)
128 struct smb_filename *smb_fname = fsp->fsp_name;
129 NTSTATUS status = NT_STATUS_OK;
131 #ifdef O_NOFOLLOW
133 * Never follow symlinks on a POSIX client. The
134 * client should be doing this.
137 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
138 flags |= O_NOFOLLOW;
140 #endif
142 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
143 if (fsp->fh->fd == -1) {
144 status = map_nt_error_from_unix(errno);
145 if (errno == EMFILE) {
146 static time_t last_warned = 0L;
148 if (time((time_t *) NULL) > last_warned) {
149 DEBUG(0,("Too many open files, unable "
150 "to open more! smbd's max "
151 "open files = %d\n",
152 lp_max_open_files()));
153 last_warned = time((time_t *) NULL);
159 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
160 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
161 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
163 return status;
166 /****************************************************************************
167 Close the file associated with a fsp.
168 ****************************************************************************/
170 NTSTATUS fd_close(files_struct *fsp)
172 int ret;
174 if (fsp->dptr) {
175 dptr_CloseDir(fsp);
177 if (fsp->fh->fd == -1) {
178 return NT_STATUS_OK; /* What we used to call a stat open. */
180 if (fsp->fh->ref_count > 1) {
181 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
184 ret = SMB_VFS_CLOSE(fsp);
185 fsp->fh->fd = -1;
186 if (ret == -1) {
187 return map_nt_error_from_unix(errno);
189 return NT_STATUS_OK;
192 /****************************************************************************
193 Change the ownership of a file to that of the parent directory.
194 Do this by fd if possible.
195 ****************************************************************************/
197 void change_file_owner_to_parent(connection_struct *conn,
198 const char *inherit_from_dir,
199 files_struct *fsp)
201 struct smb_filename *smb_fname_parent = NULL;
202 NTSTATUS status;
203 int ret;
205 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
206 NULL, NULL, &smb_fname_parent);
207 if (!NT_STATUS_IS_OK(status)) {
208 return;
211 ret = SMB_VFS_STAT(conn, smb_fname_parent);
212 if (ret == -1) {
213 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
214 "directory %s. Error was %s\n",
215 smb_fname_str_dbg(smb_fname_parent),
216 strerror(errno)));
217 TALLOC_FREE(smb_fname_parent);
218 return;
221 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
222 /* Already this uid - no need to change. */
223 DEBUG(10,("change_file_owner_to_parent: file %s "
224 "is already owned by uid %d\n",
225 fsp_str_dbg(fsp),
226 (int)fsp->fsp_name->st.st_ex_uid ));
227 TALLOC_FREE(smb_fname_parent);
228 return;
231 become_root();
232 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
233 unbecome_root();
234 if (ret == -1) {
235 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
236 "file %s to parent directory uid %u. Error "
237 "was %s\n", fsp_str_dbg(fsp),
238 (unsigned int)smb_fname_parent->st.st_ex_uid,
239 strerror(errno) ));
242 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
243 "parent directory uid %u.\n", fsp_str_dbg(fsp),
244 (unsigned int)smb_fname_parent->st.st_ex_uid));
246 TALLOC_FREE(smb_fname_parent);
249 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
250 const char *inherit_from_dir,
251 const char *fname,
252 SMB_STRUCT_STAT *psbuf)
254 struct smb_filename *smb_fname_parent = NULL;
255 struct smb_filename *smb_fname_cwd = NULL;
256 char *saved_dir = NULL;
257 TALLOC_CTX *ctx = talloc_tos();
258 NTSTATUS status = NT_STATUS_OK;
259 int ret;
261 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
262 &smb_fname_parent);
263 if (!NT_STATUS_IS_OK(status)) {
264 return status;
267 ret = SMB_VFS_STAT(conn, smb_fname_parent);
268 if (ret == -1) {
269 status = map_nt_error_from_unix(errno);
270 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
271 "directory %s. Error was %s\n",
272 smb_fname_str_dbg(smb_fname_parent),
273 strerror(errno)));
274 goto out;
277 /* We've already done an lstat into psbuf, and we know it's a
278 directory. If we can cd into the directory and the dev/ino
279 are the same then we can safely chown without races as
280 we're locking the directory in place by being in it. This
281 should work on any UNIX (thanks tridge :-). JRA.
284 saved_dir = vfs_GetWd(ctx,conn);
285 if (!saved_dir) {
286 status = map_nt_error_from_unix(errno);
287 DEBUG(0,("change_dir_owner_to_parent: failed to get "
288 "current working directory. Error was %s\n",
289 strerror(errno)));
290 goto out;
293 /* Chdir into the new path. */
294 if (vfs_ChDir(conn, fname) == -1) {
295 status = map_nt_error_from_unix(errno);
296 DEBUG(0,("change_dir_owner_to_parent: failed to change "
297 "current working directory to %s. Error "
298 "was %s\n", fname, strerror(errno) ));
299 goto chdir;
302 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
303 &smb_fname_cwd);
304 if (!NT_STATUS_IS_OK(status)) {
305 return status;
308 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
309 if (ret == -1) {
310 status = map_nt_error_from_unix(errno);
311 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
312 "directory '.' (%s) Error was %s\n",
313 fname, strerror(errno)));
314 goto chdir;
317 /* Ensure we're pointing at the same place. */
318 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
319 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino ||
320 smb_fname_cwd->st.st_ex_mode != psbuf->st_ex_mode ) {
321 DEBUG(0,("change_dir_owner_to_parent: "
322 "device/inode/mode on directory %s changed. "
323 "Refusing to chown !\n", fname ));
324 status = NT_STATUS_ACCESS_DENIED;
325 goto chdir;
328 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
329 /* Already this uid - no need to change. */
330 DEBUG(10,("change_dir_owner_to_parent: directory %s "
331 "is already owned by uid %d\n",
332 fname,
333 (int)smb_fname_cwd->st.st_ex_uid ));
334 status = NT_STATUS_OK;
335 goto chdir;
338 become_root();
339 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
340 (gid_t)-1);
341 unbecome_root();
342 if (ret == -1) {
343 status = map_nt_error_from_unix(errno);
344 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
345 "directory %s to parent directory uid %u. "
346 "Error was %s\n", fname,
347 (unsigned int)smb_fname_parent->st.st_ex_uid,
348 strerror(errno) ));
349 goto chdir;
352 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
353 "directory %s to parent directory uid %u.\n",
354 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
356 chdir:
357 vfs_ChDir(conn,saved_dir);
358 out:
359 TALLOC_FREE(smb_fname_parent);
360 TALLOC_FREE(smb_fname_cwd);
361 return status;
364 /****************************************************************************
365 Open a file.
366 ****************************************************************************/
368 static NTSTATUS open_file(files_struct *fsp,
369 connection_struct *conn,
370 struct smb_request *req,
371 const char *parent_dir,
372 int flags,
373 mode_t unx_mode,
374 uint32 access_mask, /* client requested access mask. */
375 uint32 open_access_mask) /* what we're actually using in the open. */
377 struct smb_filename *smb_fname = fsp->fsp_name;
378 NTSTATUS status = NT_STATUS_OK;
379 int accmode = (flags & O_ACCMODE);
380 int local_flags = flags;
381 bool file_existed = VALID_STAT(fsp->fsp_name->st);
383 fsp->fh->fd = -1;
384 errno = EPERM;
386 /* Check permissions */
389 * This code was changed after seeing a client open request
390 * containing the open mode of (DENY_WRITE/read-only) with
391 * the 'create if not exist' bit set. The previous code
392 * would fail to open the file read only on a read-only share
393 * as it was checking the flags parameter directly against O_RDONLY,
394 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
395 * JRA.
398 if (!CAN_WRITE(conn)) {
399 /* It's a read-only share - fail if we wanted to write. */
400 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
401 DEBUG(3,("Permission denied opening %s\n",
402 smb_fname_str_dbg(smb_fname)));
403 return NT_STATUS_ACCESS_DENIED;
404 } else if(flags & O_CREAT) {
405 /* We don't want to write - but we must make sure that
406 O_CREAT doesn't create the file if we have write
407 access into the directory.
409 flags &= ~(O_CREAT|O_EXCL);
410 local_flags &= ~(O_CREAT|O_EXCL);
415 * This little piece of insanity is inspired by the
416 * fact that an NT client can open a file for O_RDONLY,
417 * but set the create disposition to FILE_EXISTS_TRUNCATE.
418 * If the client *can* write to the file, then it expects to
419 * truncate the file, even though it is opening for readonly.
420 * Quicken uses this stupid trick in backup file creation...
421 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
422 * for helping track this one down. It didn't bite us in 2.0.x
423 * as we always opened files read-write in that release. JRA.
426 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
427 DEBUG(10,("open_file: truncate requested on read-only open "
428 "for file %s\n", smb_fname_str_dbg(smb_fname)));
429 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
432 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
433 (!file_existed && (local_flags & O_CREAT)) ||
434 ((local_flags & O_TRUNC) == O_TRUNC) ) {
435 const char *wild;
438 * We can't actually truncate here as the file may be locked.
439 * open_file_ntcreate will take care of the truncate later. JRA.
442 local_flags &= ~O_TRUNC;
444 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
446 * We would block on opening a FIFO with no one else on the
447 * other end. Do what we used to do and add O_NONBLOCK to the
448 * open flags. JRA.
451 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
452 local_flags |= O_NONBLOCK;
454 #endif
456 /* Don't create files with Microsoft wildcard characters. */
457 if (fsp->base_fsp) {
459 * wildcard characters are allowed in stream names
460 * only test the basefilename
462 wild = fsp->base_fsp->fsp_name->base_name;
463 } else {
464 wild = smb_fname->base_name;
466 if ((local_flags & O_CREAT) && !file_existed &&
467 ms_has_wild(wild)) {
468 return NT_STATUS_OBJECT_NAME_INVALID;
471 /* Actually do the open */
472 status = fd_open(conn, fsp, local_flags, unx_mode);
473 if (!NT_STATUS_IS_OK(status)) {
474 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
475 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
476 nt_errstr(status),local_flags,flags));
477 return status;
480 if ((local_flags & O_CREAT) && !file_existed) {
482 /* Inherit the ACL if required */
483 if (lp_inherit_perms(SNUM(conn))) {
484 inherit_access_posix_acl(conn, parent_dir,
485 smb_fname->base_name,
486 unx_mode);
489 /* Change the owner if required. */
490 if (lp_inherit_owner(SNUM(conn))) {
491 change_file_owner_to_parent(conn, parent_dir,
492 fsp);
495 notify_fname(conn, NOTIFY_ACTION_ADDED,
496 FILE_NOTIFY_CHANGE_FILE_NAME,
497 smb_fname->base_name);
500 } else {
501 fsp->fh->fd = -1; /* What we used to call a stat open. */
502 if (file_existed) {
503 uint32_t access_granted = 0;
505 status = smbd_check_open_rights(conn,
506 smb_fname,
507 access_mask,
508 &access_granted);
509 if (!NT_STATUS_IS_OK(status)) {
510 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
512 * On NT_STATUS_ACCESS_DENIED, access_granted
513 * contains the denied bits.
516 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
517 (access_granted & FILE_WRITE_ATTRIBUTES) &&
518 (lp_map_readonly(SNUM(conn)) ||
519 lp_map_archive(SNUM(conn)) ||
520 lp_map_hidden(SNUM(conn)) ||
521 lp_map_system(SNUM(conn)))) {
522 access_granted &= ~FILE_WRITE_ATTRIBUTES;
524 DEBUG(10,("open_file: "
525 "overrode "
526 "FILE_WRITE_"
527 "ATTRIBUTES "
528 "on file %s\n",
529 smb_fname_str_dbg(
530 smb_fname)));
533 if ((access_mask & DELETE_ACCESS) &&
534 (access_granted & DELETE_ACCESS) &&
535 can_delete_file_in_directory(conn,
536 smb_fname)) {
537 /* Were we trying to do a stat open
538 * for delete and didn't get DELETE
539 * access (only) ? Check if the
540 * directory allows DELETE_CHILD.
541 * See here:
542 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
543 * for details. */
545 access_granted &= ~DELETE_ACCESS;
547 DEBUG(10,("open_file: "
548 "overrode "
549 "DELETE_ACCESS on "
550 "file %s\n",
551 smb_fname_str_dbg(
552 smb_fname)));
555 if (access_granted != 0) {
556 DEBUG(10,("open_file: Access "
557 "denied on file "
558 "%s\n",
559 smb_fname_str_dbg(
560 smb_fname)));
561 return status;
563 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
564 fsp->posix_open &&
565 S_ISLNK(smb_fname->st.st_ex_mode)) {
566 /* This is a POSIX stat open for delete
567 * or rename on a symlink that points
568 * nowhere. Allow. */
569 DEBUG(10,("open_file: allowing POSIX "
570 "open on bad symlink %s\n",
571 smb_fname_str_dbg(
572 smb_fname)));
573 } else {
574 DEBUG(10,("open_file: "
575 "smbd_check_open_rights on file "
576 "%s returned %s\n",
577 smb_fname_str_dbg(smb_fname),
578 nt_errstr(status) ));
579 return status;
585 if (!file_existed) {
586 int ret;
588 if (fsp->fh->fd == -1) {
589 ret = SMB_VFS_STAT(conn, smb_fname);
590 } else {
591 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
592 /* If we have an fd, this stat should succeed. */
593 if (ret == -1) {
594 DEBUG(0,("Error doing fstat on open file %s "
595 "(%s)\n",
596 smb_fname_str_dbg(smb_fname),
597 strerror(errno) ));
601 /* For a non-io open, this stat failing means file not found. JRA */
602 if (ret == -1) {
603 status = map_nt_error_from_unix(errno);
604 fd_close(fsp);
605 return status;
610 * POSIX allows read-only opens of directories. We don't
611 * want to do this (we use a different code path for this)
612 * so catch a directory open and return an EISDIR. JRA.
615 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
616 fd_close(fsp);
617 errno = EISDIR;
618 return NT_STATUS_FILE_IS_A_DIRECTORY;
621 fsp->mode = smb_fname->st.st_ex_mode;
622 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
623 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
624 fsp->file_pid = req ? req->smbpid : 0;
625 fsp->can_lock = True;
626 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
627 if (!CAN_WRITE(conn)) {
628 fsp->can_write = False;
629 } else {
630 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
631 True : False;
633 fsp->print_file = NULL;
634 fsp->modified = False;
635 fsp->sent_oplock_break = NO_BREAK_SENT;
636 fsp->is_directory = False;
637 if (conn->aio_write_behind_list &&
638 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
639 conn->case_sensitive)) {
640 fsp->aio_write_behind = True;
643 fsp->wcp = NULL; /* Write cache pointer. */
645 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
646 conn->session_info->unix_name,
647 smb_fname_str_dbg(smb_fname),
648 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
649 conn->num_files_open));
651 errno = 0;
652 return NT_STATUS_OK;
655 /*******************************************************************
656 Return True if the filename is one of the special executable types.
657 ********************************************************************/
659 bool is_executable(const char *fname)
661 if ((fname = strrchr_m(fname,'.'))) {
662 if (strequal(fname,".com") ||
663 strequal(fname,".dll") ||
664 strequal(fname,".exe") ||
665 strequal(fname,".sym")) {
666 return True;
669 return False;
672 /****************************************************************************
673 Check if we can open a file with a share mode.
674 Returns True if conflict, False if not.
675 ****************************************************************************/
677 static bool share_conflict(struct share_mode_entry *entry,
678 uint32 access_mask,
679 uint32 share_access)
681 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
682 "entry->share_access = 0x%x, "
683 "entry->private_options = 0x%x\n",
684 (unsigned int)entry->access_mask,
685 (unsigned int)entry->share_access,
686 (unsigned int)entry->private_options));
688 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
689 (unsigned int)access_mask, (unsigned int)share_access));
691 if ((entry->access_mask & (FILE_WRITE_DATA|
692 FILE_APPEND_DATA|
693 FILE_READ_DATA|
694 FILE_EXECUTE|
695 DELETE_ACCESS)) == 0) {
696 DEBUG(10,("share_conflict: No conflict due to "
697 "entry->access_mask = 0x%x\n",
698 (unsigned int)entry->access_mask ));
699 return False;
702 if ((access_mask & (FILE_WRITE_DATA|
703 FILE_APPEND_DATA|
704 FILE_READ_DATA|
705 FILE_EXECUTE|
706 DELETE_ACCESS)) == 0) {
707 DEBUG(10,("share_conflict: No conflict due to "
708 "access_mask = 0x%x\n",
709 (unsigned int)access_mask ));
710 return False;
713 #if 1 /* JRA TEST - Superdebug. */
714 #define CHECK_MASK(num, am, right, sa, share) \
715 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
716 (unsigned int)(num), (unsigned int)(am), \
717 (unsigned int)(right), (unsigned int)(am)&(right) )); \
718 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
719 (unsigned int)(num), (unsigned int)(sa), \
720 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
721 if (((am) & (right)) && !((sa) & (share))) { \
722 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
723 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
724 (unsigned int)(share) )); \
725 return True; \
727 #else
728 #define CHECK_MASK(num, am, right, sa, share) \
729 if (((am) & (right)) && !((sa) & (share))) { \
730 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
731 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
732 (unsigned int)(share) )); \
733 return True; \
735 #endif
737 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
738 share_access, FILE_SHARE_WRITE);
739 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
740 entry->share_access, FILE_SHARE_WRITE);
742 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
743 share_access, FILE_SHARE_READ);
744 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
745 entry->share_access, FILE_SHARE_READ);
747 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
748 share_access, FILE_SHARE_DELETE);
749 CHECK_MASK(6, access_mask, DELETE_ACCESS,
750 entry->share_access, FILE_SHARE_DELETE);
752 DEBUG(10,("share_conflict: No conflict.\n"));
753 return False;
756 #if defined(DEVELOPER)
757 static void validate_my_share_entries(struct smbd_server_connection *sconn,
758 int num,
759 struct share_mode_entry *share_entry)
761 files_struct *fsp;
763 if (!procid_is_me(&share_entry->pid)) {
764 return;
767 if (is_deferred_open_entry(share_entry) &&
768 !open_was_deferred(share_entry->op_mid)) {
769 char *str = talloc_asprintf(talloc_tos(),
770 "Got a deferred entry without a request: "
771 "PANIC: %s\n",
772 share_mode_str(talloc_tos(), num, share_entry));
773 smb_panic(str);
776 if (!is_valid_share_mode_entry(share_entry)) {
777 return;
780 fsp = file_find_dif(sconn, share_entry->id,
781 share_entry->share_file_id);
782 if (!fsp) {
783 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
784 share_mode_str(talloc_tos(), num, share_entry) ));
785 smb_panic("validate_my_share_entries: Cannot match a "
786 "share entry with an open file\n");
789 if (is_deferred_open_entry(share_entry) ||
790 is_unused_share_mode_entry(share_entry)) {
791 goto panic;
794 if ((share_entry->op_type == NO_OPLOCK) &&
795 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
796 /* Someone has already written to it, but I haven't yet
797 * noticed */
798 return;
801 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
802 goto panic;
805 return;
807 panic:
809 char *str;
810 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
811 share_mode_str(talloc_tos(), num, share_entry) ));
812 str = talloc_asprintf(talloc_tos(),
813 "validate_my_share_entries: "
814 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
815 fsp->fsp_name->base_name,
816 (unsigned int)fsp->oplock_type,
817 (unsigned int)share_entry->op_type );
818 smb_panic(str);
821 #endif
823 bool is_stat_open(uint32 access_mask)
825 return (access_mask &&
826 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
827 FILE_WRITE_ATTRIBUTES))==0) &&
828 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
829 FILE_WRITE_ATTRIBUTES)) != 0));
832 /****************************************************************************
833 Deal with share modes
834 Invarient: Share mode must be locked on entry and exit.
835 Returns -1 on error, or number of share modes on success (may be zero).
836 ****************************************************************************/
838 static NTSTATUS open_mode_check(connection_struct *conn,
839 struct share_mode_lock *lck,
840 uint32_t name_hash,
841 uint32 access_mask,
842 uint32 share_access,
843 uint32 create_options,
844 bool *file_existed)
846 int i;
848 if(lck->num_share_modes == 0) {
849 return NT_STATUS_OK;
852 *file_existed = True;
854 /* A delete on close prohibits everything */
856 if (is_delete_on_close_set(lck, name_hash)) {
857 return NT_STATUS_DELETE_PENDING;
860 if (is_stat_open(access_mask)) {
861 /* Stat open that doesn't trigger oplock breaks or share mode
862 * checks... ! JRA. */
863 return NT_STATUS_OK;
867 * Check if the share modes will give us access.
870 #if defined(DEVELOPER)
871 for(i = 0; i < lck->num_share_modes; i++) {
872 validate_my_share_entries(conn->sconn, i,
873 &lck->share_modes[i]);
875 #endif
877 if (!lp_share_modes(SNUM(conn))) {
878 return NT_STATUS_OK;
881 /* Now we check the share modes, after any oplock breaks. */
882 for(i = 0; i < lck->num_share_modes; i++) {
884 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
885 continue;
888 /* someone else has a share lock on it, check to see if we can
889 * too */
890 if (share_conflict(&lck->share_modes[i],
891 access_mask, share_access)) {
892 return NT_STATUS_SHARING_VIOLATION;
896 return NT_STATUS_OK;
899 static bool is_delete_request(files_struct *fsp) {
900 return ((fsp->access_mask == DELETE_ACCESS) &&
901 (fsp->oplock_type == NO_OPLOCK));
905 * Send a break message to the oplock holder and delay the open for
906 * our client.
909 static NTSTATUS send_break_message(files_struct *fsp,
910 struct share_mode_entry *exclusive,
911 uint64_t mid,
912 int oplock_request)
914 NTSTATUS status;
915 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
917 DEBUG(10, ("Sending break request to PID %s\n",
918 procid_str_static(&exclusive->pid)));
919 exclusive->op_mid = mid;
921 /* Create the message. */
922 share_mode_entry_to_message(msg, exclusive);
924 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
925 don't want this set in the share mode struct pointed to by lck. */
927 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
928 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
929 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
932 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
933 MSG_SMB_BREAK_REQUEST,
934 (uint8 *)msg,
935 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
936 if (!NT_STATUS_IS_OK(status)) {
937 DEBUG(3, ("Could not send oplock break message: %s\n",
938 nt_errstr(status)));
941 return status;
945 * Return share_mode_entry pointers for :
946 * 1). Batch oplock entry.
947 * 2). Batch or exclusive oplock entry (may be identical to #1).
948 * bool have_level2_oplock
949 * bool have_no_oplock.
950 * Do internal consistency checks on the share mode for a file.
953 static void find_oplock_types(struct share_mode_lock *lck,
954 struct share_mode_entry **pp_batch,
955 struct share_mode_entry **pp_ex_or_batch,
956 bool *got_level2,
957 bool *got_no_oplock)
959 int i;
961 *pp_batch = NULL;
962 *pp_ex_or_batch = NULL;
963 *got_level2 = false;
964 *got_no_oplock = false;
966 for (i=0; i<lck->num_share_modes; i++) {
967 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
968 continue;
971 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
972 /* batch - can only be one. */
973 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
974 smb_panic("Bad batch oplock entry.");
976 *pp_batch = &lck->share_modes[i];
979 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
980 /* Exclusive or batch - can only be one. */
981 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
982 smb_panic("Bad exclusive or batch oplock entry.");
984 *pp_ex_or_batch = &lck->share_modes[i];
987 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
988 if (*pp_batch || *pp_ex_or_batch) {
989 smb_panic("Bad levelII oplock entry.");
991 *got_level2 = true;
994 if (lck->share_modes[i].op_type == NO_OPLOCK) {
995 if (*pp_batch || *pp_ex_or_batch) {
996 smb_panic("Bad no oplock entry.");
998 *got_no_oplock = true;
1003 static bool delay_for_batch_oplocks(files_struct *fsp,
1004 uint64_t mid,
1005 int oplock_request,
1006 struct share_mode_entry *batch_entry)
1008 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1009 return false;
1012 if (batch_entry != NULL) {
1013 /* Found a batch oplock */
1014 send_break_message(fsp, batch_entry, mid, oplock_request);
1015 return true;
1017 return false;
1020 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1021 uint64_t mid,
1022 int oplock_request,
1023 struct share_mode_entry *ex_entry)
1025 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1026 return false;
1029 if (ex_entry != NULL) {
1030 /* Found an exclusive or batch oplock */
1031 bool delay_it = is_delete_request(fsp) ?
1032 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1033 if (delay_it) {
1034 send_break_message(fsp, ex_entry, mid, oplock_request);
1035 return true;
1038 return false;
1041 static bool file_has_brlocks(files_struct *fsp)
1043 struct byte_range_lock *br_lck;
1045 br_lck = brl_get_locks_readonly(fsp);
1046 if (!br_lck)
1047 return false;
1049 return br_lck->num_locks > 0 ? true : false;
1052 static void grant_fsp_oplock_type(files_struct *fsp,
1053 int oplock_request,
1054 bool got_level2_oplock,
1055 bool got_a_none_oplock)
1057 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1058 lp_level2_oplocks(SNUM(fsp->conn));
1060 /* Start by granting what the client asked for,
1061 but ensure no SAMBA_PRIVATE bits can be set. */
1062 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1064 if (oplock_request & INTERNAL_OPEN_ONLY) {
1065 /* No oplocks on internal open. */
1066 fsp->oplock_type = NO_OPLOCK;
1067 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1068 fsp->oplock_type, fsp_str_dbg(fsp)));
1069 return;
1070 } else if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1071 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1072 fsp_str_dbg(fsp)));
1073 fsp->oplock_type = NO_OPLOCK;
1076 if (is_stat_open(fsp->access_mask)) {
1077 /* Leave the value already set. */
1078 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1079 fsp->oplock_type, fsp_str_dbg(fsp)));
1080 return;
1084 * Match what was requested (fsp->oplock_type) with
1085 * what was found in the existing share modes.
1088 if (got_a_none_oplock) {
1089 fsp->oplock_type = NO_OPLOCK;
1090 } else if (got_level2_oplock) {
1091 if (fsp->oplock_type == NO_OPLOCK ||
1092 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1093 /* Store a level2 oplock, but don't tell the client */
1094 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1095 } else {
1096 fsp->oplock_type = LEVEL_II_OPLOCK;
1098 } else {
1099 /* All share_mode_entries are placeholders or deferred.
1100 * Silently upgrade to fake levelII if the client didn't
1101 * ask for an oplock. */
1102 if (fsp->oplock_type == NO_OPLOCK) {
1103 /* Store a level2 oplock, but don't tell the client */
1104 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1109 * Don't grant level2 to clients that don't want them
1110 * or if we've turned them off.
1112 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1113 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1116 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1117 fsp->oplock_type, fsp_str_dbg(fsp)));
1120 bool request_timed_out(struct timeval request_time,
1121 struct timeval timeout)
1123 struct timeval now, end_time;
1124 GetTimeOfDay(&now);
1125 end_time = timeval_sum(&request_time, &timeout);
1126 return (timeval_compare(&end_time, &now) < 0);
1129 /****************************************************************************
1130 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1131 ****************************************************************************/
1133 static void defer_open(struct share_mode_lock *lck,
1134 struct timeval request_time,
1135 struct timeval timeout,
1136 struct smb_request *req,
1137 struct deferred_open_record *state)
1139 int i;
1141 /* Paranoia check */
1143 for (i=0; i<lck->num_share_modes; i++) {
1144 struct share_mode_entry *e = &lck->share_modes[i];
1146 if (!is_deferred_open_entry(e)) {
1147 continue;
1150 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
1151 DEBUG(0, ("Trying to defer an already deferred "
1152 "request: mid=%llu, exiting\n",
1153 (unsigned long long)req->mid));
1154 exit_server("attempt to defer a deferred request");
1158 /* End paranoia check */
1160 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1161 "open entry for mid %llu\n",
1162 (unsigned int)request_time.tv_sec,
1163 (unsigned int)request_time.tv_usec,
1164 (unsigned long long)req->mid));
1166 if (!push_deferred_open_message_smb(req, request_time, timeout,
1167 state->id, (char *)state, sizeof(*state))) {
1168 exit_server("push_deferred_open_message_smb failed");
1170 add_deferred_open(lck, req->mid, request_time,
1171 sconn_server_id(req->sconn), state->id);
1175 /****************************************************************************
1176 On overwrite open ensure that the attributes match.
1177 ****************************************************************************/
1179 bool open_match_attributes(connection_struct *conn,
1180 uint32 old_dos_attr,
1181 uint32 new_dos_attr,
1182 mode_t existing_unx_mode,
1183 mode_t new_unx_mode,
1184 mode_t *returned_unx_mode)
1186 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1188 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1189 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1191 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1192 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1193 *returned_unx_mode = new_unx_mode;
1194 } else {
1195 *returned_unx_mode = (mode_t)0;
1198 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1199 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1200 "returned_unx_mode = 0%o\n",
1201 (unsigned int)old_dos_attr,
1202 (unsigned int)existing_unx_mode,
1203 (unsigned int)new_dos_attr,
1204 (unsigned int)*returned_unx_mode ));
1206 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1207 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1208 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1209 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1210 return False;
1213 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1214 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1215 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1216 return False;
1219 return True;
1222 /****************************************************************************
1223 Special FCB or DOS processing in the case of a sharing violation.
1224 Try and find a duplicated file handle.
1225 ****************************************************************************/
1227 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1228 connection_struct *conn,
1229 files_struct *fsp_to_dup_into,
1230 const struct smb_filename *smb_fname,
1231 struct file_id id,
1232 uint16 file_pid,
1233 uint16 vuid,
1234 uint32 access_mask,
1235 uint32 share_access,
1236 uint32 create_options)
1238 files_struct *fsp;
1240 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1241 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1243 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1244 fsp = file_find_di_next(fsp)) {
1246 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1247 "vuid = %u, file_pid = %u, private_options = 0x%x "
1248 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1249 fsp->fh->fd, (unsigned int)fsp->vuid,
1250 (unsigned int)fsp->file_pid,
1251 (unsigned int)fsp->fh->private_options,
1252 (unsigned int)fsp->access_mask ));
1254 if (fsp->fh->fd != -1 &&
1255 fsp->vuid == vuid &&
1256 fsp->file_pid == file_pid &&
1257 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1258 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1259 (fsp->access_mask & FILE_WRITE_DATA) &&
1260 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1261 strequal(fsp->fsp_name->stream_name,
1262 smb_fname->stream_name)) {
1263 DEBUG(10,("fcb_or_dos_open: file match\n"));
1264 break;
1268 if (!fsp) {
1269 return NT_STATUS_NOT_FOUND;
1272 /* quite an insane set of semantics ... */
1273 if (is_executable(smb_fname->base_name) &&
1274 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1275 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1276 return NT_STATUS_INVALID_PARAMETER;
1279 /* We need to duplicate this fsp. */
1280 return dup_file_fsp(req, fsp, access_mask, share_access,
1281 create_options, fsp_to_dup_into);
1284 /****************************************************************************
1285 Open a file with a share mode - old openX method - map into NTCreate.
1286 ****************************************************************************/
1288 bool map_open_params_to_ntcreate(const struct smb_filename *smb_fname,
1289 int deny_mode, int open_func,
1290 uint32 *paccess_mask,
1291 uint32 *pshare_mode,
1292 uint32 *pcreate_disposition,
1293 uint32 *pcreate_options,
1294 uint32_t *pprivate_flags)
1296 uint32 access_mask;
1297 uint32 share_mode;
1298 uint32 create_disposition;
1299 uint32 create_options = FILE_NON_DIRECTORY_FILE;
1300 uint32_t private_flags = 0;
1302 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1303 "open_func = 0x%x\n",
1304 smb_fname_str_dbg(smb_fname), (unsigned int)deny_mode,
1305 (unsigned int)open_func ));
1307 /* Create the NT compatible access_mask. */
1308 switch (GET_OPENX_MODE(deny_mode)) {
1309 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1310 case DOS_OPEN_RDONLY:
1311 access_mask = FILE_GENERIC_READ;
1312 break;
1313 case DOS_OPEN_WRONLY:
1314 access_mask = FILE_GENERIC_WRITE;
1315 break;
1316 case DOS_OPEN_RDWR:
1317 case DOS_OPEN_FCB:
1318 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1319 break;
1320 default:
1321 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1322 (unsigned int)GET_OPENX_MODE(deny_mode)));
1323 return False;
1326 /* Create the NT compatible create_disposition. */
1327 switch (open_func) {
1328 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1329 create_disposition = FILE_CREATE;
1330 break;
1332 case OPENX_FILE_EXISTS_OPEN:
1333 create_disposition = FILE_OPEN;
1334 break;
1336 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1337 create_disposition = FILE_OPEN_IF;
1338 break;
1340 case OPENX_FILE_EXISTS_TRUNCATE:
1341 create_disposition = FILE_OVERWRITE;
1342 break;
1344 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1345 create_disposition = FILE_OVERWRITE_IF;
1346 break;
1348 default:
1349 /* From samba4 - to be confirmed. */
1350 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1351 create_disposition = FILE_CREATE;
1352 break;
1354 DEBUG(10,("map_open_params_to_ntcreate: bad "
1355 "open_func 0x%x\n", (unsigned int)open_func));
1356 return False;
1359 /* Create the NT compatible share modes. */
1360 switch (GET_DENY_MODE(deny_mode)) {
1361 case DENY_ALL:
1362 share_mode = FILE_SHARE_NONE;
1363 break;
1365 case DENY_WRITE:
1366 share_mode = FILE_SHARE_READ;
1367 break;
1369 case DENY_READ:
1370 share_mode = FILE_SHARE_WRITE;
1371 break;
1373 case DENY_NONE:
1374 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1375 break;
1377 case DENY_DOS:
1378 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1379 if (is_executable(smb_fname->base_name)) {
1380 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1381 } else {
1382 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1383 share_mode = FILE_SHARE_READ;
1384 } else {
1385 share_mode = FILE_SHARE_NONE;
1388 break;
1390 case DENY_FCB:
1391 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1392 share_mode = FILE_SHARE_NONE;
1393 break;
1395 default:
1396 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1397 (unsigned int)GET_DENY_MODE(deny_mode) ));
1398 return False;
1401 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1402 "share_mode = 0x%x, create_disposition = 0x%x, "
1403 "create_options = 0x%x private_flags = 0x%x\n",
1404 smb_fname_str_dbg(smb_fname),
1405 (unsigned int)access_mask,
1406 (unsigned int)share_mode,
1407 (unsigned int)create_disposition,
1408 (unsigned int)create_options,
1409 (unsigned int)private_flags));
1411 if (paccess_mask) {
1412 *paccess_mask = access_mask;
1414 if (pshare_mode) {
1415 *pshare_mode = share_mode;
1417 if (pcreate_disposition) {
1418 *pcreate_disposition = create_disposition;
1420 if (pcreate_options) {
1421 *pcreate_options = create_options;
1423 if (pprivate_flags) {
1424 *pprivate_flags = private_flags;
1427 return True;
1431 static void schedule_defer_open(struct share_mode_lock *lck,
1432 struct timeval request_time,
1433 struct smb_request *req)
1435 struct deferred_open_record state;
1437 /* This is a relative time, added to the absolute
1438 request_time value to get the absolute timeout time.
1439 Note that if this is the second or greater time we enter
1440 this codepath for this particular request mid then
1441 request_time is left as the absolute time of the *first*
1442 time this request mid was processed. This is what allows
1443 the request to eventually time out. */
1445 struct timeval timeout;
1447 /* Normally the smbd we asked should respond within
1448 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1449 * the client did, give twice the timeout as a safety
1450 * measure here in case the other smbd is stuck
1451 * somewhere else. */
1453 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1455 /* Nothing actually uses state.delayed_for_oplocks
1456 but it's handy to differentiate in debug messages
1457 between a 30 second delay due to oplock break, and
1458 a 1 second delay for share mode conflicts. */
1460 state.delayed_for_oplocks = True;
1461 state.id = lck->id;
1463 if (!request_timed_out(request_time, timeout)) {
1464 defer_open(lck, request_time, timeout, req, &state);
1468 /****************************************************************************
1469 Work out what access_mask to use from what the client sent us.
1470 ****************************************************************************/
1472 static NTSTATUS calculate_access_mask(connection_struct *conn,
1473 const struct smb_filename *smb_fname,
1474 bool file_existed,
1475 uint32_t access_mask,
1476 uint32_t *access_mask_out)
1478 NTSTATUS status;
1481 * Convert GENERIC bits to specific bits.
1484 se_map_generic(&access_mask, &file_generic_mapping);
1486 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1487 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1488 if (file_existed) {
1490 struct security_descriptor *sd;
1491 uint32_t access_granted = 0;
1493 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1494 (SECINFO_OWNER |
1495 SECINFO_GROUP |
1496 SECINFO_DACL),&sd);
1498 if (!NT_STATUS_IS_OK(status)) {
1499 DEBUG(10, ("calculate_access_mask: Could not get acl "
1500 "on file %s: %s\n",
1501 smb_fname_str_dbg(smb_fname),
1502 nt_errstr(status)));
1503 return NT_STATUS_ACCESS_DENIED;
1506 status = smb1_file_se_access_check(conn,
1508 get_current_nttok(conn),
1509 access_mask,
1510 &access_granted);
1512 TALLOC_FREE(sd);
1514 if (!NT_STATUS_IS_OK(status)) {
1515 DEBUG(10, ("calculate_access_mask: Access denied on "
1516 "file %s: when calculating maximum access\n",
1517 smb_fname_str_dbg(smb_fname)));
1518 return NT_STATUS_ACCESS_DENIED;
1521 access_mask = access_granted;
1522 } else {
1523 access_mask = FILE_GENERIC_ALL;
1527 *access_mask_out = access_mask;
1528 return NT_STATUS_OK;
1531 /****************************************************************************
1532 Remove the deferred open entry under lock.
1533 ****************************************************************************/
1535 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1536 struct server_id pid)
1538 struct share_mode_lock *lck = get_share_mode_lock(talloc_tos(), id,
1539 NULL, NULL, NULL);
1540 if (lck == NULL) {
1541 DEBUG(0, ("could not get share mode lock\n"));
1542 } else {
1543 del_deferred_open_entry(lck, mid, pid);
1544 TALLOC_FREE(lck);
1548 /****************************************************************************
1549 Open a file with a share mode. Passed in an already created files_struct *.
1550 ****************************************************************************/
1552 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1553 struct smb_request *req,
1554 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1555 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1556 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1557 uint32 create_options, /* options such as delete on close. */
1558 uint32 new_dos_attributes, /* attributes used for new file. */
1559 int oplock_request, /* internal Samba oplock codes. */
1560 /* Information (FILE_EXISTS etc.) */
1561 uint32_t private_flags, /* Samba specific flags. */
1562 int *pinfo,
1563 files_struct *fsp)
1565 struct smb_filename *smb_fname = fsp->fsp_name;
1566 int flags=0;
1567 int flags2=0;
1568 bool file_existed = VALID_STAT(smb_fname->st);
1569 bool def_acl = False;
1570 bool posix_open = False;
1571 bool new_file_created = False;
1572 bool clear_ads = false;
1573 struct file_id id;
1574 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1575 mode_t new_unx_mode = (mode_t)0;
1576 mode_t unx_mode = (mode_t)0;
1577 int info;
1578 uint32 existing_dos_attributes = 0;
1579 struct timeval request_time = timeval_zero();
1580 struct share_mode_lock *lck = NULL;
1581 uint32 open_access_mask = access_mask;
1582 NTSTATUS status;
1583 char *parent_dir;
1585 ZERO_STRUCT(id);
1587 /* Windows allows a new file to be created and
1588 silently removes a FILE_ATTRIBUTE_DIRECTORY
1589 sent by the client. Do the same. */
1591 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1593 if (conn->printer) {
1595 * Printers are handled completely differently.
1596 * Most of the passed parameters are ignored.
1599 if (pinfo) {
1600 *pinfo = FILE_WAS_CREATED;
1603 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1604 smb_fname_str_dbg(smb_fname)));
1606 if (!req) {
1607 DEBUG(0,("open_file_ntcreate: printer open without "
1608 "an SMB request!\n"));
1609 return NT_STATUS_INTERNAL_ERROR;
1612 return print_spool_open(fsp, smb_fname->base_name,
1613 req->vuid);
1616 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1617 NULL)) {
1618 return NT_STATUS_NO_MEMORY;
1621 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1622 posix_open = True;
1623 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1624 new_dos_attributes = 0;
1625 } else {
1626 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1627 * created new. */
1628 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1629 smb_fname, parent_dir);
1632 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1633 "access_mask=0x%x share_access=0x%x "
1634 "create_disposition = 0x%x create_options=0x%x "
1635 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1636 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1637 access_mask, share_access, create_disposition,
1638 create_options, (unsigned int)unx_mode, oplock_request,
1639 (unsigned int)private_flags));
1641 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1642 DEBUG(0, ("No smb request but not an internal only open!\n"));
1643 return NT_STATUS_INTERNAL_ERROR;
1647 * Only non-internal opens can be deferred at all
1650 if (req) {
1651 void *ptr;
1652 if (get_deferred_open_message_state(req,
1653 &request_time,
1654 &ptr)) {
1656 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1657 /* Remember the absolute time of the original
1658 request with this mid. We'll use it later to
1659 see if this has timed out. */
1661 /* Remove the deferred open entry under lock. */
1662 remove_deferred_open_entry(
1663 state->id, req->mid,
1664 sconn_server_id(req->sconn));
1666 /* Ensure we don't reprocess this message. */
1667 remove_deferred_open_message_smb(req->mid);
1671 status = check_name(conn, smb_fname->base_name);
1672 if (!NT_STATUS_IS_OK(status)) {
1673 return status;
1676 if (!posix_open) {
1677 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1678 if (file_existed) {
1679 existing_dos_attributes = dos_mode(conn, smb_fname);
1683 /* ignore any oplock requests if oplocks are disabled */
1684 if (!lp_oplocks(SNUM(conn)) ||
1685 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1686 /* Mask off everything except the private Samba bits. */
1687 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1690 /* this is for OS/2 long file names - say we don't support them */
1691 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1692 /* OS/2 Workplace shell fix may be main code stream in a later
1693 * release. */
1694 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1695 "supported.\n"));
1696 if (use_nt_status()) {
1697 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1699 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1702 switch( create_disposition ) {
1704 * Currently we're using FILE_SUPERSEDE as the same as
1705 * FILE_OVERWRITE_IF but they really are
1706 * different. FILE_SUPERSEDE deletes an existing file
1707 * (requiring delete access) then recreates it.
1709 case FILE_SUPERSEDE:
1710 /* If file exists replace/overwrite. If file doesn't
1711 * exist create. */
1712 flags2 |= (O_CREAT | O_TRUNC);
1713 clear_ads = true;
1714 break;
1716 case FILE_OVERWRITE_IF:
1717 /* If file exists replace/overwrite. If file doesn't
1718 * exist create. */
1719 flags2 |= (O_CREAT | O_TRUNC);
1720 clear_ads = true;
1721 break;
1723 case FILE_OPEN:
1724 /* If file exists open. If file doesn't exist error. */
1725 if (!file_existed) {
1726 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1727 "requested for file %s and file "
1728 "doesn't exist.\n",
1729 smb_fname_str_dbg(smb_fname)));
1730 errno = ENOENT;
1731 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1733 break;
1735 case FILE_OVERWRITE:
1736 /* If file exists overwrite. If file doesn't exist
1737 * error. */
1738 if (!file_existed) {
1739 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1740 "requested for file %s and file "
1741 "doesn't exist.\n",
1742 smb_fname_str_dbg(smb_fname) ));
1743 errno = ENOENT;
1744 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1746 flags2 |= O_TRUNC;
1747 clear_ads = true;
1748 break;
1750 case FILE_CREATE:
1751 /* If file exists error. If file doesn't exist
1752 * create. */
1753 if (file_existed) {
1754 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1755 "requested for file %s and file "
1756 "already exists.\n",
1757 smb_fname_str_dbg(smb_fname)));
1758 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1759 errno = EISDIR;
1760 } else {
1761 errno = EEXIST;
1763 return map_nt_error_from_unix(errno);
1765 flags2 |= (O_CREAT|O_EXCL);
1766 break;
1768 case FILE_OPEN_IF:
1769 /* If file exists open. If file doesn't exist
1770 * create. */
1771 flags2 |= O_CREAT;
1772 break;
1774 default:
1775 return NT_STATUS_INVALID_PARAMETER;
1778 /* We only care about matching attributes on file exists and
1779 * overwrite. */
1781 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1782 (create_disposition == FILE_OVERWRITE_IF))) {
1783 if (!open_match_attributes(conn, existing_dos_attributes,
1784 new_dos_attributes,
1785 smb_fname->st.st_ex_mode,
1786 unx_mode, &new_unx_mode)) {
1787 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1788 "for file %s (%x %x) (0%o, 0%o)\n",
1789 smb_fname_str_dbg(smb_fname),
1790 existing_dos_attributes,
1791 new_dos_attributes,
1792 (unsigned int)smb_fname->st.st_ex_mode,
1793 (unsigned int)unx_mode ));
1794 errno = EACCES;
1795 return NT_STATUS_ACCESS_DENIED;
1799 status = calculate_access_mask(conn, smb_fname, file_existed,
1800 access_mask,
1801 &access_mask);
1802 if (!NT_STATUS_IS_OK(status)) {
1803 DEBUG(10, ("open_file_ntcreate: calculate_access_mask "
1804 "on file %s returned %s\n",
1805 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1806 return status;
1809 open_access_mask = access_mask;
1811 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1812 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1815 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1816 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1817 access_mask));
1820 * Note that we ignore the append flag as append does not
1821 * mean the same thing under DOS and Unix.
1824 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1825 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1826 /* DENY_DOS opens are always underlying read-write on the
1827 file handle, no matter what the requested access mask
1828 says. */
1829 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1830 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1831 flags = O_RDWR;
1832 } else {
1833 flags = O_WRONLY;
1835 } else {
1836 flags = O_RDONLY;
1840 * Currently we only look at FILE_WRITE_THROUGH for create options.
1843 #if defined(O_SYNC)
1844 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1845 flags2 |= O_SYNC;
1847 #endif /* O_SYNC */
1849 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1850 flags2 |= O_APPEND;
1853 if (!posix_open && !CAN_WRITE(conn)) {
1855 * We should really return a permission denied error if either
1856 * O_CREAT or O_TRUNC are set, but for compatibility with
1857 * older versions of Samba we just AND them out.
1859 flags2 &= ~(O_CREAT|O_TRUNC);
1863 * Ensure we can't write on a read-only share or file.
1866 if (flags != O_RDONLY && file_existed &&
1867 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1868 DEBUG(5,("open_file_ntcreate: write access requested for "
1869 "file %s on read only %s\n",
1870 smb_fname_str_dbg(smb_fname),
1871 !CAN_WRITE(conn) ? "share" : "file" ));
1872 errno = EACCES;
1873 return NT_STATUS_ACCESS_DENIED;
1876 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1877 fsp->share_access = share_access;
1878 fsp->fh->private_options = private_flags;
1879 fsp->access_mask = open_access_mask; /* We change this to the
1880 * requested access_mask after
1881 * the open is done. */
1882 fsp->posix_open = posix_open;
1884 /* Ensure no SAMBA_PRIVATE bits can be set. */
1885 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1887 if (timeval_is_zero(&request_time)) {
1888 request_time = fsp->open_time;
1891 if (file_existed) {
1892 struct share_mode_entry *batch_entry = NULL;
1893 struct share_mode_entry *exclusive_entry = NULL;
1894 bool got_level2_oplock = false;
1895 bool got_a_none_oplock = false;
1897 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1898 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1900 lck = get_share_mode_lock(talloc_tos(), id,
1901 conn->connectpath,
1902 smb_fname, &old_write_time);
1904 if (lck == NULL) {
1905 DEBUG(0, ("Could not get share mode lock\n"));
1906 return NT_STATUS_SHARING_VIOLATION;
1909 /* Get the types we need to examine. */
1910 find_oplock_types(lck,
1911 &batch_entry,
1912 &exclusive_entry,
1913 &got_level2_oplock,
1914 &got_a_none_oplock);
1916 /* First pass - send break only on batch oplocks. */
1917 if ((req != NULL) &&
1918 delay_for_batch_oplocks(fsp,
1919 req->mid,
1920 oplock_request,
1921 batch_entry)) {
1922 schedule_defer_open(lck, request_time, req);
1923 TALLOC_FREE(lck);
1924 return NT_STATUS_SHARING_VIOLATION;
1927 /* Use the client requested access mask here, not the one we
1928 * open with. */
1929 status = open_mode_check(conn, lck, fsp->name_hash,
1930 access_mask, share_access,
1931 create_options, &file_existed);
1933 if (NT_STATUS_IS_OK(status)) {
1934 /* We might be going to allow this open. Check oplock
1935 * status again. */
1936 /* Second pass - send break for both batch or
1937 * exclusive oplocks. */
1938 if ((req != NULL) &&
1939 delay_for_exclusive_oplocks(
1940 fsp,
1941 req->mid,
1942 oplock_request,
1943 exclusive_entry)) {
1944 schedule_defer_open(lck, request_time, req);
1945 TALLOC_FREE(lck);
1946 return NT_STATUS_SHARING_VIOLATION;
1950 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1951 /* DELETE_PENDING is not deferred for a second */
1952 TALLOC_FREE(lck);
1953 return status;
1956 grant_fsp_oplock_type(fsp,
1957 oplock_request,
1958 got_level2_oplock,
1959 got_a_none_oplock);
1961 if (!NT_STATUS_IS_OK(status)) {
1962 uint32 can_access_mask;
1963 bool can_access = True;
1965 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1967 /* Check if this can be done with the deny_dos and fcb
1968 * calls. */
1969 if (private_flags &
1970 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1971 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1972 if (req == NULL) {
1973 DEBUG(0, ("DOS open without an SMB "
1974 "request!\n"));
1975 TALLOC_FREE(lck);
1976 return NT_STATUS_INTERNAL_ERROR;
1979 /* Use the client requested access mask here,
1980 * not the one we open with. */
1981 status = fcb_or_dos_open(req,
1982 conn,
1983 fsp,
1984 smb_fname,
1986 req->smbpid,
1987 req->vuid,
1988 access_mask,
1989 share_access,
1990 create_options);
1992 if (NT_STATUS_IS_OK(status)) {
1993 TALLOC_FREE(lck);
1994 if (pinfo) {
1995 *pinfo = FILE_WAS_OPENED;
1997 return NT_STATUS_OK;
2002 * This next line is a subtlety we need for
2003 * MS-Access. If a file open will fail due to share
2004 * permissions and also for security (access) reasons,
2005 * we need to return the access failed error, not the
2006 * share error. We can't open the file due to kernel
2007 * oplock deadlock (it's possible we failed above on
2008 * the open_mode_check()) so use a userspace check.
2011 if (flags & O_RDWR) {
2012 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2013 } else if (flags & O_WRONLY) {
2014 can_access_mask = FILE_WRITE_DATA;
2015 } else {
2016 can_access_mask = FILE_READ_DATA;
2019 if (((can_access_mask & FILE_WRITE_DATA) &&
2020 !CAN_WRITE(conn)) ||
2021 !can_access_file_data(conn, smb_fname,
2022 can_access_mask)) {
2023 can_access = False;
2027 * If we're returning a share violation, ensure we
2028 * cope with the braindead 1 second delay.
2031 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2032 lp_defer_sharing_violations()) {
2033 struct timeval timeout;
2034 struct deferred_open_record state;
2035 int timeout_usecs;
2037 /* this is a hack to speed up torture tests
2038 in 'make test' */
2039 timeout_usecs = lp_parm_int(SNUM(conn),
2040 "smbd","sharedelay",
2041 SHARING_VIOLATION_USEC_WAIT);
2043 /* This is a relative time, added to the absolute
2044 request_time value to get the absolute timeout time.
2045 Note that if this is the second or greater time we enter
2046 this codepath for this particular request mid then
2047 request_time is left as the absolute time of the *first*
2048 time this request mid was processed. This is what allows
2049 the request to eventually time out. */
2051 timeout = timeval_set(0, timeout_usecs);
2053 /* Nothing actually uses state.delayed_for_oplocks
2054 but it's handy to differentiate in debug messages
2055 between a 30 second delay due to oplock break, and
2056 a 1 second delay for share mode conflicts. */
2058 state.delayed_for_oplocks = False;
2059 state.id = id;
2061 if ((req != NULL)
2062 && !request_timed_out(request_time,
2063 timeout)) {
2064 defer_open(lck, request_time, timeout,
2065 req, &state);
2069 TALLOC_FREE(lck);
2070 if (can_access) {
2072 * We have detected a sharing violation here
2073 * so return the correct error code
2075 status = NT_STATUS_SHARING_VIOLATION;
2076 } else {
2077 status = NT_STATUS_ACCESS_DENIED;
2079 return status;
2083 * We exit this block with the share entry *locked*.....
2087 SMB_ASSERT(!file_existed || (lck != NULL));
2090 * Ensure we pay attention to default ACLs on directories if required.
2093 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2094 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2095 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2098 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2099 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2100 (unsigned int)flags, (unsigned int)flags2,
2101 (unsigned int)unx_mode, (unsigned int)access_mask,
2102 (unsigned int)open_access_mask));
2105 * open_file strips any O_TRUNC flags itself.
2108 fsp_open = open_file(fsp, conn, req, parent_dir,
2109 flags|flags2, unx_mode, access_mask,
2110 open_access_mask);
2112 if (!NT_STATUS_IS_OK(fsp_open)) {
2113 if (lck != NULL) {
2114 TALLOC_FREE(lck);
2116 return fsp_open;
2119 if (!file_existed) {
2120 struct share_mode_entry *batch_entry = NULL;
2121 struct share_mode_entry *exclusive_entry = NULL;
2122 bool got_level2_oplock = false;
2123 bool got_a_none_oplock = false;
2124 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2126 * Deal with the race condition where two smbd's detect the
2127 * file doesn't exist and do the create at the same time. One
2128 * of them will win and set a share mode, the other (ie. this
2129 * one) should check if the requested share mode for this
2130 * create is allowed.
2134 * Now the file exists and fsp is successfully opened,
2135 * fsp->dev and fsp->inode are valid and should replace the
2136 * dev=0,inode=0 from a non existent file. Spotted by
2137 * Nadav Danieli <nadavd@exanet.com>. JRA.
2140 id = fsp->file_id;
2142 lck = get_share_mode_lock(talloc_tos(), id,
2143 conn->connectpath,
2144 smb_fname, &old_write_time);
2146 if (lck == NULL) {
2147 DEBUG(0, ("open_file_ntcreate: Could not get share "
2148 "mode lock for %s\n",
2149 smb_fname_str_dbg(smb_fname)));
2150 fd_close(fsp);
2151 return NT_STATUS_SHARING_VIOLATION;
2154 /* Get the types we need to examine. */
2155 find_oplock_types(lck,
2156 &batch_entry,
2157 &exclusive_entry,
2158 &got_level2_oplock,
2159 &got_a_none_oplock);
2161 /* First pass - send break only on batch oplocks. */
2162 if ((req != NULL) &&
2163 delay_for_batch_oplocks(fsp,
2164 req->mid,
2165 oplock_request,
2166 batch_entry)) {
2167 schedule_defer_open(lck, request_time, req);
2168 TALLOC_FREE(lck);
2169 fd_close(fsp);
2170 return NT_STATUS_SHARING_VIOLATION;
2173 status = open_mode_check(conn, lck, fsp->name_hash,
2174 access_mask, share_access,
2175 create_options, &file_existed);
2177 if (NT_STATUS_IS_OK(status)) {
2178 /* We might be going to allow this open. Check oplock
2179 * status again. */
2180 /* Second pass - send break for both batch or
2181 * exclusive oplocks. */
2182 if ((req != NULL) &&
2183 delay_for_exclusive_oplocks(
2184 fsp,
2185 req->mid,
2186 oplock_request,
2187 exclusive_entry)) {
2188 schedule_defer_open(lck, request_time, req);
2189 TALLOC_FREE(lck);
2190 fd_close(fsp);
2191 return NT_STATUS_SHARING_VIOLATION;
2195 if (!NT_STATUS_IS_OK(status)) {
2196 struct deferred_open_record state;
2198 fd_close(fsp);
2200 state.delayed_for_oplocks = False;
2201 state.id = id;
2203 /* Do it all over again immediately. In the second
2204 * round we will find that the file existed and handle
2205 * the DELETE_PENDING and FCB cases correctly. No need
2206 * to duplicate the code here. Essentially this is a
2207 * "goto top of this function", but don't tell
2208 * anybody... */
2210 if (req != NULL) {
2211 defer_open(lck, request_time, timeval_zero(),
2212 req, &state);
2214 TALLOC_FREE(lck);
2215 return status;
2218 grant_fsp_oplock_type(fsp,
2219 oplock_request,
2220 got_level2_oplock,
2221 got_a_none_oplock);
2224 * We exit this block with the share entry *locked*.....
2229 SMB_ASSERT(lck != NULL);
2231 /* Delete streams if create_disposition requires it */
2232 if (file_existed && clear_ads &&
2233 !is_ntfs_stream_smb_fname(smb_fname)) {
2234 status = delete_all_streams(conn, smb_fname->base_name);
2235 if (!NT_STATUS_IS_OK(status)) {
2236 TALLOC_FREE(lck);
2237 fd_close(fsp);
2238 return status;
2242 /* note that we ignore failure for the following. It is
2243 basically a hack for NFS, and NFS will never set one of
2244 these only read them. Nobody but Samba can ever set a deny
2245 mode and we have already checked our more authoritative
2246 locking database for permission to set this deny mode. If
2247 the kernel refuses the operations then the kernel is wrong.
2248 note that GPFS supports it as well - jmcd */
2250 if (fsp->fh->fd != -1) {
2251 int ret_flock;
2252 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2253 if(ret_flock == -1 ){
2255 TALLOC_FREE(lck);
2256 fd_close(fsp);
2258 return NT_STATUS_SHARING_VIOLATION;
2263 * At this point onwards, we can guarentee that the share entry
2264 * is locked, whether we created the file or not, and that the
2265 * deny mode is compatible with all current opens.
2269 * If requested, truncate the file.
2272 if (file_existed && (flags2&O_TRUNC)) {
2274 * We are modifing the file after open - update the stat
2275 * struct..
2277 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2278 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2279 status = map_nt_error_from_unix(errno);
2280 TALLOC_FREE(lck);
2281 fd_close(fsp);
2282 return status;
2287 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2288 * but we don't have to store this - just ignore it on access check.
2290 if (conn->sconn->using_smb2) {
2292 * SMB2 doesn't return it (according to Microsoft tests).
2293 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2294 * File created with access = 0x7 (Read, Write, Delete)
2295 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2297 fsp->access_mask = access_mask;
2298 } else {
2299 /* But SMB1 does. */
2300 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2303 if (file_existed) {
2304 /* stat opens on existing files don't get oplocks. */
2305 if (is_stat_open(open_access_mask)) {
2306 fsp->oplock_type = NO_OPLOCK;
2309 if (!(flags2 & O_TRUNC)) {
2310 info = FILE_WAS_OPENED;
2311 } else {
2312 info = FILE_WAS_OVERWRITTEN;
2314 } else {
2315 info = FILE_WAS_CREATED;
2318 if (pinfo) {
2319 *pinfo = info;
2323 * Setup the oplock info in both the shared memory and
2324 * file structs.
2327 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2329 * Could not get the kernel oplock or there are byte-range
2330 * locks on the file.
2332 fsp->oplock_type = NO_OPLOCK;
2335 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
2336 new_file_created = True;
2339 set_share_mode(lck, fsp, get_current_uid(conn),
2340 req ? req->mid : 0,
2341 fsp->oplock_type);
2343 /* Handle strange delete on close create semantics. */
2344 if (create_options & FILE_DELETE_ON_CLOSE) {
2346 status = can_set_delete_on_close(fsp, new_dos_attributes);
2348 if (!NT_STATUS_IS_OK(status)) {
2349 /* Remember to delete the mode we just added. */
2350 del_share_mode(lck, fsp);
2351 TALLOC_FREE(lck);
2352 fd_close(fsp);
2353 return status;
2355 /* Note that here we set the *inital* delete on close flag,
2356 not the regular one. The magic gets handled in close. */
2357 fsp->initial_delete_on_close = True;
2360 if (new_file_created) {
2361 /* Files should be initially set as archive */
2362 if (lp_map_archive(SNUM(conn)) ||
2363 lp_store_dos_attributes(SNUM(conn))) {
2364 if (!posix_open) {
2365 if (file_set_dosmode(conn, smb_fname,
2366 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2367 parent_dir, true) == 0) {
2368 unx_mode = smb_fname->st.st_ex_mode;
2374 /* Determine sparse flag. */
2375 if (posix_open) {
2376 /* POSIX opens are sparse by default. */
2377 fsp->is_sparse = true;
2378 } else {
2379 fsp->is_sparse = (file_existed &&
2380 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2384 * Take care of inherited ACLs on created files - if default ACL not
2385 * selected.
2388 if (!posix_open && !file_existed && !def_acl) {
2390 int saved_errno = errno; /* We might get ENOSYS in the next
2391 * call.. */
2393 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2394 errno == ENOSYS) {
2395 errno = saved_errno; /* Ignore ENOSYS */
2398 } else if (new_unx_mode) {
2400 int ret = -1;
2402 /* Attributes need changing. File already existed. */
2405 int saved_errno = errno; /* We might get ENOSYS in the
2406 * next call.. */
2407 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2409 if (ret == -1 && errno == ENOSYS) {
2410 errno = saved_errno; /* Ignore ENOSYS */
2411 } else {
2412 DEBUG(5, ("open_file_ntcreate: reset "
2413 "attributes of file %s to 0%o\n",
2414 smb_fname_str_dbg(smb_fname),
2415 (unsigned int)new_unx_mode));
2416 ret = 0; /* Don't do the fchmod below. */
2420 if ((ret == -1) &&
2421 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2422 DEBUG(5, ("open_file_ntcreate: failed to reset "
2423 "attributes of file %s to 0%o\n",
2424 smb_fname_str_dbg(smb_fname),
2425 (unsigned int)new_unx_mode));
2428 /* If this is a successful open, we must remove any deferred open
2429 * records. */
2430 if (req != NULL) {
2431 del_deferred_open_entry(lck, req->mid,
2432 sconn_server_id(req->sconn));
2434 TALLOC_FREE(lck);
2436 return NT_STATUS_OK;
2440 /****************************************************************************
2441 Open a file for for write to ensure that we can fchmod it.
2442 ****************************************************************************/
2444 NTSTATUS open_file_fchmod(connection_struct *conn,
2445 struct smb_filename *smb_fname,
2446 files_struct **result)
2448 if (!VALID_STAT(smb_fname->st)) {
2449 return NT_STATUS_INVALID_PARAMETER;
2452 return SMB_VFS_CREATE_FILE(
2453 conn, /* conn */
2454 NULL, /* req */
2455 0, /* root_dir_fid */
2456 smb_fname, /* fname */
2457 FILE_WRITE_DATA, /* access_mask */
2458 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2459 FILE_SHARE_DELETE),
2460 FILE_OPEN, /* create_disposition*/
2461 0, /* create_options */
2462 0, /* file_attributes */
2463 INTERNAL_OPEN_ONLY, /* oplock_request */
2464 0, /* allocation_size */
2465 0, /* private_flags */
2466 NULL, /* sd */
2467 NULL, /* ea_list */
2468 result, /* result */
2469 NULL); /* pinfo */
2472 static NTSTATUS mkdir_internal(connection_struct *conn,
2473 struct smb_filename *smb_dname,
2474 uint32 file_attributes)
2476 mode_t mode;
2477 char *parent_dir;
2478 NTSTATUS status;
2479 bool posix_open = false;
2481 if(!CAN_WRITE(conn)) {
2482 DEBUG(5,("mkdir_internal: failing create on read-only share "
2483 "%s\n", lp_servicename(SNUM(conn))));
2484 return NT_STATUS_ACCESS_DENIED;
2487 status = check_name(conn, smb_dname->base_name);
2488 if (!NT_STATUS_IS_OK(status)) {
2489 return status;
2492 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2493 NULL)) {
2494 return NT_STATUS_NO_MEMORY;
2497 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2498 posix_open = true;
2499 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2500 } else {
2501 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2504 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2505 return map_nt_error_from_unix(errno);
2508 /* Ensure we're checking for a symlink here.... */
2509 /* We don't want to get caught by a symlink racer. */
2511 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2512 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2513 smb_fname_str_dbg(smb_dname), strerror(errno)));
2514 return map_nt_error_from_unix(errno);
2517 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2518 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2519 smb_fname_str_dbg(smb_dname)));
2520 return NT_STATUS_ACCESS_DENIED;
2523 if (lp_store_dos_attributes(SNUM(conn))) {
2524 if (!posix_open) {
2525 file_set_dosmode(conn, smb_dname,
2526 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2527 parent_dir, true);
2531 if (lp_inherit_perms(SNUM(conn))) {
2532 inherit_access_posix_acl(conn, parent_dir,
2533 smb_dname->base_name, mode);
2536 if (!posix_open) {
2538 * Check if high bits should have been set,
2539 * then (if bits are missing): add them.
2540 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2541 * dir.
2543 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2544 (mode & ~smb_dname->st.st_ex_mode)) {
2545 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2546 (smb_dname->st.st_ex_mode |
2547 (mode & ~smb_dname->st.st_ex_mode)));
2551 /* Change the owner if required. */
2552 if (lp_inherit_owner(SNUM(conn))) {
2553 change_dir_owner_to_parent(conn, parent_dir,
2554 smb_dname->base_name,
2555 &smb_dname->st);
2558 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2559 smb_dname->base_name);
2561 return NT_STATUS_OK;
2564 /****************************************************************************
2565 Ensure we didn't get symlink raced on opening a directory.
2566 ****************************************************************************/
2568 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2569 const SMB_STRUCT_STAT *sbuf2)
2571 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2572 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2573 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2574 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2575 return false;
2577 return true;
2580 /****************************************************************************
2581 Open a directory from an NT SMB call.
2582 ****************************************************************************/
2584 static NTSTATUS open_directory(connection_struct *conn,
2585 struct smb_request *req,
2586 struct smb_filename *smb_dname,
2587 uint32 access_mask,
2588 uint32 share_access,
2589 uint32 create_disposition,
2590 uint32 create_options,
2591 uint32 file_attributes,
2592 int *pinfo,
2593 files_struct **result)
2595 files_struct *fsp = NULL;
2596 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2597 struct share_mode_lock *lck = NULL;
2598 NTSTATUS status;
2599 struct timespec mtimespec;
2600 int info = 0;
2602 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
2604 /* Ensure we have a directory attribute. */
2605 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2607 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2608 "share_access = 0x%x create_options = 0x%x, "
2609 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2610 smb_fname_str_dbg(smb_dname),
2611 (unsigned int)access_mask,
2612 (unsigned int)share_access,
2613 (unsigned int)create_options,
2614 (unsigned int)create_disposition,
2615 (unsigned int)file_attributes));
2617 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2618 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2619 is_ntfs_stream_smb_fname(smb_dname)) {
2620 DEBUG(2, ("open_directory: %s is a stream name!\n",
2621 smb_fname_str_dbg(smb_dname)));
2622 return NT_STATUS_NOT_A_DIRECTORY;
2625 status = calculate_access_mask(conn, smb_dname, dir_existed,
2626 access_mask, &access_mask);
2627 if (!NT_STATUS_IS_OK(status)) {
2628 DEBUG(10, ("open_directory: calculate_access_mask "
2629 "on file %s returned %s\n",
2630 smb_fname_str_dbg(smb_dname),
2631 nt_errstr(status)));
2632 return status;
2635 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2636 !security_token_has_privilege(get_current_nttok(conn),
2637 SEC_PRIV_SECURITY)) {
2638 DEBUG(10, ("open_directory: open on %s "
2639 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2640 smb_fname_str_dbg(smb_dname)));
2641 return NT_STATUS_PRIVILEGE_NOT_HELD;
2644 switch( create_disposition ) {
2645 case FILE_OPEN:
2647 if (!dir_existed) {
2648 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2651 info = FILE_WAS_OPENED;
2652 break;
2654 case FILE_CREATE:
2656 /* If directory exists error. If directory doesn't
2657 * exist create. */
2659 status = mkdir_internal(conn, smb_dname,
2660 file_attributes);
2662 if (!NT_STATUS_IS_OK(status)) {
2663 DEBUG(2, ("open_directory: unable to create "
2664 "%s. Error was %s\n",
2665 smb_fname_str_dbg(smb_dname),
2666 nt_errstr(status)));
2667 return status;
2670 info = FILE_WAS_CREATED;
2671 break;
2673 case FILE_OPEN_IF:
2675 * If directory exists open. If directory doesn't
2676 * exist create.
2679 status = mkdir_internal(conn, smb_dname,
2680 file_attributes);
2682 if (NT_STATUS_IS_OK(status)) {
2683 info = FILE_WAS_CREATED;
2686 if (NT_STATUS_EQUAL(status,
2687 NT_STATUS_OBJECT_NAME_COLLISION)) {
2688 info = FILE_WAS_OPENED;
2689 status = NT_STATUS_OK;
2691 break;
2693 case FILE_SUPERSEDE:
2694 case FILE_OVERWRITE:
2695 case FILE_OVERWRITE_IF:
2696 default:
2697 DEBUG(5,("open_directory: invalid create_disposition "
2698 "0x%x for directory %s\n",
2699 (unsigned int)create_disposition,
2700 smb_fname_str_dbg(smb_dname)));
2701 return NT_STATUS_INVALID_PARAMETER;
2704 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2705 DEBUG(5,("open_directory: %s is not a directory !\n",
2706 smb_fname_str_dbg(smb_dname)));
2707 return NT_STATUS_NOT_A_DIRECTORY;
2710 if (info == FILE_WAS_OPENED) {
2711 uint32_t access_granted = 0;
2712 status = smbd_check_open_rights(conn, smb_dname, access_mask,
2713 &access_granted);
2715 /* Were we trying to do a directory open
2716 * for delete and didn't get DELETE
2717 * access (only) ? Check if the
2718 * directory allows DELETE_CHILD.
2719 * See here:
2720 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
2721 * for details. */
2723 if ((NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
2724 (access_mask & DELETE_ACCESS) &&
2725 (access_granted == DELETE_ACCESS) &&
2726 can_delete_file_in_directory(conn, smb_dname))) {
2727 DEBUG(10,("open_directory: overrode ACCESS_DENIED "
2728 "on directory %s\n",
2729 smb_fname_str_dbg(smb_dname)));
2730 status = NT_STATUS_OK;
2733 if (!NT_STATUS_IS_OK(status)) {
2734 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2735 "file %s failed with %s\n",
2736 smb_fname_str_dbg(smb_dname),
2737 nt_errstr(status)));
2738 return status;
2742 status = file_new(req, conn, &fsp);
2743 if(!NT_STATUS_IS_OK(status)) {
2744 return status;
2748 * Setup the files_struct for it.
2751 fsp->mode = smb_dname->st.st_ex_mode;
2752 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2753 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2754 fsp->file_pid = req ? req->smbpid : 0;
2755 fsp->can_lock = False;
2756 fsp->can_read = False;
2757 fsp->can_write = False;
2759 fsp->share_access = share_access;
2760 fsp->fh->private_options = 0;
2762 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2764 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2765 fsp->print_file = NULL;
2766 fsp->modified = False;
2767 fsp->oplock_type = NO_OPLOCK;
2768 fsp->sent_oplock_break = NO_BREAK_SENT;
2769 fsp->is_directory = True;
2770 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2771 status = fsp_set_smb_fname(fsp, smb_dname);
2772 if (!NT_STATUS_IS_OK(status)) {
2773 file_free(req, fsp);
2774 return status;
2777 mtimespec = smb_dname->st.st_ex_mtime;
2779 #ifdef O_DIRECTORY
2780 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2781 #else
2782 /* POSIX allows us to open a directory with O_RDONLY. */
2783 status = fd_open(conn, fsp, O_RDONLY, 0);
2784 #endif
2785 if (!NT_STATUS_IS_OK(status)) {
2786 DEBUG(5, ("open_directory: Could not open fd for "
2787 "%s (%s)\n",
2788 smb_fname_str_dbg(smb_dname),
2789 nt_errstr(status)));
2790 file_free(req, fsp);
2791 return status;
2794 status = vfs_stat_fsp(fsp);
2795 if (!NT_STATUS_IS_OK(status)) {
2796 fd_close(fsp);
2797 file_free(req, fsp);
2798 return status;
2801 /* Ensure there was no race condition. */
2802 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2803 DEBUG(5,("open_directory: stat struct differs for "
2804 "directory %s.\n",
2805 smb_fname_str_dbg(smb_dname)));
2806 fd_close(fsp);
2807 file_free(req, fsp);
2808 return NT_STATUS_ACCESS_DENIED;
2811 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2812 conn->connectpath, smb_dname, &mtimespec);
2814 if (lck == NULL) {
2815 DEBUG(0, ("open_directory: Could not get share mode lock for "
2816 "%s\n", smb_fname_str_dbg(smb_dname)));
2817 fd_close(fsp);
2818 file_free(req, fsp);
2819 return NT_STATUS_SHARING_VIOLATION;
2822 status = open_mode_check(conn, lck, fsp->name_hash,
2823 access_mask, share_access,
2824 create_options, &dir_existed);
2826 if (!NT_STATUS_IS_OK(status)) {
2827 TALLOC_FREE(lck);
2828 fd_close(fsp);
2829 file_free(req, fsp);
2830 return status;
2833 set_share_mode(lck, fsp, get_current_uid(conn),
2834 req ? req->mid : 0, NO_OPLOCK);
2836 /* For directories the delete on close bit at open time seems
2837 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2838 if (create_options & FILE_DELETE_ON_CLOSE) {
2839 status = can_set_delete_on_close(fsp, 0);
2840 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2841 TALLOC_FREE(lck);
2842 fd_close(fsp);
2843 file_free(req, fsp);
2844 return status;
2847 if (NT_STATUS_IS_OK(status)) {
2848 /* Note that here we set the *inital* delete on close flag,
2849 not the regular one. The magic gets handled in close. */
2850 fsp->initial_delete_on_close = True;
2854 TALLOC_FREE(lck);
2856 if (pinfo) {
2857 *pinfo = info;
2860 *result = fsp;
2861 return NT_STATUS_OK;
2864 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2865 struct smb_filename *smb_dname)
2867 NTSTATUS status;
2868 files_struct *fsp;
2870 status = SMB_VFS_CREATE_FILE(
2871 conn, /* conn */
2872 req, /* req */
2873 0, /* root_dir_fid */
2874 smb_dname, /* fname */
2875 FILE_READ_ATTRIBUTES, /* access_mask */
2876 FILE_SHARE_NONE, /* share_access */
2877 FILE_CREATE, /* create_disposition*/
2878 FILE_DIRECTORY_FILE, /* create_options */
2879 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2880 0, /* oplock_request */
2881 0, /* allocation_size */
2882 0, /* private_flags */
2883 NULL, /* sd */
2884 NULL, /* ea_list */
2885 &fsp, /* result */
2886 NULL); /* pinfo */
2888 if (NT_STATUS_IS_OK(status)) {
2889 close_file(req, fsp, NORMAL_CLOSE);
2892 return status;
2895 /****************************************************************************
2896 Receive notification that one of our open files has been renamed by another
2897 smbd process.
2898 ****************************************************************************/
2900 void msg_file_was_renamed(struct messaging_context *msg,
2901 void *private_data,
2902 uint32_t msg_type,
2903 struct server_id server_id,
2904 DATA_BLOB *data)
2906 struct smbd_server_connection *sconn;
2907 files_struct *fsp;
2908 char *frm = (char *)data->data;
2909 struct file_id id;
2910 const char *sharepath;
2911 const char *base_name;
2912 const char *stream_name;
2913 struct smb_filename *smb_fname = NULL;
2914 size_t sp_len, bn_len;
2915 NTSTATUS status;
2917 sconn = msg_ctx_to_sconn(msg);
2918 if (sconn == NULL) {
2919 DEBUG(1, ("could not find sconn\n"));
2920 return;
2923 if (data->data == NULL
2924 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2925 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2926 (int)data->length));
2927 return;
2930 /* Unpack the message. */
2931 pull_file_id_24(frm, &id);
2932 sharepath = &frm[24];
2933 sp_len = strlen(sharepath);
2934 base_name = sharepath + sp_len + 1;
2935 bn_len = strlen(base_name);
2936 stream_name = sharepath + sp_len + 1 + bn_len + 1;
2938 /* stream_name must always be NULL if there is no stream. */
2939 if (stream_name[0] == '\0') {
2940 stream_name = NULL;
2943 status = create_synthetic_smb_fname(talloc_tos(), base_name,
2944 stream_name, NULL, &smb_fname);
2945 if (!NT_STATUS_IS_OK(status)) {
2946 return;
2949 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2950 "file_id %s\n",
2951 sharepath, smb_fname_str_dbg(smb_fname),
2952 file_id_string_tos(&id)));
2954 for(fsp = file_find_di_first(sconn, id); fsp;
2955 fsp = file_find_di_next(fsp)) {
2956 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2958 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2959 fsp->fnum, fsp_str_dbg(fsp),
2960 smb_fname_str_dbg(smb_fname)));
2961 status = fsp_set_smb_fname(fsp, smb_fname);
2962 if (!NT_STATUS_IS_OK(status)) {
2963 goto out;
2965 } else {
2966 /* TODO. JRA. */
2967 /* Now we have the complete path we can work out if this is
2968 actually within this share and adjust newname accordingly. */
2969 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2970 "not sharepath %s) "
2971 "fnum %d from %s -> %s\n",
2972 fsp->conn->connectpath,
2973 sharepath,
2974 fsp->fnum,
2975 fsp_str_dbg(fsp),
2976 smb_fname_str_dbg(smb_fname)));
2979 out:
2980 TALLOC_FREE(smb_fname);
2981 return;
2985 * If a main file is opened for delete, all streams need to be checked for
2986 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2987 * If that works, delete them all by setting the delete on close and close.
2990 NTSTATUS open_streams_for_delete(connection_struct *conn,
2991 const char *fname)
2993 struct stream_struct *stream_info;
2994 files_struct **streams;
2995 int i;
2996 unsigned int num_streams;
2997 TALLOC_CTX *frame = talloc_stackframe();
2998 NTSTATUS status;
3000 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
3001 &num_streams, &stream_info);
3003 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3004 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3005 DEBUG(10, ("no streams around\n"));
3006 TALLOC_FREE(frame);
3007 return NT_STATUS_OK;
3010 if (!NT_STATUS_IS_OK(status)) {
3011 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
3012 nt_errstr(status)));
3013 goto fail;
3016 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3017 num_streams));
3019 if (num_streams == 0) {
3020 TALLOC_FREE(frame);
3021 return NT_STATUS_OK;
3024 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
3025 if (streams == NULL) {
3026 DEBUG(0, ("talloc failed\n"));
3027 status = NT_STATUS_NO_MEMORY;
3028 goto fail;
3031 for (i=0; i<num_streams; i++) {
3032 struct smb_filename *smb_fname = NULL;
3034 if (strequal(stream_info[i].name, "::$DATA")) {
3035 streams[i] = NULL;
3036 continue;
3039 status = create_synthetic_smb_fname(talloc_tos(), fname,
3040 stream_info[i].name,
3041 NULL, &smb_fname);
3042 if (!NT_STATUS_IS_OK(status)) {
3043 goto fail;
3046 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3047 DEBUG(10, ("Unable to stat stream: %s\n",
3048 smb_fname_str_dbg(smb_fname)));
3051 status = SMB_VFS_CREATE_FILE(
3052 conn, /* conn */
3053 NULL, /* req */
3054 0, /* root_dir_fid */
3055 smb_fname, /* fname */
3056 DELETE_ACCESS, /* access_mask */
3057 (FILE_SHARE_READ | /* share_access */
3058 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3059 FILE_OPEN, /* create_disposition*/
3060 0, /* create_options */
3061 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3062 0, /* oplock_request */
3063 0, /* allocation_size */
3064 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3065 NULL, /* sd */
3066 NULL, /* ea_list */
3067 &streams[i], /* result */
3068 NULL); /* pinfo */
3070 if (!NT_STATUS_IS_OK(status)) {
3071 DEBUG(10, ("Could not open stream %s: %s\n",
3072 smb_fname_str_dbg(smb_fname),
3073 nt_errstr(status)));
3075 TALLOC_FREE(smb_fname);
3076 break;
3078 TALLOC_FREE(smb_fname);
3082 * don't touch the variable "status" beyond this point :-)
3085 for (i -= 1 ; i >= 0; i--) {
3086 if (streams[i] == NULL) {
3087 continue;
3090 DEBUG(10, ("Closing stream # %d, %s\n", i,
3091 fsp_str_dbg(streams[i])));
3092 close_file(NULL, streams[i], NORMAL_CLOSE);
3095 fail:
3096 TALLOC_FREE(frame);
3097 return status;
3101 * Wrapper around open_file_ntcreate and open_directory
3104 static NTSTATUS create_file_unixpath(connection_struct *conn,
3105 struct smb_request *req,
3106 struct smb_filename *smb_fname,
3107 uint32_t access_mask,
3108 uint32_t share_access,
3109 uint32_t create_disposition,
3110 uint32_t create_options,
3111 uint32_t file_attributes,
3112 uint32_t oplock_request,
3113 uint64_t allocation_size,
3114 uint32_t private_flags,
3115 struct security_descriptor *sd,
3116 struct ea_list *ea_list,
3118 files_struct **result,
3119 int *pinfo)
3121 int info = FILE_WAS_OPENED;
3122 files_struct *base_fsp = NULL;
3123 files_struct *fsp = NULL;
3124 NTSTATUS status;
3126 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3127 "file_attributes = 0x%x, share_access = 0x%x, "
3128 "create_disposition = 0x%x create_options = 0x%x "
3129 "oplock_request = 0x%x private_flags = 0x%x "
3130 "ea_list = 0x%p, sd = 0x%p, "
3131 "fname = %s\n",
3132 (unsigned int)access_mask,
3133 (unsigned int)file_attributes,
3134 (unsigned int)share_access,
3135 (unsigned int)create_disposition,
3136 (unsigned int)create_options,
3137 (unsigned int)oplock_request,
3138 (unsigned int)private_flags,
3139 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3141 if (create_options & FILE_OPEN_BY_FILE_ID) {
3142 status = NT_STATUS_NOT_SUPPORTED;
3143 goto fail;
3146 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3147 status = NT_STATUS_INVALID_PARAMETER;
3148 goto fail;
3151 if (req == NULL) {
3152 oplock_request |= INTERNAL_OPEN_ONLY;
3155 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3156 && (access_mask & DELETE_ACCESS)
3157 && !is_ntfs_stream_smb_fname(smb_fname)) {
3159 * We can't open a file with DELETE access if any of the
3160 * streams is open without FILE_SHARE_DELETE
3162 status = open_streams_for_delete(conn, smb_fname->base_name);
3164 if (!NT_STATUS_IS_OK(status)) {
3165 goto fail;
3169 /* This is the correct thing to do (check every time) but can_delete
3170 * is expensive (it may have to read the parent directory
3171 * permissions). So for now we're not doing it unless we have a strong
3172 * hint the client is really going to delete this file. If the client
3173 * is forcing FILE_CREATE let the filesystem take care of the
3174 * permissions. */
3176 /* Setting FILE_SHARE_DELETE is the hint. */
3178 if (lp_acl_check_permissions(SNUM(conn))
3179 && (create_disposition != FILE_CREATE)
3180 && (access_mask & DELETE_ACCESS)
3181 && (!(can_delete_file_in_directory(conn, smb_fname) ||
3182 can_access_file_acl(conn, smb_fname, DELETE_ACCESS)))) {
3183 status = NT_STATUS_ACCESS_DENIED;
3184 DEBUG(10,("create_file_unixpath: open file %s "
3185 "for delete ACCESS_DENIED\n",
3186 smb_fname_str_dbg(smb_fname)));
3187 goto fail;
3190 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3191 !security_token_has_privilege(get_current_nttok(conn),
3192 SEC_PRIV_SECURITY)) {
3193 DEBUG(10, ("create_file_unixpath: open on %s "
3194 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3195 smb_fname_str_dbg(smb_fname)));
3196 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3197 goto fail;
3200 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3201 && is_ntfs_stream_smb_fname(smb_fname)
3202 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3203 uint32 base_create_disposition;
3204 struct smb_filename *smb_fname_base = NULL;
3206 if (create_options & FILE_DIRECTORY_FILE) {
3207 status = NT_STATUS_NOT_A_DIRECTORY;
3208 goto fail;
3211 switch (create_disposition) {
3212 case FILE_OPEN:
3213 base_create_disposition = FILE_OPEN;
3214 break;
3215 default:
3216 base_create_disposition = FILE_OPEN_IF;
3217 break;
3220 /* Create an smb_filename with stream_name == NULL. */
3221 status = create_synthetic_smb_fname(talloc_tos(),
3222 smb_fname->base_name,
3223 NULL, NULL,
3224 &smb_fname_base);
3225 if (!NT_STATUS_IS_OK(status)) {
3226 goto fail;
3229 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3230 DEBUG(10, ("Unable to stat stream: %s\n",
3231 smb_fname_str_dbg(smb_fname_base)));
3234 /* Open the base file. */
3235 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3236 FILE_SHARE_READ
3237 | FILE_SHARE_WRITE
3238 | FILE_SHARE_DELETE,
3239 base_create_disposition,
3240 0, 0, 0, 0, 0, NULL, NULL,
3241 &base_fsp, NULL);
3242 TALLOC_FREE(smb_fname_base);
3244 if (!NT_STATUS_IS_OK(status)) {
3245 DEBUG(10, ("create_file_unixpath for base %s failed: "
3246 "%s\n", smb_fname->base_name,
3247 nt_errstr(status)));
3248 goto fail;
3250 /* we don't need to low level fd */
3251 fd_close(base_fsp);
3255 * If it's a request for a directory open, deal with it separately.
3258 if (create_options & FILE_DIRECTORY_FILE) {
3260 if (create_options & FILE_NON_DIRECTORY_FILE) {
3261 status = NT_STATUS_INVALID_PARAMETER;
3262 goto fail;
3265 /* Can't open a temp directory. IFS kit test. */
3266 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3267 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3268 status = NT_STATUS_INVALID_PARAMETER;
3269 goto fail;
3273 * We will get a create directory here if the Win32
3274 * app specified a security descriptor in the
3275 * CreateDirectory() call.
3278 oplock_request = 0;
3279 status = open_directory(
3280 conn, req, smb_fname, access_mask, share_access,
3281 create_disposition, create_options, file_attributes,
3282 &info, &fsp);
3283 } else {
3286 * Ordinary file case.
3289 status = file_new(req, conn, &fsp);
3290 if(!NT_STATUS_IS_OK(status)) {
3291 goto fail;
3294 status = fsp_set_smb_fname(fsp, smb_fname);
3295 if (!NT_STATUS_IS_OK(status)) {
3296 goto fail;
3300 * We're opening the stream element of a base_fsp
3301 * we already opened. Set up the base_fsp pointer.
3303 if (base_fsp) {
3304 fsp->base_fsp = base_fsp;
3307 status = open_file_ntcreate(conn,
3308 req,
3309 access_mask,
3310 share_access,
3311 create_disposition,
3312 create_options,
3313 file_attributes,
3314 oplock_request,
3315 private_flags,
3316 &info,
3317 fsp);
3319 if(!NT_STATUS_IS_OK(status)) {
3320 file_free(req, fsp);
3321 fsp = NULL;
3324 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3326 /* A stream open never opens a directory */
3328 if (base_fsp) {
3329 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3330 goto fail;
3334 * Fail the open if it was explicitly a non-directory
3335 * file.
3338 if (create_options & FILE_NON_DIRECTORY_FILE) {
3339 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3340 goto fail;
3343 oplock_request = 0;
3344 status = open_directory(
3345 conn, req, smb_fname, access_mask,
3346 share_access, create_disposition,
3347 create_options, file_attributes,
3348 &info, &fsp);
3352 if (!NT_STATUS_IS_OK(status)) {
3353 goto fail;
3356 fsp->base_fsp = base_fsp;
3359 * According to the MS documentation, the only time the security
3360 * descriptor is applied to the opened file is iff we *created* the
3361 * file; an existing file stays the same.
3363 * Also, it seems (from observation) that you can open the file with
3364 * any access mask but you can still write the sd. We need to override
3365 * the granted access before we call set_sd
3366 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3369 if ((sd != NULL) && (info == FILE_WAS_CREATED)
3370 && lp_nt_acl_support(SNUM(conn))) {
3372 uint32_t sec_info_sent;
3373 uint32_t saved_access_mask = fsp->access_mask;
3375 sec_info_sent = get_sec_info(sd);
3377 fsp->access_mask = FILE_GENERIC_ALL;
3379 /* Convert all the generic bits. */
3380 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3381 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3383 if (sec_info_sent & (SECINFO_OWNER|
3384 SECINFO_GROUP|
3385 SECINFO_DACL|
3386 SECINFO_SACL)) {
3387 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3390 fsp->access_mask = saved_access_mask;
3392 if (!NT_STATUS_IS_OK(status)) {
3393 goto fail;
3397 if ((ea_list != NULL) &&
3398 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3399 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3400 if (!NT_STATUS_IS_OK(status)) {
3401 goto fail;
3405 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3406 status = NT_STATUS_ACCESS_DENIED;
3407 goto fail;
3410 /* Save the requested allocation size. */
3411 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3412 if (allocation_size
3413 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3414 fsp->initial_allocation_size = smb_roundup(
3415 fsp->conn, allocation_size);
3416 if (fsp->is_directory) {
3417 /* Can't set allocation size on a directory. */
3418 status = NT_STATUS_ACCESS_DENIED;
3419 goto fail;
3421 if (vfs_allocate_file_space(
3422 fsp, fsp->initial_allocation_size) == -1) {
3423 status = NT_STATUS_DISK_FULL;
3424 goto fail;
3426 } else {
3427 fsp->initial_allocation_size = smb_roundup(
3428 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3432 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3434 *result = fsp;
3435 if (pinfo != NULL) {
3436 *pinfo = info;
3439 smb_fname->st = fsp->fsp_name->st;
3441 return NT_STATUS_OK;
3443 fail:
3444 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3446 if (fsp != NULL) {
3447 if (base_fsp && fsp->base_fsp == base_fsp) {
3449 * The close_file below will close
3450 * fsp->base_fsp.
3452 base_fsp = NULL;
3454 close_file(req, fsp, ERROR_CLOSE);
3455 fsp = NULL;
3457 if (base_fsp != NULL) {
3458 close_file(req, base_fsp, ERROR_CLOSE);
3459 base_fsp = NULL;
3461 return status;
3465 * Calculate the full path name given a relative fid.
3467 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3468 struct smb_request *req,
3469 uint16_t root_dir_fid,
3470 const struct smb_filename *smb_fname,
3471 struct smb_filename **smb_fname_out)
3473 files_struct *dir_fsp;
3474 char *parent_fname = NULL;
3475 char *new_base_name = NULL;
3476 NTSTATUS status;
3478 if (root_dir_fid == 0 || !smb_fname) {
3479 status = NT_STATUS_INTERNAL_ERROR;
3480 goto out;
3483 dir_fsp = file_fsp(req, root_dir_fid);
3485 if (dir_fsp == NULL) {
3486 status = NT_STATUS_INVALID_HANDLE;
3487 goto out;
3490 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3491 status = NT_STATUS_INVALID_HANDLE;
3492 goto out;
3495 if (!dir_fsp->is_directory) {
3498 * Check to see if this is a mac fork of some kind.
3501 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3502 is_ntfs_stream_smb_fname(smb_fname)) {
3503 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3504 goto out;
3508 we need to handle the case when we get a
3509 relative open relative to a file and the
3510 pathname is blank - this is a reopen!
3511 (hint from demyn plantenberg)
3514 status = NT_STATUS_INVALID_HANDLE;
3515 goto out;
3518 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3520 * We're at the toplevel dir, the final file name
3521 * must not contain ./, as this is filtered out
3522 * normally by srvstr_get_path and unix_convert
3523 * explicitly rejects paths containing ./.
3525 parent_fname = talloc_strdup(talloc_tos(), "");
3526 if (parent_fname == NULL) {
3527 status = NT_STATUS_NO_MEMORY;
3528 goto out;
3530 } else {
3531 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3534 * Copy in the base directory name.
3537 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3538 dir_name_len+2);
3539 if (parent_fname == NULL) {
3540 status = NT_STATUS_NO_MEMORY;
3541 goto out;
3543 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3544 dir_name_len+1);
3547 * Ensure it ends in a '/'.
3548 * We used TALLOC_SIZE +2 to add space for the '/'.
3551 if(dir_name_len
3552 && (parent_fname[dir_name_len-1] != '\\')
3553 && (parent_fname[dir_name_len-1] != '/')) {
3554 parent_fname[dir_name_len] = '/';
3555 parent_fname[dir_name_len+1] = '\0';
3559 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3560 smb_fname->base_name);
3561 if (new_base_name == NULL) {
3562 status = NT_STATUS_NO_MEMORY;
3563 goto out;
3566 status = filename_convert(req,
3567 conn,
3568 req->flags2 & FLAGS2_DFS_PATHNAMES,
3569 new_base_name,
3571 NULL,
3572 smb_fname_out);
3573 if (!NT_STATUS_IS_OK(status)) {
3574 goto out;
3577 out:
3578 TALLOC_FREE(parent_fname);
3579 TALLOC_FREE(new_base_name);
3580 return status;
3583 NTSTATUS create_file_default(connection_struct *conn,
3584 struct smb_request *req,
3585 uint16_t root_dir_fid,
3586 struct smb_filename *smb_fname,
3587 uint32_t access_mask,
3588 uint32_t share_access,
3589 uint32_t create_disposition,
3590 uint32_t create_options,
3591 uint32_t file_attributes,
3592 uint32_t oplock_request,
3593 uint64_t allocation_size,
3594 uint32_t private_flags,
3595 struct security_descriptor *sd,
3596 struct ea_list *ea_list,
3597 files_struct **result,
3598 int *pinfo)
3600 int info = FILE_WAS_OPENED;
3601 files_struct *fsp = NULL;
3602 NTSTATUS status;
3603 bool stream_name = false;
3605 DEBUG(10,("create_file: access_mask = 0x%x "
3606 "file_attributes = 0x%x, share_access = 0x%x, "
3607 "create_disposition = 0x%x create_options = 0x%x "
3608 "oplock_request = 0x%x "
3609 "private_flags = 0x%x "
3610 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3611 "fname = %s\n",
3612 (unsigned int)access_mask,
3613 (unsigned int)file_attributes,
3614 (unsigned int)share_access,
3615 (unsigned int)create_disposition,
3616 (unsigned int)create_options,
3617 (unsigned int)oplock_request,
3618 (unsigned int)private_flags,
3619 (unsigned int)root_dir_fid,
3620 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3623 * Calculate the filename from the root_dir_if if necessary.
3626 if (root_dir_fid != 0) {
3627 struct smb_filename *smb_fname_out = NULL;
3628 status = get_relative_fid_filename(conn, req, root_dir_fid,
3629 smb_fname, &smb_fname_out);
3630 if (!NT_STATUS_IS_OK(status)) {
3631 goto fail;
3633 smb_fname = smb_fname_out;
3637 * Check to see if this is a mac fork of some kind.
3640 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3641 if (stream_name) {
3642 enum FAKE_FILE_TYPE fake_file_type;
3644 fake_file_type = is_fake_file(smb_fname);
3646 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3649 * Here we go! support for changing the disk quotas
3650 * --metze
3652 * We need to fake up to open this MAGIC QUOTA file
3653 * and return a valid FID.
3655 * w2k close this file directly after openening xp
3656 * also tries a QUERY_FILE_INFO on the file and then
3657 * close it
3659 status = open_fake_file(req, conn, req->vuid,
3660 fake_file_type, smb_fname,
3661 access_mask, &fsp);
3662 if (!NT_STATUS_IS_OK(status)) {
3663 goto fail;
3666 ZERO_STRUCT(smb_fname->st);
3667 goto done;
3670 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3671 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3672 goto fail;
3676 /* All file access must go through check_name() */
3678 status = check_name(conn, smb_fname->base_name);
3679 if (!NT_STATUS_IS_OK(status)) {
3680 goto fail;
3683 if (stream_name && is_ntfs_default_stream_smb_fname(smb_fname)) {
3684 int ret;
3685 smb_fname->stream_name = NULL;
3686 /* We have to handle this error here. */
3687 if (create_options & FILE_DIRECTORY_FILE) {
3688 status = NT_STATUS_NOT_A_DIRECTORY;
3689 goto fail;
3691 if (lp_posix_pathnames()) {
3692 ret = SMB_VFS_LSTAT(conn, smb_fname);
3693 } else {
3694 ret = SMB_VFS_STAT(conn, smb_fname);
3697 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3698 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3699 goto fail;
3703 status = create_file_unixpath(
3704 conn, req, smb_fname, access_mask, share_access,
3705 create_disposition, create_options, file_attributes,
3706 oplock_request, allocation_size, private_flags,
3707 sd, ea_list,
3708 &fsp, &info);
3710 if (!NT_STATUS_IS_OK(status)) {
3711 goto fail;
3714 done:
3715 DEBUG(10, ("create_file: info=%d\n", info));
3717 *result = fsp;
3718 if (pinfo != NULL) {
3719 *pinfo = info;
3721 return NT_STATUS_OK;
3723 fail:
3724 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3726 if (fsp != NULL) {
3727 close_file(req, fsp, ERROR_CLOSE);
3728 fsp = NULL;
3730 return status;