s4:provision - Updated FDS schema mapping.
[Samba/kamenim.git] / source3 / smbd / open.c
blobfd9796dbf4fdf1562374caa044d8e0aad376100b
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 "smbd/globals.h"
25 extern const struct generic_mapping file_generic_mapping;
27 struct deferred_open_record {
28 bool delayed_for_oplocks;
29 struct file_id id;
32 static NTSTATUS create_file_unixpath(connection_struct *conn,
33 struct smb_request *req,
34 struct smb_filename *smb_fname,
35 uint32_t access_mask,
36 uint32_t share_access,
37 uint32_t create_disposition,
38 uint32_t create_options,
39 uint32_t file_attributes,
40 uint32_t oplock_request,
41 uint64_t allocation_size,
42 uint32_t private_flags,
43 struct security_descriptor *sd,
44 struct ea_list *ea_list,
46 files_struct **result,
47 int *pinfo);
49 /****************************************************************************
50 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
51 ****************************************************************************/
53 NTSTATUS smb1_file_se_access_check(const struct security_descriptor *sd,
54 const NT_USER_TOKEN *token,
55 uint32_t access_desired,
56 uint32_t *access_granted)
58 return se_access_check(sd,
59 token,
60 (access_desired & ~FILE_READ_ATTRIBUTES),
61 access_granted);
64 /****************************************************************************
65 Check if we have open rights.
66 ****************************************************************************/
68 NTSTATUS smbd_check_open_rights(struct connection_struct *conn,
69 const struct smb_filename *smb_fname,
70 uint32_t access_mask,
71 uint32_t *access_granted)
73 /* Check if we have rights to open. */
74 NTSTATUS status;
75 struct security_descriptor *sd = NULL;
77 *access_granted = 0;
79 if (conn->server_info->utok.uid == 0 || conn->admin_user) {
80 /* I'm sorry sir, I didn't know you were root... */
81 *access_granted = access_mask;
82 if (access_mask & SEC_FLAG_MAXIMUM_ALLOWED) {
83 *access_granted |= FILE_GENERIC_ALL;
85 return NT_STATUS_OK;
88 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
89 (OWNER_SECURITY_INFORMATION |
90 GROUP_SECURITY_INFORMATION |
91 DACL_SECURITY_INFORMATION),&sd);
93 if (!NT_STATUS_IS_OK(status)) {
94 DEBUG(10, ("smbd_check_open_rights: Could not get acl "
95 "on %s: %s\n",
96 smb_fname_str_dbg(smb_fname),
97 nt_errstr(status)));
98 return status;
101 status = smb1_file_se_access_check(sd,
102 conn->server_info->ptok,
103 access_mask,
104 access_granted);
106 DEBUG(10,("smbd_check_open_rights: file %s requesting "
107 "0x%x returning 0x%x (%s)\n",
108 smb_fname_str_dbg(smb_fname),
109 (unsigned int)access_mask,
110 (unsigned int)*access_granted,
111 nt_errstr(status) ));
113 if (!NT_STATUS_IS_OK(status)) {
114 if (DEBUGLEVEL >= 10) {
115 DEBUG(10,("smbd_check_open_rights: acl for %s is:\n",
116 smb_fname_str_dbg(smb_fname) ));
117 NDR_PRINT_DEBUG(security_descriptor, sd);
121 TALLOC_FREE(sd);
123 return status;
126 /****************************************************************************
127 fd support routines - attempt to do a dos_open.
128 ****************************************************************************/
130 static NTSTATUS fd_open(struct connection_struct *conn,
131 files_struct *fsp,
132 int flags,
133 mode_t mode)
135 struct smb_filename *smb_fname = fsp->fsp_name;
136 NTSTATUS status = NT_STATUS_OK;
138 #ifdef O_NOFOLLOW
140 * Never follow symlinks on a POSIX client. The
141 * client should be doing this.
144 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
145 flags |= O_NOFOLLOW;
147 #endif
149 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
150 if (fsp->fh->fd == -1) {
151 status = map_nt_error_from_unix(errno);
152 if (errno == EMFILE) {
153 static time_t last_warned = 0L;
155 if (time((time_t *) NULL) > last_warned) {
156 DEBUG(0,("Too many open files, unable "
157 "to open more! smbd's max "
158 "open files = %d\n",
159 lp_max_open_files()));
160 last_warned = time((time_t *) NULL);
166 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
167 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
168 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
170 return status;
173 /****************************************************************************
174 Close the file associated with a fsp.
175 ****************************************************************************/
177 NTSTATUS fd_close(files_struct *fsp)
179 int ret;
181 if (fsp->fh->fd == -1) {
182 return NT_STATUS_OK; /* What we used to call a stat open. */
184 if (fsp->fh->ref_count > 1) {
185 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
188 ret = SMB_VFS_CLOSE(fsp);
189 fsp->fh->fd = -1;
190 if (ret == -1) {
191 return map_nt_error_from_unix(errno);
193 return NT_STATUS_OK;
196 /****************************************************************************
197 Change the ownership of a file to that of the parent directory.
198 Do this by fd if possible.
199 ****************************************************************************/
201 void change_file_owner_to_parent(connection_struct *conn,
202 const char *inherit_from_dir,
203 files_struct *fsp)
205 struct smb_filename *smb_fname_parent = NULL;
206 NTSTATUS status;
207 int ret;
209 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
210 NULL, NULL, &smb_fname_parent);
211 if (!NT_STATUS_IS_OK(status)) {
212 return;
215 ret = SMB_VFS_STAT(conn, smb_fname_parent);
216 if (ret == -1) {
217 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
218 "directory %s. Error was %s\n",
219 smb_fname_str_dbg(smb_fname_parent),
220 strerror(errno)));
221 return;
224 become_root();
225 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
226 unbecome_root();
227 if (ret == -1) {
228 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
229 "file %s to parent directory uid %u. Error "
230 "was %s\n", fsp_str_dbg(fsp),
231 (unsigned int)smb_fname_parent->st.st_ex_uid,
232 strerror(errno) ));
235 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
236 "parent directory uid %u.\n", fsp_str_dbg(fsp),
237 (unsigned int)smb_fname_parent->st.st_ex_uid));
239 TALLOC_FREE(smb_fname_parent);
242 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
243 const char *inherit_from_dir,
244 const char *fname,
245 SMB_STRUCT_STAT *psbuf)
247 struct smb_filename *smb_fname_parent = NULL;
248 struct smb_filename *smb_fname_cwd = NULL;
249 char *saved_dir = NULL;
250 TALLOC_CTX *ctx = talloc_tos();
251 NTSTATUS status = NT_STATUS_OK;
252 int ret;
254 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
255 &smb_fname_parent);
256 if (!NT_STATUS_IS_OK(status)) {
257 return status;
260 ret = SMB_VFS_STAT(conn, smb_fname_parent);
261 if (ret == -1) {
262 status = map_nt_error_from_unix(errno);
263 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
264 "directory %s. Error was %s\n",
265 smb_fname_str_dbg(smb_fname_parent),
266 strerror(errno)));
267 goto out;
270 /* We've already done an lstat into psbuf, and we know it's a
271 directory. If we can cd into the directory and the dev/ino
272 are the same then we can safely chown without races as
273 we're locking the directory in place by being in it. This
274 should work on any UNIX (thanks tridge :-). JRA.
277 saved_dir = vfs_GetWd(ctx,conn);
278 if (!saved_dir) {
279 status = map_nt_error_from_unix(errno);
280 DEBUG(0,("change_dir_owner_to_parent: failed to get "
281 "current working directory. Error was %s\n",
282 strerror(errno)));
283 goto out;
286 /* Chdir into the new path. */
287 if (vfs_ChDir(conn, fname) == -1) {
288 status = map_nt_error_from_unix(errno);
289 DEBUG(0,("change_dir_owner_to_parent: failed to change "
290 "current working directory to %s. Error "
291 "was %s\n", fname, strerror(errno) ));
292 goto chdir;
295 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
296 &smb_fname_cwd);
297 if (!NT_STATUS_IS_OK(status)) {
298 return status;
301 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
302 if (ret == -1) {
303 status = map_nt_error_from_unix(errno);
304 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
305 "directory '.' (%s) Error was %s\n",
306 fname, strerror(errno)));
307 goto chdir;
310 /* Ensure we're pointing at the same place. */
311 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
312 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino ||
313 smb_fname_cwd->st.st_ex_mode != psbuf->st_ex_mode ) {
314 DEBUG(0,("change_dir_owner_to_parent: "
315 "device/inode/mode on directory %s changed. "
316 "Refusing to chown !\n", fname ));
317 status = NT_STATUS_ACCESS_DENIED;
318 goto chdir;
321 become_root();
322 ret = SMB_VFS_CHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
323 (gid_t)-1);
324 unbecome_root();
325 if (ret == -1) {
326 status = map_nt_error_from_unix(errno);
327 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
328 "directory %s to parent directory uid %u. "
329 "Error was %s\n", fname,
330 (unsigned int)smb_fname_parent->st.st_ex_uid,
331 strerror(errno) ));
332 goto chdir;
335 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
336 "directory %s to parent directory uid %u.\n",
337 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
339 chdir:
340 vfs_ChDir(conn,saved_dir);
341 out:
342 TALLOC_FREE(smb_fname_parent);
343 TALLOC_FREE(smb_fname_cwd);
344 return status;
347 /****************************************************************************
348 Open a file.
349 ****************************************************************************/
351 static NTSTATUS open_file(files_struct *fsp,
352 connection_struct *conn,
353 struct smb_request *req,
354 const char *parent_dir,
355 int flags,
356 mode_t unx_mode,
357 uint32 access_mask, /* client requested access mask. */
358 uint32 open_access_mask) /* what we're actually using in the open. */
360 struct smb_filename *smb_fname = fsp->fsp_name;
361 NTSTATUS status = NT_STATUS_OK;
362 int accmode = (flags & O_ACCMODE);
363 int local_flags = flags;
364 bool file_existed = VALID_STAT(fsp->fsp_name->st);
366 fsp->fh->fd = -1;
367 errno = EPERM;
369 /* Check permissions */
372 * This code was changed after seeing a client open request
373 * containing the open mode of (DENY_WRITE/read-only) with
374 * the 'create if not exist' bit set. The previous code
375 * would fail to open the file read only on a read-only share
376 * as it was checking the flags parameter directly against O_RDONLY,
377 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
378 * JRA.
381 if (!CAN_WRITE(conn)) {
382 /* It's a read-only share - fail if we wanted to write. */
383 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
384 DEBUG(3,("Permission denied opening %s\n",
385 smb_fname_str_dbg(smb_fname)));
386 return NT_STATUS_ACCESS_DENIED;
387 } else if(flags & O_CREAT) {
388 /* We don't want to write - but we must make sure that
389 O_CREAT doesn't create the file if we have write
390 access into the directory.
392 flags &= ~(O_CREAT|O_EXCL);
393 local_flags &= ~(O_CREAT|O_EXCL);
398 * This little piece of insanity is inspired by the
399 * fact that an NT client can open a file for O_RDONLY,
400 * but set the create disposition to FILE_EXISTS_TRUNCATE.
401 * If the client *can* write to the file, then it expects to
402 * truncate the file, even though it is opening for readonly.
403 * Quicken uses this stupid trick in backup file creation...
404 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
405 * for helping track this one down. It didn't bite us in 2.0.x
406 * as we always opened files read-write in that release. JRA.
409 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
410 DEBUG(10,("open_file: truncate requested on read-only open "
411 "for file %s\n", smb_fname_str_dbg(smb_fname)));
412 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
415 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
416 (!file_existed && (local_flags & O_CREAT)) ||
417 ((local_flags & O_TRUNC) == O_TRUNC) ) {
418 const char *wild;
421 * We can't actually truncate here as the file may be locked.
422 * open_file_ntcreate will take care of the truncate later. JRA.
425 local_flags &= ~O_TRUNC;
427 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
429 * We would block on opening a FIFO with no one else on the
430 * other end. Do what we used to do and add O_NONBLOCK to the
431 * open flags. JRA.
434 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
435 local_flags |= O_NONBLOCK;
437 #endif
439 /* Don't create files with Microsoft wildcard characters. */
440 if (fsp->base_fsp) {
442 * wildcard characters are allowed in stream names
443 * only test the basefilename
445 wild = fsp->base_fsp->fsp_name->base_name;
446 } else {
447 wild = smb_fname->base_name;
449 if ((local_flags & O_CREAT) && !file_existed &&
450 ms_has_wild(wild)) {
451 return NT_STATUS_OBJECT_NAME_INVALID;
454 /* Actually do the open */
455 status = fd_open(conn, fsp, local_flags, unx_mode);
456 if (!NT_STATUS_IS_OK(status)) {
457 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
458 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
459 nt_errstr(status),local_flags,flags));
460 return status;
463 if ((local_flags & O_CREAT) && !file_existed) {
465 /* Inherit the ACL if required */
466 if (lp_inherit_perms(SNUM(conn))) {
467 inherit_access_posix_acl(conn, parent_dir,
468 smb_fname->base_name,
469 unx_mode);
472 /* Change the owner if required. */
473 if (lp_inherit_owner(SNUM(conn))) {
474 change_file_owner_to_parent(conn, parent_dir,
475 fsp);
478 notify_fname(conn, NOTIFY_ACTION_ADDED,
479 FILE_NOTIFY_CHANGE_FILE_NAME,
480 smb_fname->base_name);
483 } else {
484 fsp->fh->fd = -1; /* What we used to call a stat open. */
485 if (file_existed) {
486 uint32_t access_granted = 0;
488 status = smbd_check_open_rights(conn,
489 smb_fname,
490 access_mask,
491 &access_granted);
492 if (!NT_STATUS_IS_OK(status)) {
493 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
495 * On NT_STATUS_ACCESS_DENIED, access_granted
496 * contains the denied bits.
499 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
500 (access_granted & FILE_WRITE_ATTRIBUTES) &&
501 (lp_map_readonly(SNUM(conn)) ||
502 lp_map_archive(SNUM(conn)) ||
503 lp_map_hidden(SNUM(conn)) ||
504 lp_map_system(SNUM(conn)))) {
505 access_granted &= ~FILE_WRITE_ATTRIBUTES;
507 DEBUG(10,("open_file: "
508 "overrode "
509 "FILE_WRITE_"
510 "ATTRIBUTES "
511 "on file %s\n",
512 smb_fname_str_dbg(
513 smb_fname)));
516 if ((access_mask & DELETE_ACCESS) &&
517 (access_granted & DELETE_ACCESS) &&
518 can_delete_file_in_directory(conn,
519 smb_fname)) {
520 /* Were we trying to do a stat open
521 * for delete and didn't get DELETE
522 * access (only) ? Check if the
523 * directory allows DELETE_CHILD.
524 * See here:
525 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
526 * for details. */
528 access_granted &= ~DELETE_ACCESS;
530 DEBUG(10,("open_file: "
531 "overrode "
532 "DELETE_ACCESS on "
533 "file %s\n",
534 smb_fname_str_dbg(
535 smb_fname)));
538 if (access_granted != 0) {
539 DEBUG(10,("open_file: Access "
540 "denied on file "
541 "%s\n",
542 smb_fname_str_dbg(
543 smb_fname)));
544 return status;
546 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
547 fsp->posix_open &&
548 S_ISLNK(smb_fname->st.st_ex_mode)) {
549 /* This is a POSIX stat open for delete
550 * or rename on a symlink that points
551 * nowhere. Allow. */
552 DEBUG(10,("open_file: allowing POSIX "
553 "open on bad symlink %s\n",
554 smb_fname_str_dbg(
555 smb_fname)));
556 } else {
557 DEBUG(10,("open_file: "
558 "smbd_check_open_rights on file "
559 "%s returned %s\n",
560 smb_fname_str_dbg(smb_fname),
561 nt_errstr(status) ));
562 return status;
568 if (!file_existed) {
569 int ret;
571 if (fsp->fh->fd == -1) {
572 ret = SMB_VFS_STAT(conn, smb_fname);
573 } else {
574 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
575 /* If we have an fd, this stat should succeed. */
576 if (ret == -1) {
577 DEBUG(0,("Error doing fstat on open file %s "
578 "(%s)\n",
579 smb_fname_str_dbg(smb_fname),
580 strerror(errno) ));
584 /* For a non-io open, this stat failing means file not found. JRA */
585 if (ret == -1) {
586 status = map_nt_error_from_unix(errno);
587 fd_close(fsp);
588 return status;
593 * POSIX allows read-only opens of directories. We don't
594 * want to do this (we use a different code path for this)
595 * so catch a directory open and return an EISDIR. JRA.
598 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
599 fd_close(fsp);
600 errno = EISDIR;
601 return NT_STATUS_FILE_IS_A_DIRECTORY;
604 fsp->mode = smb_fname->st.st_ex_mode;
605 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
606 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
607 fsp->file_pid = req ? req->smbpid : 0;
608 fsp->can_lock = True;
609 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
610 if (!CAN_WRITE(conn)) {
611 fsp->can_write = False;
612 } else {
613 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
614 True : False;
616 fsp->print_file = False;
617 fsp->modified = False;
618 fsp->sent_oplock_break = NO_BREAK_SENT;
619 fsp->is_directory = False;
620 if (conn->aio_write_behind_list &&
621 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
622 conn->case_sensitive)) {
623 fsp->aio_write_behind = True;
626 fsp->wcp = NULL; /* Write cache pointer. */
628 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
629 conn->server_info->unix_name,
630 smb_fname_str_dbg(smb_fname),
631 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
632 conn->num_files_open));
634 errno = 0;
635 return NT_STATUS_OK;
638 /*******************************************************************
639 Return True if the filename is one of the special executable types.
640 ********************************************************************/
642 bool is_executable(const char *fname)
644 if ((fname = strrchr_m(fname,'.'))) {
645 if (strequal(fname,".com") ||
646 strequal(fname,".dll") ||
647 strequal(fname,".exe") ||
648 strequal(fname,".sym")) {
649 return True;
652 return False;
655 /****************************************************************************
656 Check if we can open a file with a share mode.
657 Returns True if conflict, False if not.
658 ****************************************************************************/
660 static bool share_conflict(struct share_mode_entry *entry,
661 uint32 access_mask,
662 uint32 share_access)
664 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
665 "entry->share_access = 0x%x, "
666 "entry->private_options = 0x%x\n",
667 (unsigned int)entry->access_mask,
668 (unsigned int)entry->share_access,
669 (unsigned int)entry->private_options));
671 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
672 (unsigned int)access_mask, (unsigned int)share_access));
674 if ((entry->access_mask & (FILE_WRITE_DATA|
675 FILE_APPEND_DATA|
676 FILE_READ_DATA|
677 FILE_EXECUTE|
678 DELETE_ACCESS)) == 0) {
679 DEBUG(10,("share_conflict: No conflict due to "
680 "entry->access_mask = 0x%x\n",
681 (unsigned int)entry->access_mask ));
682 return False;
685 if ((access_mask & (FILE_WRITE_DATA|
686 FILE_APPEND_DATA|
687 FILE_READ_DATA|
688 FILE_EXECUTE|
689 DELETE_ACCESS)) == 0) {
690 DEBUG(10,("share_conflict: No conflict due to "
691 "access_mask = 0x%x\n",
692 (unsigned int)access_mask ));
693 return False;
696 #if 1 /* JRA TEST - Superdebug. */
697 #define CHECK_MASK(num, am, right, sa, share) \
698 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
699 (unsigned int)(num), (unsigned int)(am), \
700 (unsigned int)(right), (unsigned int)(am)&(right) )); \
701 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
702 (unsigned int)(num), (unsigned int)(sa), \
703 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
704 if (((am) & (right)) && !((sa) & (share))) { \
705 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
706 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
707 (unsigned int)(share) )); \
708 return True; \
710 #else
711 #define CHECK_MASK(num, am, right, sa, share) \
712 if (((am) & (right)) && !((sa) & (share))) { \
713 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
714 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
715 (unsigned int)(share) )); \
716 return True; \
718 #endif
720 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
721 share_access, FILE_SHARE_WRITE);
722 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
723 entry->share_access, FILE_SHARE_WRITE);
725 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
726 share_access, FILE_SHARE_READ);
727 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
728 entry->share_access, FILE_SHARE_READ);
730 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
731 share_access, FILE_SHARE_DELETE);
732 CHECK_MASK(6, access_mask, DELETE_ACCESS,
733 entry->share_access, FILE_SHARE_DELETE);
735 DEBUG(10,("share_conflict: No conflict.\n"));
736 return False;
739 #if defined(DEVELOPER)
740 static void validate_my_share_entries(int num,
741 struct share_mode_entry *share_entry)
743 files_struct *fsp;
745 if (!procid_is_me(&share_entry->pid)) {
746 return;
749 if (is_deferred_open_entry(share_entry) &&
750 !open_was_deferred(share_entry->op_mid)) {
751 char *str = talloc_asprintf(talloc_tos(),
752 "Got a deferred entry without a request: "
753 "PANIC: %s\n",
754 share_mode_str(talloc_tos(), num, share_entry));
755 smb_panic(str);
758 if (!is_valid_share_mode_entry(share_entry)) {
759 return;
762 fsp = file_find_dif(share_entry->id,
763 share_entry->share_file_id);
764 if (!fsp) {
765 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
766 share_mode_str(talloc_tos(), num, share_entry) ));
767 smb_panic("validate_my_share_entries: Cannot match a "
768 "share entry with an open file\n");
771 if (is_deferred_open_entry(share_entry) ||
772 is_unused_share_mode_entry(share_entry)) {
773 goto panic;
776 if ((share_entry->op_type == NO_OPLOCK) &&
777 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
778 /* Someone has already written to it, but I haven't yet
779 * noticed */
780 return;
783 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
784 goto panic;
787 return;
789 panic:
791 char *str;
792 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
793 share_mode_str(talloc_tos(), num, share_entry) ));
794 str = talloc_asprintf(talloc_tos(),
795 "validate_my_share_entries: "
796 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
797 fsp->fsp_name->base_name,
798 (unsigned int)fsp->oplock_type,
799 (unsigned int)share_entry->op_type );
800 smb_panic(str);
803 #endif
805 bool is_stat_open(uint32 access_mask)
807 return (access_mask &&
808 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
809 FILE_WRITE_ATTRIBUTES))==0) &&
810 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
811 FILE_WRITE_ATTRIBUTES)) != 0));
814 /****************************************************************************
815 Deal with share modes
816 Invarient: Share mode must be locked on entry and exit.
817 Returns -1 on error, or number of share modes on success (may be zero).
818 ****************************************************************************/
820 static NTSTATUS open_mode_check(connection_struct *conn,
821 struct share_mode_lock *lck,
822 uint32 access_mask,
823 uint32 share_access,
824 uint32 create_options,
825 bool *file_existed)
827 int i;
829 if(lck->num_share_modes == 0) {
830 return NT_STATUS_OK;
833 *file_existed = True;
835 /* A delete on close prohibits everything */
837 if (lck->delete_on_close) {
838 return NT_STATUS_DELETE_PENDING;
841 if (is_stat_open(access_mask)) {
842 /* Stat open that doesn't trigger oplock breaks or share mode
843 * checks... ! JRA. */
844 return NT_STATUS_OK;
848 * Check if the share modes will give us access.
851 #if defined(DEVELOPER)
852 for(i = 0; i < lck->num_share_modes; i++) {
853 validate_my_share_entries(i, &lck->share_modes[i]);
855 #endif
857 if (!lp_share_modes(SNUM(conn))) {
858 return NT_STATUS_OK;
861 /* Now we check the share modes, after any oplock breaks. */
862 for(i = 0; i < lck->num_share_modes; i++) {
864 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
865 continue;
868 /* someone else has a share lock on it, check to see if we can
869 * too */
870 if (share_conflict(&lck->share_modes[i],
871 access_mask, share_access)) {
872 return NT_STATUS_SHARING_VIOLATION;
876 return NT_STATUS_OK;
879 static bool is_delete_request(files_struct *fsp) {
880 return ((fsp->access_mask == DELETE_ACCESS) &&
881 (fsp->oplock_type == NO_OPLOCK));
885 * Send a break message to the oplock holder and delay the open for
886 * our client.
889 static NTSTATUS send_break_message(files_struct *fsp,
890 struct share_mode_entry *exclusive,
891 uint16 mid,
892 int oplock_request)
894 NTSTATUS status;
895 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
897 DEBUG(10, ("Sending break request to PID %s\n",
898 procid_str_static(&exclusive->pid)));
899 exclusive->op_mid = mid;
901 /* Create the message. */
902 share_mode_entry_to_message(msg, exclusive);
904 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
905 don't want this set in the share mode struct pointed to by lck. */
907 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
908 SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
911 status = messaging_send_buf(smbd_messaging_context(), exclusive->pid,
912 MSG_SMB_BREAK_REQUEST,
913 (uint8 *)msg,
914 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
915 if (!NT_STATUS_IS_OK(status)) {
916 DEBUG(3, ("Could not send oplock break message: %s\n",
917 nt_errstr(status)));
920 return status;
924 * 1) No files open at all or internal open: Grant whatever the client wants.
926 * 2) Exclusive (or batch) oplock around: If the requested access is a delete
927 * request, break if the oplock around is a batch oplock. If it's another
928 * requested access type, break.
930 * 3) Only level2 around: Grant level2 and do nothing else.
933 static bool delay_for_oplocks(struct share_mode_lock *lck,
934 files_struct *fsp,
935 uint16 mid,
936 int pass_number,
937 int oplock_request)
939 int i;
940 struct share_mode_entry *exclusive = NULL;
941 bool valid_entry = false;
942 bool have_level2 = false;
943 bool have_a_none_oplock = false;
944 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
945 lp_level2_oplocks(SNUM(fsp->conn));
947 if (oplock_request & INTERNAL_OPEN_ONLY) {
948 fsp->oplock_type = NO_OPLOCK;
951 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
952 return false;
955 for (i=0; i<lck->num_share_modes; i++) {
957 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
958 continue;
961 /* At least one entry is not an invalid or deferred entry. */
962 valid_entry = true;
964 if (pass_number == 1) {
965 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
966 SMB_ASSERT(exclusive == NULL);
967 exclusive = &lck->share_modes[i];
969 } else {
970 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
971 SMB_ASSERT(exclusive == NULL);
972 exclusive = &lck->share_modes[i];
976 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
977 SMB_ASSERT(exclusive == NULL);
978 have_level2 = true;
981 if (lck->share_modes[i].op_type == NO_OPLOCK) {
982 have_a_none_oplock = true;
986 if (exclusive != NULL) { /* Found an exclusive oplock */
987 bool delay_it = is_delete_request(fsp) ?
988 BATCH_OPLOCK_TYPE(exclusive->op_type) : true;
989 SMB_ASSERT(!have_level2);
990 if (delay_it) {
991 send_break_message(fsp, exclusive, mid, oplock_request);
992 return true;
997 * Match what was requested (fsp->oplock_type) with
998 * what was found in the existing share modes.
1001 if (!valid_entry) {
1002 /* All entries are placeholders or deferred.
1003 * Directly grant whatever the client wants. */
1004 if (fsp->oplock_type == NO_OPLOCK) {
1005 /* Store a level2 oplock, but don't tell the client */
1006 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1008 } else if (have_a_none_oplock) {
1009 fsp->oplock_type = NO_OPLOCK;
1010 } else if (have_level2) {
1011 if (fsp->oplock_type == NO_OPLOCK ||
1012 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1013 /* Store a level2 oplock, but don't tell the client */
1014 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1015 } else {
1016 fsp->oplock_type = LEVEL_II_OPLOCK;
1018 } else {
1019 /* This case can never happen. */
1020 SMB_ASSERT(1);
1024 * Don't grant level2 to clients that don't want them
1025 * or if we've turned them off.
1027 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1028 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1031 DEBUG(10,("delay_for_oplocks: oplock type 0x%x on file %s\n",
1032 fsp->oplock_type, fsp_str_dbg(fsp)));
1034 /* No delay. */
1035 return false;
1038 bool request_timed_out(struct timeval request_time,
1039 struct timeval timeout)
1041 struct timeval now, end_time;
1042 GetTimeOfDay(&now);
1043 end_time = timeval_sum(&request_time, &timeout);
1044 return (timeval_compare(&end_time, &now) < 0);
1047 /****************************************************************************
1048 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1049 ****************************************************************************/
1051 static void defer_open(struct share_mode_lock *lck,
1052 struct timeval request_time,
1053 struct timeval timeout,
1054 struct smb_request *req,
1055 struct deferred_open_record *state)
1057 int i;
1059 /* Paranoia check */
1061 for (i=0; i<lck->num_share_modes; i++) {
1062 struct share_mode_entry *e = &lck->share_modes[i];
1064 if (!is_deferred_open_entry(e)) {
1065 continue;
1068 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
1069 DEBUG(0, ("Trying to defer an already deferred "
1070 "request: mid=%d, exiting\n", req->mid));
1071 exit_server("attempt to defer a deferred request");
1075 /* End paranoia check */
1077 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1078 "open entry for mid %u\n",
1079 (unsigned int)request_time.tv_sec,
1080 (unsigned int)request_time.tv_usec,
1081 (unsigned int)req->mid));
1083 if (!push_deferred_smb_message(req, request_time, timeout,
1084 (char *)state, sizeof(*state))) {
1085 exit_server("push_deferred_smb_message failed");
1087 add_deferred_open(lck, req->mid, request_time, state->id);
1091 /****************************************************************************
1092 On overwrite open ensure that the attributes match.
1093 ****************************************************************************/
1095 bool open_match_attributes(connection_struct *conn,
1096 uint32 old_dos_attr,
1097 uint32 new_dos_attr,
1098 mode_t existing_unx_mode,
1099 mode_t new_unx_mode,
1100 mode_t *returned_unx_mode)
1102 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1104 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1105 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1107 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1108 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1109 *returned_unx_mode = new_unx_mode;
1110 } else {
1111 *returned_unx_mode = (mode_t)0;
1114 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1115 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1116 "returned_unx_mode = 0%o\n",
1117 (unsigned int)old_dos_attr,
1118 (unsigned int)existing_unx_mode,
1119 (unsigned int)new_dos_attr,
1120 (unsigned int)*returned_unx_mode ));
1122 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1123 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1124 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1125 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1126 return False;
1129 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1130 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1131 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1132 return False;
1135 return True;
1138 /****************************************************************************
1139 Special FCB or DOS processing in the case of a sharing violation.
1140 Try and find a duplicated file handle.
1141 ****************************************************************************/
1143 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1144 connection_struct *conn,
1145 files_struct *fsp_to_dup_into,
1146 const struct smb_filename *smb_fname,
1147 struct file_id id,
1148 uint16 file_pid,
1149 uint16 vuid,
1150 uint32 access_mask,
1151 uint32 share_access,
1152 uint32 create_options)
1154 files_struct *fsp;
1156 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1157 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1159 for(fsp = file_find_di_first(id); fsp;
1160 fsp = file_find_di_next(fsp)) {
1162 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1163 "vuid = %u, file_pid = %u, private_options = 0x%x "
1164 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1165 fsp->fh->fd, (unsigned int)fsp->vuid,
1166 (unsigned int)fsp->file_pid,
1167 (unsigned int)fsp->fh->private_options,
1168 (unsigned int)fsp->access_mask ));
1170 if (fsp->fh->fd != -1 &&
1171 fsp->vuid == vuid &&
1172 fsp->file_pid == file_pid &&
1173 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1174 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1175 (fsp->access_mask & FILE_WRITE_DATA) &&
1176 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1177 strequal(fsp->fsp_name->stream_name,
1178 smb_fname->stream_name)) {
1179 DEBUG(10,("fcb_or_dos_open: file match\n"));
1180 break;
1184 if (!fsp) {
1185 return NT_STATUS_NOT_FOUND;
1188 /* quite an insane set of semantics ... */
1189 if (is_executable(smb_fname->base_name) &&
1190 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1191 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1192 return NT_STATUS_INVALID_PARAMETER;
1195 /* We need to duplicate this fsp. */
1196 return dup_file_fsp(req, fsp, access_mask, share_access,
1197 create_options, fsp_to_dup_into);
1200 /****************************************************************************
1201 Open a file with a share mode - old openX method - map into NTCreate.
1202 ****************************************************************************/
1204 bool map_open_params_to_ntcreate(const struct smb_filename *smb_fname,
1205 int deny_mode, int open_func,
1206 uint32 *paccess_mask,
1207 uint32 *pshare_mode,
1208 uint32 *pcreate_disposition,
1209 uint32 *pcreate_options,
1210 uint32_t *pprivate_flags)
1212 uint32 access_mask;
1213 uint32 share_mode;
1214 uint32 create_disposition;
1215 uint32 create_options = FILE_NON_DIRECTORY_FILE;
1216 uint32_t private_flags = 0;
1218 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1219 "open_func = 0x%x\n",
1220 smb_fname_str_dbg(smb_fname), (unsigned int)deny_mode,
1221 (unsigned int)open_func ));
1223 /* Create the NT compatible access_mask. */
1224 switch (GET_OPENX_MODE(deny_mode)) {
1225 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1226 case DOS_OPEN_RDONLY:
1227 access_mask = FILE_GENERIC_READ;
1228 break;
1229 case DOS_OPEN_WRONLY:
1230 access_mask = FILE_GENERIC_WRITE;
1231 break;
1232 case DOS_OPEN_RDWR:
1233 case DOS_OPEN_FCB:
1234 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1235 break;
1236 default:
1237 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1238 (unsigned int)GET_OPENX_MODE(deny_mode)));
1239 return False;
1242 /* Create the NT compatible create_disposition. */
1243 switch (open_func) {
1244 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1245 create_disposition = FILE_CREATE;
1246 break;
1248 case OPENX_FILE_EXISTS_OPEN:
1249 create_disposition = FILE_OPEN;
1250 break;
1252 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1253 create_disposition = FILE_OPEN_IF;
1254 break;
1256 case OPENX_FILE_EXISTS_TRUNCATE:
1257 create_disposition = FILE_OVERWRITE;
1258 break;
1260 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1261 create_disposition = FILE_OVERWRITE_IF;
1262 break;
1264 default:
1265 /* From samba4 - to be confirmed. */
1266 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1267 create_disposition = FILE_CREATE;
1268 break;
1270 DEBUG(10,("map_open_params_to_ntcreate: bad "
1271 "open_func 0x%x\n", (unsigned int)open_func));
1272 return False;
1275 /* Create the NT compatible share modes. */
1276 switch (GET_DENY_MODE(deny_mode)) {
1277 case DENY_ALL:
1278 share_mode = FILE_SHARE_NONE;
1279 break;
1281 case DENY_WRITE:
1282 share_mode = FILE_SHARE_READ;
1283 break;
1285 case DENY_READ:
1286 share_mode = FILE_SHARE_WRITE;
1287 break;
1289 case DENY_NONE:
1290 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1291 break;
1293 case DENY_DOS:
1294 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1295 if (is_executable(smb_fname->base_name)) {
1296 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1297 } else {
1298 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1299 share_mode = FILE_SHARE_READ;
1300 } else {
1301 share_mode = FILE_SHARE_NONE;
1304 break;
1306 case DENY_FCB:
1307 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1308 share_mode = FILE_SHARE_NONE;
1309 break;
1311 default:
1312 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1313 (unsigned int)GET_DENY_MODE(deny_mode) ));
1314 return False;
1317 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1318 "share_mode = 0x%x, create_disposition = 0x%x, "
1319 "create_options = 0x%x private_flags = 0x%x\n",
1320 smb_fname_str_dbg(smb_fname),
1321 (unsigned int)access_mask,
1322 (unsigned int)share_mode,
1323 (unsigned int)create_disposition,
1324 (unsigned int)create_options,
1325 (unsigned int)private_flags));
1327 if (paccess_mask) {
1328 *paccess_mask = access_mask;
1330 if (pshare_mode) {
1331 *pshare_mode = share_mode;
1333 if (pcreate_disposition) {
1334 *pcreate_disposition = create_disposition;
1336 if (pcreate_options) {
1337 *pcreate_options = create_options;
1339 if (pprivate_flags) {
1340 *pprivate_flags = private_flags;
1343 return True;
1347 static void schedule_defer_open(struct share_mode_lock *lck,
1348 struct timeval request_time,
1349 struct smb_request *req)
1351 struct deferred_open_record state;
1353 /* This is a relative time, added to the absolute
1354 request_time value to get the absolute timeout time.
1355 Note that if this is the second or greater time we enter
1356 this codepath for this particular request mid then
1357 request_time is left as the absolute time of the *first*
1358 time this request mid was processed. This is what allows
1359 the request to eventually time out. */
1361 struct timeval timeout;
1363 /* Normally the smbd we asked should respond within
1364 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1365 * the client did, give twice the timeout as a safety
1366 * measure here in case the other smbd is stuck
1367 * somewhere else. */
1369 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1371 /* Nothing actually uses state.delayed_for_oplocks
1372 but it's handy to differentiate in debug messages
1373 between a 30 second delay due to oplock break, and
1374 a 1 second delay for share mode conflicts. */
1376 state.delayed_for_oplocks = True;
1377 state.id = lck->id;
1379 if (!request_timed_out(request_time, timeout)) {
1380 defer_open(lck, request_time, timeout, req, &state);
1384 /****************************************************************************
1385 Work out what access_mask to use from what the client sent us.
1386 ****************************************************************************/
1388 static NTSTATUS calculate_access_mask(connection_struct *conn,
1389 const struct smb_filename *smb_fname,
1390 bool file_existed,
1391 uint32_t access_mask,
1392 uint32_t *access_mask_out)
1394 NTSTATUS status;
1397 * Convert GENERIC bits to specific bits.
1400 se_map_generic(&access_mask, &file_generic_mapping);
1402 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1403 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1404 if (file_existed) {
1406 struct security_descriptor *sd;
1407 uint32_t access_granted = 0;
1409 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1410 (OWNER_SECURITY_INFORMATION |
1411 GROUP_SECURITY_INFORMATION |
1412 DACL_SECURITY_INFORMATION),&sd);
1414 if (!NT_STATUS_IS_OK(status)) {
1415 DEBUG(10, ("calculate_access_mask: Could not get acl "
1416 "on file %s: %s\n",
1417 smb_fname_str_dbg(smb_fname),
1418 nt_errstr(status)));
1419 return NT_STATUS_ACCESS_DENIED;
1422 status = smb1_file_se_access_check(sd,
1423 conn->server_info->ptok,
1424 access_mask,
1425 &access_granted);
1427 TALLOC_FREE(sd);
1429 if (!NT_STATUS_IS_OK(status)) {
1430 DEBUG(10, ("calculate_access_mask: Access denied on "
1431 "file %s: when calculating maximum access\n",
1432 smb_fname_str_dbg(smb_fname)));
1433 return NT_STATUS_ACCESS_DENIED;
1436 access_mask = access_granted;
1437 } else {
1438 access_mask = FILE_GENERIC_ALL;
1442 *access_mask_out = access_mask;
1443 return NT_STATUS_OK;
1446 /****************************************************************************
1447 Open a file with a share mode. Passed in an already created files_struct *.
1448 ****************************************************************************/
1450 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1451 struct smb_request *req,
1452 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1453 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1454 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1455 uint32 create_options, /* options such as delete on close. */
1456 uint32 new_dos_attributes, /* attributes used for new file. */
1457 int oplock_request, /* internal Samba oplock codes. */
1458 /* Information (FILE_EXISTS etc.) */
1459 uint32_t private_flags, /* Samba specific flags. */
1460 int *pinfo,
1461 files_struct *fsp)
1463 struct smb_filename *smb_fname = fsp->fsp_name;
1464 int flags=0;
1465 int flags2=0;
1466 bool file_existed = VALID_STAT(smb_fname->st);
1467 bool def_acl = False;
1468 bool posix_open = False;
1469 bool new_file_created = False;
1470 bool clear_ads = false;
1471 struct file_id id;
1472 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1473 mode_t new_unx_mode = (mode_t)0;
1474 mode_t unx_mode = (mode_t)0;
1475 int info;
1476 uint32 existing_dos_attributes = 0;
1477 struct pending_message_list *pml = NULL;
1478 struct timeval request_time = timeval_zero();
1479 struct share_mode_lock *lck = NULL;
1480 uint32 open_access_mask = access_mask;
1481 NTSTATUS status;
1482 char *parent_dir;
1484 ZERO_STRUCT(id);
1486 if (conn->printer) {
1488 * Printers are handled completely differently.
1489 * Most of the passed parameters are ignored.
1492 if (pinfo) {
1493 *pinfo = FILE_WAS_CREATED;
1496 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1497 smb_fname_str_dbg(smb_fname)));
1499 if (!req) {
1500 DEBUG(0,("open_file_ntcreate: printer open without "
1501 "an SMB request!\n"));
1502 return NT_STATUS_INTERNAL_ERROR;
1505 return print_fsp_open(req, conn, smb_fname->base_name,
1506 req->vuid, fsp);
1509 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1510 NULL)) {
1511 return NT_STATUS_NO_MEMORY;
1514 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1515 posix_open = True;
1516 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1517 new_dos_attributes = 0;
1518 } else {
1519 /* We add aARCH to this as this mode is only used if the file is
1520 * created new. */
1521 unx_mode = unix_mode(conn, new_dos_attributes | aARCH,
1522 smb_fname, parent_dir);
1525 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1526 "access_mask=0x%x share_access=0x%x "
1527 "create_disposition = 0x%x create_options=0x%x "
1528 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1529 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1530 access_mask, share_access, create_disposition,
1531 create_options, (unsigned int)unx_mode, oplock_request,
1532 (unsigned int)private_flags));
1534 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1535 DEBUG(0, ("No smb request but not an internal only open!\n"));
1536 return NT_STATUS_INTERNAL_ERROR;
1540 * Only non-internal opens can be deferred at all
1543 if ((req != NULL)
1544 && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
1545 struct deferred_open_record *state =
1546 (struct deferred_open_record *)pml->private_data.data;
1548 /* Remember the absolute time of the original
1549 request with this mid. We'll use it later to
1550 see if this has timed out. */
1552 request_time = pml->request_time;
1554 /* Remove the deferred open entry under lock. */
1555 lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
1556 NULL);
1557 if (lck == NULL) {
1558 DEBUG(0, ("could not get share mode lock\n"));
1559 } else {
1560 del_deferred_open_entry(lck, req->mid);
1561 TALLOC_FREE(lck);
1564 /* Ensure we don't reprocess this message. */
1565 remove_deferred_open_smb_message(req->mid);
1568 status = check_name(conn, smb_fname->base_name);
1569 if (!NT_STATUS_IS_OK(status)) {
1570 return status;
1573 if (!posix_open) {
1574 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1575 if (file_existed) {
1576 existing_dos_attributes = dos_mode(conn, smb_fname);
1580 /* ignore any oplock requests if oplocks are disabled */
1581 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
1582 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1583 /* Mask off everything except the private Samba bits. */
1584 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1587 /* this is for OS/2 long file names - say we don't support them */
1588 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1589 /* OS/2 Workplace shell fix may be main code stream in a later
1590 * release. */
1591 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1592 "supported.\n"));
1593 if (use_nt_status()) {
1594 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1596 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1599 switch( create_disposition ) {
1601 * Currently we're using FILE_SUPERSEDE as the same as
1602 * FILE_OVERWRITE_IF but they really are
1603 * different. FILE_SUPERSEDE deletes an existing file
1604 * (requiring delete access) then recreates it.
1606 case FILE_SUPERSEDE:
1607 /* If file exists replace/overwrite. If file doesn't
1608 * exist create. */
1609 flags2 |= (O_CREAT | O_TRUNC);
1610 clear_ads = true;
1611 break;
1613 case FILE_OVERWRITE_IF:
1614 /* If file exists replace/overwrite. If file doesn't
1615 * exist create. */
1616 flags2 |= (O_CREAT | O_TRUNC);
1617 clear_ads = true;
1618 break;
1620 case FILE_OPEN:
1621 /* If file exists open. If file doesn't exist error. */
1622 if (!file_existed) {
1623 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1624 "requested for file %s and file "
1625 "doesn't exist.\n",
1626 smb_fname_str_dbg(smb_fname)));
1627 errno = ENOENT;
1628 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1630 break;
1632 case FILE_OVERWRITE:
1633 /* If file exists overwrite. If file doesn't exist
1634 * error. */
1635 if (!file_existed) {
1636 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1637 "requested for file %s and file "
1638 "doesn't exist.\n",
1639 smb_fname_str_dbg(smb_fname) ));
1640 errno = ENOENT;
1641 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1643 flags2 |= O_TRUNC;
1644 clear_ads = true;
1645 break;
1647 case FILE_CREATE:
1648 /* If file exists error. If file doesn't exist
1649 * create. */
1650 if (file_existed) {
1651 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1652 "requested for file %s and file "
1653 "already exists.\n",
1654 smb_fname_str_dbg(smb_fname)));
1655 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1656 errno = EISDIR;
1657 } else {
1658 errno = EEXIST;
1660 return map_nt_error_from_unix(errno);
1662 flags2 |= (O_CREAT|O_EXCL);
1663 break;
1665 case FILE_OPEN_IF:
1666 /* If file exists open. If file doesn't exist
1667 * create. */
1668 flags2 |= O_CREAT;
1669 break;
1671 default:
1672 return NT_STATUS_INVALID_PARAMETER;
1675 /* We only care about matching attributes on file exists and
1676 * overwrite. */
1678 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1679 (create_disposition == FILE_OVERWRITE_IF))) {
1680 if (!open_match_attributes(conn, existing_dos_attributes,
1681 new_dos_attributes,
1682 smb_fname->st.st_ex_mode,
1683 unx_mode, &new_unx_mode)) {
1684 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1685 "for file %s (%x %x) (0%o, 0%o)\n",
1686 smb_fname_str_dbg(smb_fname),
1687 existing_dos_attributes,
1688 new_dos_attributes,
1689 (unsigned int)smb_fname->st.st_ex_mode,
1690 (unsigned int)unx_mode ));
1691 errno = EACCES;
1692 return NT_STATUS_ACCESS_DENIED;
1696 status = calculate_access_mask(conn, smb_fname, file_existed,
1697 access_mask,
1698 &access_mask);
1699 if (!NT_STATUS_IS_OK(status)) {
1700 DEBUG(10, ("open_file_ntcreate: calculate_access_mask "
1701 "on file %s returned %s\n",
1702 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1703 return status;
1706 open_access_mask = access_mask;
1708 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1709 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1712 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1713 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1714 access_mask));
1717 * Note that we ignore the append flag as append does not
1718 * mean the same thing under DOS and Unix.
1721 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1722 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1723 /* DENY_DOS opens are always underlying read-write on the
1724 file handle, no matter what the requested access mask
1725 says. */
1726 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1727 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1728 flags = O_RDWR;
1729 } else {
1730 flags = O_WRONLY;
1732 } else {
1733 flags = O_RDONLY;
1737 * Currently we only look at FILE_WRITE_THROUGH for create options.
1740 #if defined(O_SYNC)
1741 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1742 flags2 |= O_SYNC;
1744 #endif /* O_SYNC */
1746 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1747 flags2 |= O_APPEND;
1750 if (!posix_open && !CAN_WRITE(conn)) {
1752 * We should really return a permission denied error if either
1753 * O_CREAT or O_TRUNC are set, but for compatibility with
1754 * older versions of Samba we just AND them out.
1756 flags2 &= ~(O_CREAT|O_TRUNC);
1760 * Ensure we can't write on a read-only share or file.
1763 if (flags != O_RDONLY && file_existed &&
1764 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1765 DEBUG(5,("open_file_ntcreate: write access requested for "
1766 "file %s on read only %s\n",
1767 smb_fname_str_dbg(smb_fname),
1768 !CAN_WRITE(conn) ? "share" : "file" ));
1769 errno = EACCES;
1770 return NT_STATUS_ACCESS_DENIED;
1773 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1774 fsp->share_access = share_access;
1775 fsp->fh->private_options = private_flags;
1776 fsp->access_mask = open_access_mask; /* We change this to the
1777 * requested access_mask after
1778 * the open is done. */
1779 fsp->posix_open = posix_open;
1781 /* Ensure no SAMBA_PRIVATE bits can be set. */
1782 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1784 if (timeval_is_zero(&request_time)) {
1785 request_time = fsp->open_time;
1788 if (file_existed) {
1789 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1790 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1792 lck = get_share_mode_lock(talloc_tos(), id,
1793 conn->connectpath,
1794 smb_fname, &old_write_time);
1796 if (lck == NULL) {
1797 DEBUG(0, ("Could not get share mode lock\n"));
1798 return NT_STATUS_SHARING_VIOLATION;
1801 /* First pass - send break only on batch oplocks. */
1802 if ((req != NULL)
1803 && delay_for_oplocks(lck, fsp, req->mid, 1,
1804 oplock_request)) {
1805 schedule_defer_open(lck, request_time, req);
1806 TALLOC_FREE(lck);
1807 return NT_STATUS_SHARING_VIOLATION;
1810 /* Use the client requested access mask here, not the one we
1811 * open with. */
1812 status = open_mode_check(conn, lck, access_mask, share_access,
1813 create_options, &file_existed);
1815 if (NT_STATUS_IS_OK(status)) {
1816 /* We might be going to allow this open. Check oplock
1817 * status again. */
1818 /* Second pass - send break for both batch or
1819 * exclusive oplocks. */
1820 if ((req != NULL)
1821 && delay_for_oplocks(lck, fsp, req->mid, 2,
1822 oplock_request)) {
1823 schedule_defer_open(lck, request_time, req);
1824 TALLOC_FREE(lck);
1825 return NT_STATUS_SHARING_VIOLATION;
1829 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1830 /* DELETE_PENDING is not deferred for a second */
1831 TALLOC_FREE(lck);
1832 return status;
1835 if (!NT_STATUS_IS_OK(status)) {
1836 uint32 can_access_mask;
1837 bool can_access = True;
1839 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1841 /* Check if this can be done with the deny_dos and fcb
1842 * calls. */
1843 if (private_flags &
1844 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1845 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1846 if (req == NULL) {
1847 DEBUG(0, ("DOS open without an SMB "
1848 "request!\n"));
1849 TALLOC_FREE(lck);
1850 return NT_STATUS_INTERNAL_ERROR;
1853 /* Use the client requested access mask here,
1854 * not the one we open with. */
1855 status = fcb_or_dos_open(req,
1856 conn,
1857 fsp,
1858 smb_fname,
1860 req->smbpid,
1861 req->vuid,
1862 access_mask,
1863 share_access,
1864 create_options);
1866 if (NT_STATUS_IS_OK(status)) {
1867 TALLOC_FREE(lck);
1868 if (pinfo) {
1869 *pinfo = FILE_WAS_OPENED;
1871 return NT_STATUS_OK;
1876 * This next line is a subtlety we need for
1877 * MS-Access. If a file open will fail due to share
1878 * permissions and also for security (access) reasons,
1879 * we need to return the access failed error, not the
1880 * share error. We can't open the file due to kernel
1881 * oplock deadlock (it's possible we failed above on
1882 * the open_mode_check()) so use a userspace check.
1885 if (flags & O_RDWR) {
1886 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1887 } else if (flags & O_WRONLY) {
1888 can_access_mask = FILE_WRITE_DATA;
1889 } else {
1890 can_access_mask = FILE_READ_DATA;
1893 if (((can_access_mask & FILE_WRITE_DATA) &&
1894 !CAN_WRITE(conn)) ||
1895 !can_access_file_data(conn, smb_fname,
1896 can_access_mask)) {
1897 can_access = False;
1901 * If we're returning a share violation, ensure we
1902 * cope with the braindead 1 second delay.
1905 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
1906 lp_defer_sharing_violations()) {
1907 struct timeval timeout;
1908 struct deferred_open_record state;
1909 int timeout_usecs;
1911 /* this is a hack to speed up torture tests
1912 in 'make test' */
1913 timeout_usecs = lp_parm_int(SNUM(conn),
1914 "smbd","sharedelay",
1915 SHARING_VIOLATION_USEC_WAIT);
1917 /* This is a relative time, added to the absolute
1918 request_time value to get the absolute timeout time.
1919 Note that if this is the second or greater time we enter
1920 this codepath for this particular request mid then
1921 request_time is left as the absolute time of the *first*
1922 time this request mid was processed. This is what allows
1923 the request to eventually time out. */
1925 timeout = timeval_set(0, timeout_usecs);
1927 /* Nothing actually uses state.delayed_for_oplocks
1928 but it's handy to differentiate in debug messages
1929 between a 30 second delay due to oplock break, and
1930 a 1 second delay for share mode conflicts. */
1932 state.delayed_for_oplocks = False;
1933 state.id = id;
1935 if ((req != NULL)
1936 && !request_timed_out(request_time,
1937 timeout)) {
1938 defer_open(lck, request_time, timeout,
1939 req, &state);
1943 TALLOC_FREE(lck);
1944 if (can_access) {
1946 * We have detected a sharing violation here
1947 * so return the correct error code
1949 status = NT_STATUS_SHARING_VIOLATION;
1950 } else {
1951 status = NT_STATUS_ACCESS_DENIED;
1953 return status;
1957 * We exit this block with the share entry *locked*.....
1961 SMB_ASSERT(!file_existed || (lck != NULL));
1964 * Ensure we pay attention to default ACLs on directories if required.
1967 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1968 (def_acl = directory_has_default_acl(conn, parent_dir))) {
1969 unx_mode = 0777;
1972 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
1973 "access_mask = 0x%x, open_access_mask = 0x%x\n",
1974 (unsigned int)flags, (unsigned int)flags2,
1975 (unsigned int)unx_mode, (unsigned int)access_mask,
1976 (unsigned int)open_access_mask));
1979 * open_file strips any O_TRUNC flags itself.
1982 fsp_open = open_file(fsp, conn, req, parent_dir,
1983 flags|flags2, unx_mode, access_mask,
1984 open_access_mask);
1986 if (!NT_STATUS_IS_OK(fsp_open)) {
1987 if (lck != NULL) {
1988 TALLOC_FREE(lck);
1990 return fsp_open;
1993 if (!file_existed) {
1994 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1996 * Deal with the race condition where two smbd's detect the
1997 * file doesn't exist and do the create at the same time. One
1998 * of them will win and set a share mode, the other (ie. this
1999 * one) should check if the requested share mode for this
2000 * create is allowed.
2004 * Now the file exists and fsp is successfully opened,
2005 * fsp->dev and fsp->inode are valid and should replace the
2006 * dev=0,inode=0 from a non existent file. Spotted by
2007 * Nadav Danieli <nadavd@exanet.com>. JRA.
2010 id = fsp->file_id;
2012 lck = get_share_mode_lock(talloc_tos(), id,
2013 conn->connectpath,
2014 smb_fname, &old_write_time);
2016 if (lck == NULL) {
2017 DEBUG(0, ("open_file_ntcreate: Could not get share "
2018 "mode lock for %s\n",
2019 smb_fname_str_dbg(smb_fname)));
2020 fd_close(fsp);
2021 return NT_STATUS_SHARING_VIOLATION;
2024 /* First pass - send break only on batch oplocks. */
2025 if ((req != NULL)
2026 && delay_for_oplocks(lck, fsp, req->mid, 1,
2027 oplock_request)) {
2028 schedule_defer_open(lck, request_time, req);
2029 TALLOC_FREE(lck);
2030 fd_close(fsp);
2031 return NT_STATUS_SHARING_VIOLATION;
2034 status = open_mode_check(conn, lck, access_mask, share_access,
2035 create_options, &file_existed);
2037 if (NT_STATUS_IS_OK(status)) {
2038 /* We might be going to allow this open. Check oplock
2039 * status again. */
2040 /* Second pass - send break for both batch or
2041 * exclusive oplocks. */
2042 if ((req != NULL)
2043 && delay_for_oplocks(lck, fsp, req->mid, 2,
2044 oplock_request)) {
2045 schedule_defer_open(lck, request_time, req);
2046 TALLOC_FREE(lck);
2047 fd_close(fsp);
2048 return NT_STATUS_SHARING_VIOLATION;
2052 if (!NT_STATUS_IS_OK(status)) {
2053 struct deferred_open_record state;
2055 fd_close(fsp);
2057 state.delayed_for_oplocks = False;
2058 state.id = id;
2060 /* Do it all over again immediately. In the second
2061 * round we will find that the file existed and handle
2062 * the DELETE_PENDING and FCB cases correctly. No need
2063 * to duplicate the code here. Essentially this is a
2064 * "goto top of this function", but don't tell
2065 * anybody... */
2067 if (req != NULL) {
2068 defer_open(lck, request_time, timeval_zero(),
2069 req, &state);
2071 TALLOC_FREE(lck);
2072 return status;
2076 * We exit this block with the share entry *locked*.....
2081 SMB_ASSERT(lck != NULL);
2083 /* Delete streams if create_disposition requires it */
2084 if (file_existed && clear_ads &&
2085 !is_ntfs_stream_smb_fname(smb_fname)) {
2086 status = delete_all_streams(conn, smb_fname->base_name);
2087 if (!NT_STATUS_IS_OK(status)) {
2088 TALLOC_FREE(lck);
2089 fd_close(fsp);
2090 return status;
2094 /* note that we ignore failure for the following. It is
2095 basically a hack for NFS, and NFS will never set one of
2096 these only read them. Nobody but Samba can ever set a deny
2097 mode and we have already checked our more authoritative
2098 locking database for permission to set this deny mode. If
2099 the kernel refuses the operations then the kernel is wrong.
2100 note that GPFS supports it as well - jmcd */
2102 if (fsp->fh->fd != -1) {
2103 int ret_flock;
2104 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2105 if(ret_flock == -1 ){
2107 TALLOC_FREE(lck);
2108 fd_close(fsp);
2110 return NT_STATUS_SHARING_VIOLATION;
2115 * At this point onwards, we can guarentee that the share entry
2116 * is locked, whether we created the file or not, and that the
2117 * deny mode is compatible with all current opens.
2121 * If requested, truncate the file.
2124 if (flags2&O_TRUNC) {
2126 * We are modifing the file after open - update the stat
2127 * struct..
2129 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2130 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2131 status = map_nt_error_from_unix(errno);
2132 TALLOC_FREE(lck);
2133 fd_close(fsp);
2134 return status;
2139 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2141 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2143 if (file_existed) {
2144 /* stat opens on existing files don't get oplocks. */
2145 if (is_stat_open(open_access_mask)) {
2146 fsp->oplock_type = NO_OPLOCK;
2149 if (!(flags2 & O_TRUNC)) {
2150 info = FILE_WAS_OPENED;
2151 } else {
2152 info = FILE_WAS_OVERWRITTEN;
2154 } else {
2155 info = FILE_WAS_CREATED;
2158 if (pinfo) {
2159 *pinfo = info;
2163 * Setup the oplock info in both the shared memory and
2164 * file structs.
2167 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2168 /* Could not get the kernel oplock */
2169 fsp->oplock_type = NO_OPLOCK;
2172 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
2173 new_file_created = True;
2176 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0,
2177 fsp->oplock_type);
2179 /* Handle strange delete on close create semantics. */
2180 if (create_options & FILE_DELETE_ON_CLOSE) {
2182 status = can_set_delete_on_close(fsp, new_dos_attributes);
2184 if (!NT_STATUS_IS_OK(status)) {
2185 /* Remember to delete the mode we just added. */
2186 del_share_mode(lck, fsp);
2187 TALLOC_FREE(lck);
2188 fd_close(fsp);
2189 return status;
2191 /* Note that here we set the *inital* delete on close flag,
2192 not the regular one. The magic gets handled in close. */
2193 fsp->initial_delete_on_close = True;
2196 if (new_file_created) {
2197 /* Files should be initially set as archive */
2198 if (lp_map_archive(SNUM(conn)) ||
2199 lp_store_dos_attributes(SNUM(conn))) {
2200 if (!posix_open) {
2201 if (file_set_dosmode(conn, smb_fname,
2202 new_dos_attributes | aARCH,
2203 parent_dir, true) == 0) {
2204 unx_mode = smb_fname->st.st_ex_mode;
2211 * Take care of inherited ACLs on created files - if default ACL not
2212 * selected.
2215 if (!posix_open && !file_existed && !def_acl) {
2217 int saved_errno = errno; /* We might get ENOSYS in the next
2218 * call.. */
2220 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2221 errno == ENOSYS) {
2222 errno = saved_errno; /* Ignore ENOSYS */
2225 } else if (new_unx_mode) {
2227 int ret = -1;
2229 /* Attributes need changing. File already existed. */
2232 int saved_errno = errno; /* We might get ENOSYS in the
2233 * next call.. */
2234 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2236 if (ret == -1 && errno == ENOSYS) {
2237 errno = saved_errno; /* Ignore ENOSYS */
2238 } else {
2239 DEBUG(5, ("open_file_ntcreate: reset "
2240 "attributes of file %s to 0%o\n",
2241 smb_fname_str_dbg(smb_fname),
2242 (unsigned int)new_unx_mode));
2243 ret = 0; /* Don't do the fchmod below. */
2247 if ((ret == -1) &&
2248 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2249 DEBUG(5, ("open_file_ntcreate: failed to reset "
2250 "attributes of file %s to 0%o\n",
2251 smb_fname_str_dbg(smb_fname),
2252 (unsigned int)new_unx_mode));
2255 /* If this is a successful open, we must remove any deferred open
2256 * records. */
2257 if (req != NULL) {
2258 del_deferred_open_entry(lck, req->mid);
2260 TALLOC_FREE(lck);
2262 return NT_STATUS_OK;
2266 /****************************************************************************
2267 Open a file for for write to ensure that we can fchmod it.
2268 ****************************************************************************/
2270 NTSTATUS open_file_fchmod(struct smb_request *req, connection_struct *conn,
2271 struct smb_filename *smb_fname,
2272 files_struct **result)
2274 files_struct *fsp = NULL;
2275 NTSTATUS status;
2277 if (!VALID_STAT(smb_fname->st)) {
2278 return NT_STATUS_INVALID_PARAMETER;
2281 status = file_new(req, conn, &fsp);
2282 if(!NT_STATUS_IS_OK(status)) {
2283 return status;
2286 status = SMB_VFS_CREATE_FILE(
2287 conn, /* conn */
2288 NULL, /* req */
2289 0, /* root_dir_fid */
2290 smb_fname, /* fname */
2291 FILE_WRITE_DATA, /* access_mask */
2292 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2293 FILE_SHARE_DELETE),
2294 FILE_OPEN, /* create_disposition*/
2295 0, /* create_options */
2296 0, /* file_attributes */
2297 0, /* oplock_request */
2298 0, /* allocation_size */
2299 0, /* private_flags */
2300 NULL, /* sd */
2301 NULL, /* ea_list */
2302 &fsp, /* result */
2303 NULL); /* pinfo */
2306 * This is not a user visible file open.
2307 * Don't set a share mode.
2310 if (!NT_STATUS_IS_OK(status)) {
2311 file_free(req, fsp);
2312 return status;
2315 *result = fsp;
2316 return NT_STATUS_OK;
2319 /****************************************************************************
2320 Close the fchmod file fd - ensure no locks are lost.
2321 ****************************************************************************/
2323 NTSTATUS close_file_fchmod(struct smb_request *req, files_struct *fsp)
2325 NTSTATUS status = fd_close(fsp);
2326 file_free(req, fsp);
2327 return status;
2330 static NTSTATUS mkdir_internal(connection_struct *conn,
2331 struct smb_filename *smb_dname,
2332 uint32 file_attributes)
2334 mode_t mode;
2335 char *parent_dir;
2336 NTSTATUS status;
2337 bool posix_open = false;
2339 if(!CAN_WRITE(conn)) {
2340 DEBUG(5,("mkdir_internal: failing create on read-only share "
2341 "%s\n", lp_servicename(SNUM(conn))));
2342 return NT_STATUS_ACCESS_DENIED;
2345 status = check_name(conn, smb_dname->base_name);
2346 if (!NT_STATUS_IS_OK(status)) {
2347 return status;
2350 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2351 NULL)) {
2352 return NT_STATUS_NO_MEMORY;
2355 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2356 posix_open = true;
2357 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2358 } else {
2359 mode = unix_mode(conn, aDIR, smb_dname, parent_dir);
2362 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2363 return map_nt_error_from_unix(errno);
2366 /* Ensure we're checking for a symlink here.... */
2367 /* We don't want to get caught by a symlink racer. */
2369 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2370 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2371 smb_fname_str_dbg(smb_dname), strerror(errno)));
2372 return map_nt_error_from_unix(errno);
2375 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2376 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2377 smb_fname_str_dbg(smb_dname)));
2378 return NT_STATUS_ACCESS_DENIED;
2381 if (lp_store_dos_attributes(SNUM(conn))) {
2382 if (!posix_open) {
2383 file_set_dosmode(conn, smb_dname,
2384 file_attributes | aDIR,
2385 parent_dir, true);
2389 if (lp_inherit_perms(SNUM(conn))) {
2390 inherit_access_posix_acl(conn, parent_dir,
2391 smb_dname->base_name, mode);
2394 if (!posix_open) {
2396 * Check if high bits should have been set,
2397 * then (if bits are missing): add them.
2398 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2399 * dir.
2401 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2402 (mode & ~smb_dname->st.st_ex_mode)) {
2403 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2404 (smb_dname->st.st_ex_mode |
2405 (mode & ~smb_dname->st.st_ex_mode)));
2409 /* Change the owner if required. */
2410 if (lp_inherit_owner(SNUM(conn))) {
2411 change_dir_owner_to_parent(conn, parent_dir,
2412 smb_dname->base_name,
2413 &smb_dname->st);
2416 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2417 smb_dname->base_name);
2419 return NT_STATUS_OK;
2422 /****************************************************************************
2423 Open a directory from an NT SMB call.
2424 ****************************************************************************/
2426 static NTSTATUS open_directory(connection_struct *conn,
2427 struct smb_request *req,
2428 struct smb_filename *smb_dname,
2429 uint32 access_mask,
2430 uint32 share_access,
2431 uint32 create_disposition,
2432 uint32 create_options,
2433 uint32 file_attributes,
2434 int *pinfo,
2435 files_struct **result)
2437 files_struct *fsp = NULL;
2438 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2439 struct share_mode_lock *lck = NULL;
2440 NTSTATUS status;
2441 struct timespec mtimespec;
2442 int info = 0;
2444 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
2446 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2447 "share_access = 0x%x create_options = 0x%x, "
2448 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2449 smb_fname_str_dbg(smb_dname),
2450 (unsigned int)access_mask,
2451 (unsigned int)share_access,
2452 (unsigned int)create_options,
2453 (unsigned int)create_disposition,
2454 (unsigned int)file_attributes));
2456 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2457 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2458 is_ntfs_stream_smb_fname(smb_dname)) {
2459 DEBUG(2, ("open_directory: %s is a stream name!\n",
2460 smb_fname_str_dbg(smb_dname)));
2461 return NT_STATUS_NOT_A_DIRECTORY;
2464 status = calculate_access_mask(conn, smb_dname, dir_existed,
2465 access_mask, &access_mask);
2466 if (!NT_STATUS_IS_OK(status)) {
2467 DEBUG(10, ("open_directory: calculate_access_mask "
2468 "on file %s returned %s\n",
2469 smb_fname_str_dbg(smb_dname),
2470 nt_errstr(status)));
2471 return status;
2474 /* We need to support SeSecurityPrivilege for this. */
2475 if (access_mask & SEC_FLAG_SYSTEM_SECURITY) {
2476 DEBUG(10, ("open_directory: open on %s "
2477 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2478 smb_fname_str_dbg(smb_dname)));
2479 return NT_STATUS_PRIVILEGE_NOT_HELD;
2482 switch( create_disposition ) {
2483 case FILE_OPEN:
2485 info = FILE_WAS_OPENED;
2488 * We want to follow symlinks here.
2491 if (SMB_VFS_STAT(conn, smb_dname) != 0) {
2492 return map_nt_error_from_unix(errno);
2495 break;
2497 case FILE_CREATE:
2499 /* If directory exists error. If directory doesn't
2500 * exist create. */
2502 status = mkdir_internal(conn, smb_dname,
2503 file_attributes);
2505 if (!NT_STATUS_IS_OK(status)) {
2506 DEBUG(2, ("open_directory: unable to create "
2507 "%s. Error was %s\n",
2508 smb_fname_str_dbg(smb_dname),
2509 nt_errstr(status)));
2510 return status;
2513 info = FILE_WAS_CREATED;
2514 break;
2516 case FILE_OPEN_IF:
2518 * If directory exists open. If directory doesn't
2519 * exist create.
2522 status = mkdir_internal(conn, smb_dname,
2523 file_attributes);
2525 if (NT_STATUS_IS_OK(status)) {
2526 info = FILE_WAS_CREATED;
2529 if (NT_STATUS_EQUAL(status,
2530 NT_STATUS_OBJECT_NAME_COLLISION)) {
2531 info = FILE_WAS_OPENED;
2532 status = NT_STATUS_OK;
2535 break;
2537 case FILE_SUPERSEDE:
2538 case FILE_OVERWRITE:
2539 case FILE_OVERWRITE_IF:
2540 default:
2541 DEBUG(5,("open_directory: invalid create_disposition "
2542 "0x%x for directory %s\n",
2543 (unsigned int)create_disposition,
2544 smb_fname_str_dbg(smb_dname)));
2545 return NT_STATUS_INVALID_PARAMETER;
2548 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2549 DEBUG(5,("open_directory: %s is not a directory !\n",
2550 smb_fname_str_dbg(smb_dname)));
2551 return NT_STATUS_NOT_A_DIRECTORY;
2554 if (info == FILE_WAS_OPENED) {
2555 uint32_t access_granted = 0;
2556 status = smbd_check_open_rights(conn, smb_dname, access_mask,
2557 &access_granted);
2559 /* Were we trying to do a directory open
2560 * for delete and didn't get DELETE
2561 * access (only) ? Check if the
2562 * directory allows DELETE_CHILD.
2563 * See here:
2564 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
2565 * for details. */
2567 if ((NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
2568 (access_mask & DELETE_ACCESS) &&
2569 (access_granted == DELETE_ACCESS) &&
2570 can_delete_file_in_directory(conn, smb_dname))) {
2571 DEBUG(10,("open_directory: overrode ACCESS_DENIED "
2572 "on directory %s\n",
2573 smb_fname_str_dbg(smb_dname)));
2574 status = NT_STATUS_OK;
2577 if (!NT_STATUS_IS_OK(status)) {
2578 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2579 "file %s failed with %s\n",
2580 smb_fname_str_dbg(smb_dname),
2581 nt_errstr(status)));
2582 return status;
2586 status = file_new(req, conn, &fsp);
2587 if(!NT_STATUS_IS_OK(status)) {
2588 return status;
2592 * Setup the files_struct for it.
2595 fsp->mode = smb_dname->st.st_ex_mode;
2596 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2597 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2598 fsp->file_pid = req ? req->smbpid : 0;
2599 fsp->can_lock = False;
2600 fsp->can_read = False;
2601 fsp->can_write = False;
2603 fsp->share_access = share_access;
2604 fsp->fh->private_options = 0;
2606 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2608 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2609 fsp->print_file = False;
2610 fsp->modified = False;
2611 fsp->oplock_type = NO_OPLOCK;
2612 fsp->sent_oplock_break = NO_BREAK_SENT;
2613 fsp->is_directory = True;
2614 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2615 status = fsp_set_smb_fname(fsp, smb_dname);
2616 if (!NT_STATUS_IS_OK(status)) {
2617 return status;
2620 mtimespec = smb_dname->st.st_ex_mtime;
2622 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2623 conn->connectpath, smb_dname, &mtimespec);
2625 if (lck == NULL) {
2626 DEBUG(0, ("open_directory: Could not get share mode lock for "
2627 "%s\n", smb_fname_str_dbg(smb_dname)));
2628 file_free(req, fsp);
2629 return NT_STATUS_SHARING_VIOLATION;
2632 status = open_mode_check(conn, lck, access_mask, share_access,
2633 create_options, &dir_existed);
2635 if (!NT_STATUS_IS_OK(status)) {
2636 TALLOC_FREE(lck);
2637 file_free(req, fsp);
2638 return status;
2641 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0, NO_OPLOCK);
2643 /* For directories the delete on close bit at open time seems
2644 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2645 if (create_options & FILE_DELETE_ON_CLOSE) {
2646 status = can_set_delete_on_close(fsp, 0);
2647 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2648 TALLOC_FREE(lck);
2649 file_free(req, fsp);
2650 return status;
2653 if (NT_STATUS_IS_OK(status)) {
2654 /* Note that here we set the *inital* delete on close flag,
2655 not the regular one. The magic gets handled in close. */
2656 fsp->initial_delete_on_close = True;
2660 TALLOC_FREE(lck);
2662 if (pinfo) {
2663 *pinfo = info;
2666 *result = fsp;
2667 return NT_STATUS_OK;
2670 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2671 struct smb_filename *smb_dname)
2673 NTSTATUS status;
2674 files_struct *fsp;
2676 status = SMB_VFS_CREATE_FILE(
2677 conn, /* conn */
2678 req, /* req */
2679 0, /* root_dir_fid */
2680 smb_dname, /* fname */
2681 FILE_READ_ATTRIBUTES, /* access_mask */
2682 FILE_SHARE_NONE, /* share_access */
2683 FILE_CREATE, /* create_disposition*/
2684 FILE_DIRECTORY_FILE, /* create_options */
2685 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2686 0, /* oplock_request */
2687 0, /* allocation_size */
2688 0, /* private_flags */
2689 NULL, /* sd */
2690 NULL, /* ea_list */
2691 &fsp, /* result */
2692 NULL); /* pinfo */
2694 if (NT_STATUS_IS_OK(status)) {
2695 close_file(req, fsp, NORMAL_CLOSE);
2698 return status;
2701 /****************************************************************************
2702 Receive notification that one of our open files has been renamed by another
2703 smbd process.
2704 ****************************************************************************/
2706 void msg_file_was_renamed(struct messaging_context *msg,
2707 void *private_data,
2708 uint32_t msg_type,
2709 struct server_id server_id,
2710 DATA_BLOB *data)
2712 files_struct *fsp;
2713 char *frm = (char *)data->data;
2714 struct file_id id;
2715 const char *sharepath;
2716 const char *base_name;
2717 const char *stream_name;
2718 struct smb_filename *smb_fname = NULL;
2719 size_t sp_len, bn_len;
2720 NTSTATUS status;
2722 if (data->data == NULL
2723 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2724 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2725 (int)data->length));
2726 return;
2729 /* Unpack the message. */
2730 pull_file_id_24(frm, &id);
2731 sharepath = &frm[24];
2732 sp_len = strlen(sharepath);
2733 base_name = sharepath + sp_len + 1;
2734 bn_len = strlen(base_name);
2735 stream_name = sharepath + sp_len + 1 + bn_len + 1;
2737 /* stream_name must always be NULL if there is no stream. */
2738 if (stream_name[0] == '\0') {
2739 stream_name = NULL;
2742 status = create_synthetic_smb_fname(talloc_tos(), base_name,
2743 stream_name, NULL, &smb_fname);
2744 if (!NT_STATUS_IS_OK(status)) {
2745 return;
2748 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2749 "file_id %s\n",
2750 sharepath, smb_fname_str_dbg(smb_fname),
2751 file_id_string_tos(&id)));
2753 for(fsp = file_find_di_first(id); fsp; fsp = file_find_di_next(fsp)) {
2754 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2756 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2757 fsp->fnum, fsp_str_dbg(fsp),
2758 smb_fname_str_dbg(smb_fname)));
2759 status = fsp_set_smb_fname(fsp, smb_fname);
2760 if (!NT_STATUS_IS_OK(status)) {
2761 goto out;
2763 } else {
2764 /* TODO. JRA. */
2765 /* Now we have the complete path we can work out if this is
2766 actually within this share and adjust newname accordingly. */
2767 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2768 "not sharepath %s) "
2769 "fnum %d from %s -> %s\n",
2770 fsp->conn->connectpath,
2771 sharepath,
2772 fsp->fnum,
2773 fsp_str_dbg(fsp),
2774 smb_fname_str_dbg(smb_fname)));
2777 out:
2778 TALLOC_FREE(smb_fname);
2779 return;
2783 * If a main file is opened for delete, all streams need to be checked for
2784 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2785 * If that works, delete them all by setting the delete on close and close.
2788 NTSTATUS open_streams_for_delete(connection_struct *conn,
2789 const char *fname)
2791 struct stream_struct *stream_info;
2792 files_struct **streams;
2793 int i;
2794 unsigned int num_streams;
2795 TALLOC_CTX *frame = talloc_stackframe();
2796 NTSTATUS status;
2798 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
2799 &num_streams, &stream_info);
2801 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
2802 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2803 DEBUG(10, ("no streams around\n"));
2804 TALLOC_FREE(frame);
2805 return NT_STATUS_OK;
2808 if (!NT_STATUS_IS_OK(status)) {
2809 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
2810 nt_errstr(status)));
2811 goto fail;
2814 DEBUG(10, ("open_streams_for_delete found %d streams\n",
2815 num_streams));
2817 if (num_streams == 0) {
2818 TALLOC_FREE(frame);
2819 return NT_STATUS_OK;
2822 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
2823 if (streams == NULL) {
2824 DEBUG(0, ("talloc failed\n"));
2825 status = NT_STATUS_NO_MEMORY;
2826 goto fail;
2829 for (i=0; i<num_streams; i++) {
2830 struct smb_filename *smb_fname = NULL;
2832 if (strequal(stream_info[i].name, "::$DATA")) {
2833 streams[i] = NULL;
2834 continue;
2837 status = create_synthetic_smb_fname(talloc_tos(), fname,
2838 stream_info[i].name,
2839 NULL, &smb_fname);
2840 if (!NT_STATUS_IS_OK(status)) {
2841 goto fail;
2844 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
2845 DEBUG(10, ("Unable to stat stream: %s\n",
2846 smb_fname_str_dbg(smb_fname)));
2849 status = SMB_VFS_CREATE_FILE(
2850 conn, /* conn */
2851 NULL, /* req */
2852 0, /* root_dir_fid */
2853 smb_fname, /* fname */
2854 DELETE_ACCESS, /* access_mask */
2855 (FILE_SHARE_READ | /* share_access */
2856 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
2857 FILE_OPEN, /* create_disposition*/
2858 0, /* create_options */
2859 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2860 0, /* oplock_request */
2861 0, /* allocation_size */
2862 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
2863 NULL, /* sd */
2864 NULL, /* ea_list */
2865 &streams[i], /* result */
2866 NULL); /* pinfo */
2868 if (!NT_STATUS_IS_OK(status)) {
2869 DEBUG(10, ("Could not open stream %s: %s\n",
2870 smb_fname_str_dbg(smb_fname),
2871 nt_errstr(status)));
2873 TALLOC_FREE(smb_fname);
2874 break;
2876 TALLOC_FREE(smb_fname);
2880 * don't touch the variable "status" beyond this point :-)
2883 for (i -= 1 ; i >= 0; i--) {
2884 if (streams[i] == NULL) {
2885 continue;
2888 DEBUG(10, ("Closing stream # %d, %s\n", i,
2889 fsp_str_dbg(streams[i])));
2890 close_file(NULL, streams[i], NORMAL_CLOSE);
2893 fail:
2894 TALLOC_FREE(frame);
2895 return status;
2899 * Wrapper around open_file_ntcreate and open_directory
2902 static NTSTATUS create_file_unixpath(connection_struct *conn,
2903 struct smb_request *req,
2904 struct smb_filename *smb_fname,
2905 uint32_t access_mask,
2906 uint32_t share_access,
2907 uint32_t create_disposition,
2908 uint32_t create_options,
2909 uint32_t file_attributes,
2910 uint32_t oplock_request,
2911 uint64_t allocation_size,
2912 uint32_t private_flags,
2913 struct security_descriptor *sd,
2914 struct ea_list *ea_list,
2916 files_struct **result,
2917 int *pinfo)
2919 int info = FILE_WAS_OPENED;
2920 files_struct *base_fsp = NULL;
2921 files_struct *fsp = NULL;
2922 NTSTATUS status;
2924 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
2925 "file_attributes = 0x%x, share_access = 0x%x, "
2926 "create_disposition = 0x%x create_options = 0x%x "
2927 "oplock_request = 0x%x private_flags = 0x%x "
2928 "ea_list = 0x%p, sd = 0x%p, "
2929 "fname = %s\n",
2930 (unsigned int)access_mask,
2931 (unsigned int)file_attributes,
2932 (unsigned int)share_access,
2933 (unsigned int)create_disposition,
2934 (unsigned int)create_options,
2935 (unsigned int)oplock_request,
2936 (unsigned int)private_flags,
2937 ea_list, sd, smb_fname_str_dbg(smb_fname)));
2939 if (create_options & FILE_OPEN_BY_FILE_ID) {
2940 status = NT_STATUS_NOT_SUPPORTED;
2941 goto fail;
2944 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
2945 status = NT_STATUS_INVALID_PARAMETER;
2946 goto fail;
2949 if (req == NULL) {
2950 oplock_request |= INTERNAL_OPEN_ONLY;
2953 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2954 && (access_mask & DELETE_ACCESS)
2955 && !is_ntfs_stream_smb_fname(smb_fname)) {
2957 * We can't open a file with DELETE access if any of the
2958 * streams is open without FILE_SHARE_DELETE
2960 status = open_streams_for_delete(conn, smb_fname->base_name);
2962 if (!NT_STATUS_IS_OK(status)) {
2963 goto fail;
2967 /* This is the correct thing to do (check every time) but can_delete
2968 * is expensive (it may have to read the parent directory
2969 * permissions). So for now we're not doing it unless we have a strong
2970 * hint the client is really going to delete this file. If the client
2971 * is forcing FILE_CREATE let the filesystem take care of the
2972 * permissions. */
2974 /* Setting FILE_SHARE_DELETE is the hint. */
2976 if (lp_acl_check_permissions(SNUM(conn))
2977 && (create_disposition != FILE_CREATE)
2978 && (share_access & FILE_SHARE_DELETE)
2979 && (access_mask & DELETE_ACCESS)
2980 && (!(can_delete_file_in_directory(conn, smb_fname) ||
2981 can_access_file_acl(conn, smb_fname, DELETE_ACCESS)))) {
2982 status = NT_STATUS_ACCESS_DENIED;
2983 DEBUG(10,("create_file_unixpath: open file %s "
2984 "for delete ACCESS_DENIED\n",
2985 smb_fname_str_dbg(smb_fname)));
2986 goto fail;
2989 #if 0
2990 /* We need to support SeSecurityPrivilege for this. */
2991 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2992 !user_has_privileges(current_user.nt_user_token,
2993 &se_security)) {
2994 status = NT_STATUS_PRIVILEGE_NOT_HELD;
2995 goto fail;
2997 #else
2998 /* We need to support SeSecurityPrivilege for this. */
2999 if (access_mask & SEC_FLAG_SYSTEM_SECURITY) {
3000 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3001 goto fail;
3003 /* Don't allow a SACL set from an NTtrans create until we
3004 * support SeSecurityPrivilege. */
3005 if (!VALID_STAT(smb_fname->st) &&
3006 lp_nt_acl_support(SNUM(conn)) &&
3007 sd && (sd->sacl != NULL)) {
3008 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3009 goto fail;
3011 #endif
3013 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3014 && is_ntfs_stream_smb_fname(smb_fname)
3015 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3016 uint32 base_create_disposition;
3017 struct smb_filename *smb_fname_base = NULL;
3019 if (create_options & FILE_DIRECTORY_FILE) {
3020 status = NT_STATUS_NOT_A_DIRECTORY;
3021 goto fail;
3024 switch (create_disposition) {
3025 case FILE_OPEN:
3026 base_create_disposition = FILE_OPEN;
3027 break;
3028 default:
3029 base_create_disposition = FILE_OPEN_IF;
3030 break;
3033 /* Create an smb_filename with stream_name == NULL. */
3034 status = create_synthetic_smb_fname(talloc_tos(),
3035 smb_fname->base_name,
3036 NULL, NULL,
3037 &smb_fname_base);
3038 if (!NT_STATUS_IS_OK(status)) {
3039 goto fail;
3042 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3043 DEBUG(10, ("Unable to stat stream: %s\n",
3044 smb_fname_str_dbg(smb_fname_base)));
3047 /* Open the base file. */
3048 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3049 FILE_SHARE_READ
3050 | FILE_SHARE_WRITE
3051 | FILE_SHARE_DELETE,
3052 base_create_disposition,
3053 0, 0, 0, 0, 0, NULL, NULL,
3054 &base_fsp, NULL);
3055 TALLOC_FREE(smb_fname_base);
3057 if (!NT_STATUS_IS_OK(status)) {
3058 DEBUG(10, ("create_file_unixpath for base %s failed: "
3059 "%s\n", smb_fname->base_name,
3060 nt_errstr(status)));
3061 goto fail;
3063 /* we don't need to low level fd */
3064 fd_close(base_fsp);
3068 * If it's a request for a directory open, deal with it separately.
3071 if (create_options & FILE_DIRECTORY_FILE) {
3073 if (create_options & FILE_NON_DIRECTORY_FILE) {
3074 status = NT_STATUS_INVALID_PARAMETER;
3075 goto fail;
3078 /* Can't open a temp directory. IFS kit test. */
3079 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3080 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3081 status = NT_STATUS_INVALID_PARAMETER;
3082 goto fail;
3086 * We will get a create directory here if the Win32
3087 * app specified a security descriptor in the
3088 * CreateDirectory() call.
3091 oplock_request = 0;
3092 status = open_directory(
3093 conn, req, smb_fname, access_mask, share_access,
3094 create_disposition, create_options, file_attributes,
3095 &info, &fsp);
3096 } else {
3099 * Ordinary file case.
3102 status = file_new(req, conn, &fsp);
3103 if(!NT_STATUS_IS_OK(status)) {
3104 goto fail;
3107 status = fsp_set_smb_fname(fsp, smb_fname);
3108 if (!NT_STATUS_IS_OK(status)) {
3109 goto fail;
3113 * We're opening the stream element of a base_fsp
3114 * we already opened. Set up the base_fsp pointer.
3116 if (base_fsp) {
3117 fsp->base_fsp = base_fsp;
3120 status = open_file_ntcreate(conn,
3121 req,
3122 access_mask,
3123 share_access,
3124 create_disposition,
3125 create_options,
3126 file_attributes,
3127 oplock_request,
3128 private_flags,
3129 &info,
3130 fsp);
3132 if(!NT_STATUS_IS_OK(status)) {
3133 file_free(req, fsp);
3134 fsp = NULL;
3137 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3139 /* A stream open never opens a directory */
3141 if (base_fsp) {
3142 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3143 goto fail;
3147 * Fail the open if it was explicitly a non-directory
3148 * file.
3151 if (create_options & FILE_NON_DIRECTORY_FILE) {
3152 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3153 goto fail;
3156 oplock_request = 0;
3157 status = open_directory(
3158 conn, req, smb_fname, access_mask,
3159 share_access, create_disposition,
3160 create_options, file_attributes,
3161 &info, &fsp);
3165 if (!NT_STATUS_IS_OK(status)) {
3166 goto fail;
3169 fsp->base_fsp = base_fsp;
3172 * According to the MS documentation, the only time the security
3173 * descriptor is applied to the opened file is iff we *created* the
3174 * file; an existing file stays the same.
3176 * Also, it seems (from observation) that you can open the file with
3177 * any access mask but you can still write the sd. We need to override
3178 * the granted access before we call set_sd
3179 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3182 if ((sd != NULL) && (info == FILE_WAS_CREATED)
3183 && lp_nt_acl_support(SNUM(conn))) {
3185 uint32_t sec_info_sent;
3186 uint32_t saved_access_mask = fsp->access_mask;
3188 sec_info_sent = get_sec_info(sd);
3190 fsp->access_mask = FILE_GENERIC_ALL;
3192 /* Convert all the generic bits. */
3193 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3194 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3196 if (sec_info_sent & (OWNER_SECURITY_INFORMATION|
3197 GROUP_SECURITY_INFORMATION|
3198 DACL_SECURITY_INFORMATION|
3199 SACL_SECURITY_INFORMATION)) {
3200 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3203 fsp->access_mask = saved_access_mask;
3205 if (!NT_STATUS_IS_OK(status)) {
3206 goto fail;
3210 if ((ea_list != NULL) &&
3211 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3212 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3213 if (!NT_STATUS_IS_OK(status)) {
3214 goto fail;
3218 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3219 status = NT_STATUS_ACCESS_DENIED;
3220 goto fail;
3223 /* Save the requested allocation size. */
3224 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3225 if (allocation_size
3226 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3227 fsp->initial_allocation_size = smb_roundup(
3228 fsp->conn, allocation_size);
3229 if (fsp->is_directory) {
3230 /* Can't set allocation size on a directory. */
3231 status = NT_STATUS_ACCESS_DENIED;
3232 goto fail;
3234 if (vfs_allocate_file_space(
3235 fsp, fsp->initial_allocation_size) == -1) {
3236 status = NT_STATUS_DISK_FULL;
3237 goto fail;
3239 } else {
3240 fsp->initial_allocation_size = smb_roundup(
3241 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3245 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3247 *result = fsp;
3248 if (pinfo != NULL) {
3249 *pinfo = info;
3252 smb_fname->st = fsp->fsp_name->st;
3254 return NT_STATUS_OK;
3256 fail:
3257 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3259 if (fsp != NULL) {
3260 if (base_fsp && fsp->base_fsp == base_fsp) {
3262 * The close_file below will close
3263 * fsp->base_fsp.
3265 base_fsp = NULL;
3267 close_file(req, fsp, ERROR_CLOSE);
3268 fsp = NULL;
3270 if (base_fsp != NULL) {
3271 close_file(req, base_fsp, ERROR_CLOSE);
3272 base_fsp = NULL;
3274 return status;
3278 * Calculate the full path name given a relative fid.
3280 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3281 struct smb_request *req,
3282 uint16_t root_dir_fid,
3283 struct smb_filename *smb_fname)
3285 files_struct *dir_fsp;
3286 char *parent_fname = NULL;
3287 char *new_base_name = NULL;
3288 NTSTATUS status;
3290 if (root_dir_fid == 0 || !smb_fname) {
3291 status = NT_STATUS_INTERNAL_ERROR;
3292 goto out;
3295 dir_fsp = file_fsp(req, root_dir_fid);
3297 if (dir_fsp == NULL) {
3298 status = NT_STATUS_INVALID_HANDLE;
3299 goto out;
3302 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3303 status = NT_STATUS_INVALID_HANDLE;
3304 goto out;
3307 if (!dir_fsp->is_directory) {
3310 * Check to see if this is a mac fork of some kind.
3313 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3314 is_ntfs_stream_smb_fname(smb_fname)) {
3315 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3316 goto out;
3320 we need to handle the case when we get a
3321 relative open relative to a file and the
3322 pathname is blank - this is a reopen!
3323 (hint from demyn plantenberg)
3326 status = NT_STATUS_INVALID_HANDLE;
3327 goto out;
3330 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3332 * We're at the toplevel dir, the final file name
3333 * must not contain ./, as this is filtered out
3334 * normally by srvstr_get_path and unix_convert
3335 * explicitly rejects paths containing ./.
3337 parent_fname = talloc_strdup(talloc_tos(), "");
3338 if (parent_fname == NULL) {
3339 status = NT_STATUS_NO_MEMORY;
3340 goto out;
3342 } else {
3343 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3346 * Copy in the base directory name.
3349 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3350 dir_name_len+2);
3351 if (parent_fname == NULL) {
3352 status = NT_STATUS_NO_MEMORY;
3353 goto out;
3355 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3356 dir_name_len+1);
3359 * Ensure it ends in a '/'.
3360 * We used TALLOC_SIZE +2 to add space for the '/'.
3363 if(dir_name_len
3364 && (parent_fname[dir_name_len-1] != '\\')
3365 && (parent_fname[dir_name_len-1] != '/')) {
3366 parent_fname[dir_name_len] = '/';
3367 parent_fname[dir_name_len+1] = '\0';
3371 new_base_name = talloc_asprintf(smb_fname, "%s%s", parent_fname,
3372 smb_fname->base_name);
3373 if (new_base_name == NULL) {
3374 status = NT_STATUS_NO_MEMORY;
3375 goto out;
3378 TALLOC_FREE(smb_fname->base_name);
3379 smb_fname->base_name = new_base_name;
3380 status = NT_STATUS_OK;
3382 out:
3383 TALLOC_FREE(parent_fname);
3384 return status;
3387 NTSTATUS create_file_default(connection_struct *conn,
3388 struct smb_request *req,
3389 uint16_t root_dir_fid,
3390 struct smb_filename *smb_fname,
3391 uint32_t access_mask,
3392 uint32_t share_access,
3393 uint32_t create_disposition,
3394 uint32_t create_options,
3395 uint32_t file_attributes,
3396 uint32_t oplock_request,
3397 uint64_t allocation_size,
3398 uint32_t private_flags,
3399 struct security_descriptor *sd,
3400 struct ea_list *ea_list,
3401 files_struct **result,
3402 int *pinfo)
3404 int info = FILE_WAS_OPENED;
3405 files_struct *fsp = NULL;
3406 NTSTATUS status;
3408 DEBUG(10,("create_file: access_mask = 0x%x "
3409 "file_attributes = 0x%x, share_access = 0x%x, "
3410 "create_disposition = 0x%x create_options = 0x%x "
3411 "oplock_request = 0x%x "
3412 "private_flags = 0x%x "
3413 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3414 "fname = %s\n",
3415 (unsigned int)access_mask,
3416 (unsigned int)file_attributes,
3417 (unsigned int)share_access,
3418 (unsigned int)create_disposition,
3419 (unsigned int)create_options,
3420 (unsigned int)oplock_request,
3421 (unsigned int)private_flags,
3422 (unsigned int)root_dir_fid,
3423 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3426 * Calculate the filename from the root_dir_if if necessary.
3429 if (root_dir_fid != 0) {
3430 status = get_relative_fid_filename(conn, req, root_dir_fid,
3431 smb_fname);
3432 if (!NT_STATUS_IS_OK(status)) {
3433 goto fail;
3438 * Check to see if this is a mac fork of some kind.
3441 if (is_ntfs_stream_smb_fname(smb_fname)) {
3442 enum FAKE_FILE_TYPE fake_file_type;
3444 fake_file_type = is_fake_file(smb_fname);
3446 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3449 * Here we go! support for changing the disk quotas
3450 * --metze
3452 * We need to fake up to open this MAGIC QUOTA file
3453 * and return a valid FID.
3455 * w2k close this file directly after openening xp
3456 * also tries a QUERY_FILE_INFO on the file and then
3457 * close it
3459 status = open_fake_file(req, conn, req->vuid,
3460 fake_file_type, smb_fname,
3461 access_mask, &fsp);
3462 if (!NT_STATUS_IS_OK(status)) {
3463 goto fail;
3466 ZERO_STRUCT(smb_fname->st);
3467 goto done;
3470 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3471 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3472 goto fail;
3476 /* All file access must go through check_name() */
3478 status = check_name(conn, smb_fname->base_name);
3479 if (!NT_STATUS_IS_OK(status)) {
3480 goto fail;
3483 status = create_file_unixpath(
3484 conn, req, smb_fname, access_mask, share_access,
3485 create_disposition, create_options, file_attributes,
3486 oplock_request, allocation_size, private_flags,
3487 sd, ea_list,
3488 &fsp, &info);
3490 if (!NT_STATUS_IS_OK(status)) {
3491 goto fail;
3494 done:
3495 DEBUG(10, ("create_file: info=%d\n", info));
3497 *result = fsp;
3498 if (pinfo != NULL) {
3499 *pinfo = info;
3501 return NT_STATUS_OK;
3503 fail:
3504 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3506 if (fsp != NULL) {
3507 close_file(req, fsp, ERROR_CLOSE);
3508 fsp = NULL;
3510 return status;