Use new common function.
[Samba/gebeck_regimport.git] / source3 / smbd / open.c
blob88f779a527e401c1d99463f1429c70bf77502240
1 /*
2 Unix SMB/CIFS implementation.
3 file opening and share modes
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2004
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "printing.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "fake_file.h"
28 #include "../libcli/security/security.h"
29 #include "../librpc/gen_ndr/ndr_security.h"
30 #include "../librpc/gen_ndr/open_files.h"
31 #include "auth.h"
32 #include "messages.h"
34 extern const struct generic_mapping file_generic_mapping;
36 struct deferred_open_record {
37 bool delayed_for_oplocks;
38 bool async_open;
39 struct file_id id;
42 /****************************************************************************
43 If the requester wanted DELETE_ACCESS and was rejected because
44 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
45 overrides this.
46 ****************************************************************************/
48 static bool parent_override_delete(connection_struct *conn,
49 const struct smb_filename *smb_fname,
50 uint32_t access_mask,
51 uint32_t rejected_mask)
53 if ((access_mask & DELETE_ACCESS) &&
54 (rejected_mask & DELETE_ACCESS) &&
55 can_delete_file_in_directory(conn, smb_fname)) {
56 return true;
58 return false;
61 /****************************************************************************
62 Check if we have open rights.
63 ****************************************************************************/
65 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
66 const struct smb_filename *smb_fname,
67 uint32_t access_mask)
69 /* Check if we have rights to open. */
70 NTSTATUS status;
71 struct security_descriptor *sd = NULL;
72 uint32_t rejected_share_access;
73 uint32_t rejected_mask = access_mask;
75 rejected_share_access = access_mask & ~(conn->share_access);
77 if (rejected_share_access) {
78 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
79 "on %s (0x%x)\n",
80 (unsigned int)access_mask,
81 smb_fname_str_dbg(smb_fname),
82 (unsigned int)rejected_share_access ));
83 return NT_STATUS_ACCESS_DENIED;
86 if (get_current_uid(conn) == (uid_t)0) {
87 /* I'm sorry sir, I didn't know you were root... */
88 DEBUG(10,("smbd_check_access_rights: root override "
89 "on %s. Granting 0x%x\n",
90 smb_fname_str_dbg(smb_fname),
91 (unsigned int)access_mask ));
92 return NT_STATUS_OK;
95 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
96 DEBUG(10,("smbd_check_access_rights: not checking ACL "
97 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
98 smb_fname_str_dbg(smb_fname),
99 (unsigned int)access_mask ));
100 return NT_STATUS_OK;
103 if (access_mask == DELETE_ACCESS &&
104 VALID_STAT(smb_fname->st) &&
105 S_ISLNK(smb_fname->st.st_ex_mode)) {
106 /* We can always delete a symlink. */
107 DEBUG(10,("smbd_check_access_rights: not checking ACL "
108 "on DELETE_ACCESS on symlink %s.\n",
109 smb_fname_str_dbg(smb_fname) ));
110 return NT_STATUS_OK;
113 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
114 (SECINFO_OWNER |
115 SECINFO_GROUP |
116 SECINFO_DACL),&sd);
118 if (!NT_STATUS_IS_OK(status)) {
119 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
120 "on %s: %s\n",
121 smb_fname_str_dbg(smb_fname),
122 nt_errstr(status)));
124 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
125 goto access_denied;
128 return status;
132 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
133 * owner WRITE_DAC and READ_CONTROL.
135 status = se_access_check(sd,
136 get_current_nttok(conn),
137 (access_mask & ~FILE_READ_ATTRIBUTES),
138 &rejected_mask);
140 DEBUG(10,("smbd_check_access_rights: file %s requesting "
141 "0x%x returning 0x%x (%s)\n",
142 smb_fname_str_dbg(smb_fname),
143 (unsigned int)access_mask,
144 (unsigned int)rejected_mask,
145 nt_errstr(status) ));
147 if (!NT_STATUS_IS_OK(status)) {
148 if (DEBUGLEVEL >= 10) {
149 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
150 smb_fname_str_dbg(smb_fname) ));
151 NDR_PRINT_DEBUG(security_descriptor, sd);
155 TALLOC_FREE(sd);
157 if (NT_STATUS_IS_OK(status) ||
158 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
159 return status;
162 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
164 access_denied:
166 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
167 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
168 !lp_store_dos_attributes(SNUM(conn)) &&
169 (lp_map_readonly(SNUM(conn)) ||
170 lp_map_archive(SNUM(conn)) ||
171 lp_map_hidden(SNUM(conn)) ||
172 lp_map_system(SNUM(conn)))) {
173 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
175 DEBUG(10,("smbd_check_access_rights: "
176 "overrode "
177 "FILE_WRITE_ATTRIBUTES "
178 "on file %s\n",
179 smb_fname_str_dbg(smb_fname)));
182 if (parent_override_delete(conn,
183 smb_fname,
184 access_mask,
185 rejected_mask)) {
186 /* Were we trying to do an open
187 * for delete and didn't get DELETE
188 * access (only) ? Check if the
189 * directory allows DELETE_CHILD.
190 * See here:
191 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
192 * for details. */
194 rejected_mask &= ~DELETE_ACCESS;
196 DEBUG(10,("smbd_check_access_rights: "
197 "overrode "
198 "DELETE_ACCESS on "
199 "file %s\n",
200 smb_fname_str_dbg(smb_fname)));
203 if (rejected_mask != 0) {
204 return NT_STATUS_ACCESS_DENIED;
206 return NT_STATUS_OK;
209 static NTSTATUS check_parent_access(struct connection_struct *conn,
210 struct smb_filename *smb_fname,
211 uint32_t access_mask)
213 NTSTATUS status;
214 char *parent_dir = NULL;
215 struct security_descriptor *parent_sd = NULL;
216 uint32_t access_granted = 0;
218 if (!parent_dirname(talloc_tos(),
219 smb_fname->base_name,
220 &parent_dir,
221 NULL)) {
222 return NT_STATUS_NO_MEMORY;
225 if (get_current_uid(conn) == (uid_t)0) {
226 /* I'm sorry sir, I didn't know you were root... */
227 DEBUG(10,("check_parent_access: root override "
228 "on %s. Granting 0x%x\n",
229 smb_fname_str_dbg(smb_fname),
230 (unsigned int)access_mask ));
231 return NT_STATUS_OK;
234 status = SMB_VFS_GET_NT_ACL(conn,
235 parent_dir,
236 SECINFO_DACL,
237 &parent_sd);
239 if (!NT_STATUS_IS_OK(status)) {
240 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
241 "%s with error %s\n",
242 parent_dir,
243 nt_errstr(status)));
244 return status;
248 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
249 * owner WRITE_DAC and READ_CONTROL.
251 status = se_access_check(parent_sd,
252 get_current_nttok(conn),
253 (access_mask & ~FILE_READ_ATTRIBUTES),
254 &access_granted);
255 if(!NT_STATUS_IS_OK(status)) {
256 DEBUG(5,("check_parent_access: access check "
257 "on directory %s for "
258 "path %s for mask 0x%x returned (0x%x) %s\n",
259 parent_dir,
260 smb_fname->base_name,
261 access_mask,
262 access_granted,
263 nt_errstr(status) ));
264 return status;
267 return NT_STATUS_OK;
270 /****************************************************************************
271 fd support routines - attempt to do a dos_open.
272 ****************************************************************************/
274 static NTSTATUS fd_open(struct connection_struct *conn,
275 files_struct *fsp,
276 int flags,
277 mode_t mode)
279 struct smb_filename *smb_fname = fsp->fsp_name;
280 NTSTATUS status = NT_STATUS_OK;
282 #ifdef O_NOFOLLOW
284 * Never follow symlinks on a POSIX client. The
285 * client should be doing this.
288 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
289 flags |= O_NOFOLLOW;
291 #endif
293 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
294 if (fsp->fh->fd == -1) {
295 int posix_errno = errno;
296 #ifdef O_NOFOLLOW
297 #if defined(ENOTSUP) && defined(OSF1)
298 /* handle special Tru64 errno */
299 if (errno == ENOTSUP) {
300 posix_errno = ELOOP;
302 #endif /* ENOTSUP */
303 #ifdef EFTYPE
304 /* fix broken NetBSD errno */
305 if (errno == EFTYPE) {
306 posix_errno = ELOOP;
308 #endif /* EFTYPE */
309 /* fix broken FreeBSD errno */
310 if (errno == EMLINK) {
311 posix_errno = ELOOP;
313 #endif /* O_NOFOLLOW */
314 status = map_nt_error_from_unix(posix_errno);
315 if (errno == EMFILE) {
316 static time_t last_warned = 0L;
318 if (time((time_t *) NULL) > last_warned) {
319 DEBUG(0,("Too many open files, unable "
320 "to open more! smbd's max "
321 "open files = %d\n",
322 lp_max_open_files()));
323 last_warned = time((time_t *) NULL);
329 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
330 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
331 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
333 return status;
336 /****************************************************************************
337 Close the file associated with a fsp.
338 ****************************************************************************/
340 NTSTATUS fd_close(files_struct *fsp)
342 int ret;
344 if (fsp->dptr) {
345 dptr_CloseDir(fsp);
347 if (fsp->fh->fd == -1) {
348 return NT_STATUS_OK; /* What we used to call a stat open. */
350 if (fsp->fh->ref_count > 1) {
351 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
354 ret = SMB_VFS_CLOSE(fsp);
355 fsp->fh->fd = -1;
356 if (ret == -1) {
357 return map_nt_error_from_unix(errno);
359 return NT_STATUS_OK;
362 /****************************************************************************
363 Change the ownership of a file to that of the parent directory.
364 Do this by fd if possible.
365 ****************************************************************************/
367 void change_file_owner_to_parent(connection_struct *conn,
368 const char *inherit_from_dir,
369 files_struct *fsp)
371 struct smb_filename *smb_fname_parent = NULL;
372 NTSTATUS status;
373 int ret;
375 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
376 NULL, NULL, &smb_fname_parent);
377 if (!NT_STATUS_IS_OK(status)) {
378 return;
381 ret = SMB_VFS_STAT(conn, smb_fname_parent);
382 if (ret == -1) {
383 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
384 "directory %s. Error was %s\n",
385 smb_fname_str_dbg(smb_fname_parent),
386 strerror(errno)));
387 TALLOC_FREE(smb_fname_parent);
388 return;
391 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
392 /* Already this uid - no need to change. */
393 DEBUG(10,("change_file_owner_to_parent: file %s "
394 "is already owned by uid %d\n",
395 fsp_str_dbg(fsp),
396 (int)fsp->fsp_name->st.st_ex_uid ));
397 TALLOC_FREE(smb_fname_parent);
398 return;
401 become_root();
402 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
403 unbecome_root();
404 if (ret == -1) {
405 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
406 "file %s to parent directory uid %u. Error "
407 "was %s\n", fsp_str_dbg(fsp),
408 (unsigned int)smb_fname_parent->st.st_ex_uid,
409 strerror(errno) ));
410 } else {
411 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
412 "parent directory uid %u.\n", fsp_str_dbg(fsp),
413 (unsigned int)smb_fname_parent->st.st_ex_uid));
414 /* Ensure the uid entry is updated. */
415 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
418 TALLOC_FREE(smb_fname_parent);
421 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
422 const char *inherit_from_dir,
423 const char *fname,
424 SMB_STRUCT_STAT *psbuf)
426 struct smb_filename *smb_fname_parent = NULL;
427 struct smb_filename *smb_fname_cwd = NULL;
428 char *saved_dir = NULL;
429 TALLOC_CTX *ctx = talloc_tos();
430 NTSTATUS status = NT_STATUS_OK;
431 int ret;
433 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
434 &smb_fname_parent);
435 if (!NT_STATUS_IS_OK(status)) {
436 return status;
439 ret = SMB_VFS_STAT(conn, smb_fname_parent);
440 if (ret == -1) {
441 status = map_nt_error_from_unix(errno);
442 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
443 "directory %s. Error was %s\n",
444 smb_fname_str_dbg(smb_fname_parent),
445 strerror(errno)));
446 goto out;
449 /* We've already done an lstat into psbuf, and we know it's a
450 directory. If we can cd into the directory and the dev/ino
451 are the same then we can safely chown without races as
452 we're locking the directory in place by being in it. This
453 should work on any UNIX (thanks tridge :-). JRA.
456 saved_dir = vfs_GetWd(ctx,conn);
457 if (!saved_dir) {
458 status = map_nt_error_from_unix(errno);
459 DEBUG(0,("change_dir_owner_to_parent: failed to get "
460 "current working directory. Error was %s\n",
461 strerror(errno)));
462 goto out;
465 /* Chdir into the new path. */
466 if (vfs_ChDir(conn, fname) == -1) {
467 status = map_nt_error_from_unix(errno);
468 DEBUG(0,("change_dir_owner_to_parent: failed to change "
469 "current working directory to %s. Error "
470 "was %s\n", fname, strerror(errno) ));
471 goto chdir;
474 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
475 &smb_fname_cwd);
476 if (!NT_STATUS_IS_OK(status)) {
477 return status;
480 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
481 if (ret == -1) {
482 status = map_nt_error_from_unix(errno);
483 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
484 "directory '.' (%s) Error was %s\n",
485 fname, strerror(errno)));
486 goto chdir;
489 /* Ensure we're pointing at the same place. */
490 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
491 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
492 DEBUG(0,("change_dir_owner_to_parent: "
493 "device/inode on directory %s changed. "
494 "Refusing to chown !\n", fname ));
495 status = NT_STATUS_ACCESS_DENIED;
496 goto chdir;
499 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
500 /* Already this uid - no need to change. */
501 DEBUG(10,("change_dir_owner_to_parent: directory %s "
502 "is already owned by uid %d\n",
503 fname,
504 (int)smb_fname_cwd->st.st_ex_uid ));
505 status = NT_STATUS_OK;
506 goto chdir;
509 become_root();
510 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
511 (gid_t)-1);
512 unbecome_root();
513 if (ret == -1) {
514 status = map_nt_error_from_unix(errno);
515 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
516 "directory %s to parent directory uid %u. "
517 "Error was %s\n", fname,
518 (unsigned int)smb_fname_parent->st.st_ex_uid,
519 strerror(errno) ));
520 } else {
521 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
522 "directory %s to parent directory uid %u.\n",
523 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
524 /* Ensure the uid entry is updated. */
525 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
528 chdir:
529 vfs_ChDir(conn,saved_dir);
530 out:
531 TALLOC_FREE(smb_fname_parent);
532 TALLOC_FREE(smb_fname_cwd);
533 return status;
536 /****************************************************************************
537 Open a file.
538 ****************************************************************************/
540 static NTSTATUS open_file(files_struct *fsp,
541 connection_struct *conn,
542 struct smb_request *req,
543 const char *parent_dir,
544 int flags,
545 mode_t unx_mode,
546 uint32 access_mask, /* client requested access mask. */
547 uint32 open_access_mask) /* what we're actually using in the open. */
549 struct smb_filename *smb_fname = fsp->fsp_name;
550 NTSTATUS status = NT_STATUS_OK;
551 int accmode = (flags & O_ACCMODE);
552 int local_flags = flags;
553 bool file_existed = VALID_STAT(fsp->fsp_name->st);
554 bool file_created = false;
556 fsp->fh->fd = -1;
557 errno = EPERM;
559 /* Check permissions */
562 * This code was changed after seeing a client open request
563 * containing the open mode of (DENY_WRITE/read-only) with
564 * the 'create if not exist' bit set. The previous code
565 * would fail to open the file read only on a read-only share
566 * as it was checking the flags parameter directly against O_RDONLY,
567 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
568 * JRA.
571 if (!CAN_WRITE(conn)) {
572 /* It's a read-only share - fail if we wanted to write. */
573 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
574 DEBUG(3,("Permission denied opening %s\n",
575 smb_fname_str_dbg(smb_fname)));
576 return NT_STATUS_ACCESS_DENIED;
577 } else if(flags & O_CREAT) {
578 /* We don't want to write - but we must make sure that
579 O_CREAT doesn't create the file if we have write
580 access into the directory.
582 flags &= ~(O_CREAT|O_EXCL);
583 local_flags &= ~(O_CREAT|O_EXCL);
588 * This little piece of insanity is inspired by the
589 * fact that an NT client can open a file for O_RDONLY,
590 * but set the create disposition to FILE_EXISTS_TRUNCATE.
591 * If the client *can* write to the file, then it expects to
592 * truncate the file, even though it is opening for readonly.
593 * Quicken uses this stupid trick in backup file creation...
594 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
595 * for helping track this one down. It didn't bite us in 2.0.x
596 * as we always opened files read-write in that release. JRA.
599 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
600 DEBUG(10,("open_file: truncate requested on read-only open "
601 "for file %s\n", smb_fname_str_dbg(smb_fname)));
602 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
605 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
606 (!file_existed && (local_flags & O_CREAT)) ||
607 ((local_flags & O_TRUNC) == O_TRUNC) ) {
608 const char *wild;
611 * We can't actually truncate here as the file may be locked.
612 * open_file_ntcreate will take care of the truncate later. JRA.
615 local_flags &= ~O_TRUNC;
617 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
619 * We would block on opening a FIFO with no one else on the
620 * other end. Do what we used to do and add O_NONBLOCK to the
621 * open flags. JRA.
624 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
625 local_flags |= O_NONBLOCK;
627 #endif
629 /* Don't create files with Microsoft wildcard characters. */
630 if (fsp->base_fsp) {
632 * wildcard characters are allowed in stream names
633 * only test the basefilename
635 wild = fsp->base_fsp->fsp_name->base_name;
636 } else {
637 wild = smb_fname->base_name;
639 if ((local_flags & O_CREAT) && !file_existed &&
640 ms_has_wild(wild)) {
641 return NT_STATUS_OBJECT_NAME_INVALID;
644 /* Can we access this file ? */
645 if (!fsp->base_fsp) {
646 /* Only do this check on non-stream open. */
647 if (file_existed) {
648 status = smbd_check_access_rights(conn,
649 smb_fname,
650 access_mask);
651 } else if (local_flags & O_CREAT){
652 status = check_parent_access(conn,
653 smb_fname,
654 SEC_DIR_ADD_FILE);
655 } else {
656 /* File didn't exist and no O_CREAT. */
657 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
659 if (!NT_STATUS_IS_OK(status)) {
660 DEBUG(10,("open_file: "
661 "%s on file "
662 "%s returned %s\n",
663 file_existed ?
664 "smbd_check_access_rights" :
665 "check_parent_access",
666 smb_fname_str_dbg(smb_fname),
667 nt_errstr(status) ));
668 return status;
672 /* Actually do the open */
673 status = fd_open(conn, fsp, local_flags, unx_mode);
674 if (!NT_STATUS_IS_OK(status)) {
675 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
676 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
677 nt_errstr(status),local_flags,flags));
678 return status;
681 if ((local_flags & O_CREAT) && !file_existed) {
682 file_created = true;
685 } else {
686 fsp->fh->fd = -1; /* What we used to call a stat open. */
687 if (!file_existed) {
688 /* File must exist for a stat open. */
689 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
692 status = smbd_check_access_rights(conn,
693 smb_fname,
694 access_mask);
696 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
697 fsp->posix_open &&
698 S_ISLNK(smb_fname->st.st_ex_mode)) {
699 /* This is a POSIX stat open for delete
700 * or rename on a symlink that points
701 * nowhere. Allow. */
702 DEBUG(10,("open_file: allowing POSIX "
703 "open on bad symlink %s\n",
704 smb_fname_str_dbg(smb_fname)));
705 status = NT_STATUS_OK;
708 if (!NT_STATUS_IS_OK(status)) {
709 DEBUG(10,("open_file: "
710 "smbd_check_access_rights on file "
711 "%s returned %s\n",
712 smb_fname_str_dbg(smb_fname),
713 nt_errstr(status) ));
714 return status;
718 if (!file_existed) {
719 int ret;
721 if (fsp->fh->fd == -1) {
722 ret = SMB_VFS_STAT(conn, smb_fname);
723 } else {
724 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
725 /* If we have an fd, this stat should succeed. */
726 if (ret == -1) {
727 DEBUG(0,("Error doing fstat on open file %s "
728 "(%s)\n",
729 smb_fname_str_dbg(smb_fname),
730 strerror(errno) ));
734 /* For a non-io open, this stat failing means file not found. JRA */
735 if (ret == -1) {
736 status = map_nt_error_from_unix(errno);
737 fd_close(fsp);
738 return status;
741 if (file_created) {
742 bool need_re_stat = false;
743 /* Do all inheritance work after we've
744 done a successful stat call and filled
745 in the stat struct in fsp->fsp_name. */
747 /* Inherit the ACL if required */
748 if (lp_inherit_perms(SNUM(conn))) {
749 inherit_access_posix_acl(conn, parent_dir,
750 smb_fname->base_name,
751 unx_mode);
752 need_re_stat = true;
755 /* Change the owner if required. */
756 if (lp_inherit_owner(SNUM(conn))) {
757 change_file_owner_to_parent(conn, parent_dir,
758 fsp);
759 need_re_stat = true;
762 if (need_re_stat) {
763 if (fsp->fh->fd == -1) {
764 ret = SMB_VFS_STAT(conn, smb_fname);
765 } else {
766 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
767 /* If we have an fd, this stat should succeed. */
768 if (ret == -1) {
769 DEBUG(0,("Error doing fstat on open file %s "
770 "(%s)\n",
771 smb_fname_str_dbg(smb_fname),
772 strerror(errno) ));
777 notify_fname(conn, NOTIFY_ACTION_ADDED,
778 FILE_NOTIFY_CHANGE_FILE_NAME,
779 smb_fname->base_name);
784 * POSIX allows read-only opens of directories. We don't
785 * want to do this (we use a different code path for this)
786 * so catch a directory open and return an EISDIR. JRA.
789 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
790 fd_close(fsp);
791 errno = EISDIR;
792 return NT_STATUS_FILE_IS_A_DIRECTORY;
795 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
796 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
797 fsp->file_pid = req ? req->smbpid : 0;
798 fsp->can_lock = True;
799 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
800 fsp->can_write =
801 CAN_WRITE(conn) &&
802 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
803 fsp->print_file = NULL;
804 fsp->modified = False;
805 fsp->sent_oplock_break = NO_BREAK_SENT;
806 fsp->is_directory = False;
807 if (conn->aio_write_behind_list &&
808 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
809 conn->case_sensitive)) {
810 fsp->aio_write_behind = True;
813 fsp->wcp = NULL; /* Write cache pointer. */
815 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
816 conn->session_info->unix_info->unix_name,
817 smb_fname_str_dbg(smb_fname),
818 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
819 conn->num_files_open));
821 errno = 0;
822 return NT_STATUS_OK;
825 /****************************************************************************
826 Check if we can open a file with a share mode.
827 Returns True if conflict, False if not.
828 ****************************************************************************/
830 static bool share_conflict(struct share_mode_entry *entry,
831 uint32 access_mask,
832 uint32 share_access)
834 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
835 "entry->share_access = 0x%x, "
836 "entry->private_options = 0x%x\n",
837 (unsigned int)entry->access_mask,
838 (unsigned int)entry->share_access,
839 (unsigned int)entry->private_options));
841 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
842 (unsigned int)access_mask, (unsigned int)share_access));
844 if ((entry->access_mask & (FILE_WRITE_DATA|
845 FILE_APPEND_DATA|
846 FILE_READ_DATA|
847 FILE_EXECUTE|
848 DELETE_ACCESS)) == 0) {
849 DEBUG(10,("share_conflict: No conflict due to "
850 "entry->access_mask = 0x%x\n",
851 (unsigned int)entry->access_mask ));
852 return False;
855 if ((access_mask & (FILE_WRITE_DATA|
856 FILE_APPEND_DATA|
857 FILE_READ_DATA|
858 FILE_EXECUTE|
859 DELETE_ACCESS)) == 0) {
860 DEBUG(10,("share_conflict: No conflict due to "
861 "access_mask = 0x%x\n",
862 (unsigned int)access_mask ));
863 return False;
866 #if 1 /* JRA TEST - Superdebug. */
867 #define CHECK_MASK(num, am, right, sa, share) \
868 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
869 (unsigned int)(num), (unsigned int)(am), \
870 (unsigned int)(right), (unsigned int)(am)&(right) )); \
871 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
872 (unsigned int)(num), (unsigned int)(sa), \
873 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
874 if (((am) & (right)) && !((sa) & (share))) { \
875 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
876 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
877 (unsigned int)(share) )); \
878 return True; \
880 #else
881 #define CHECK_MASK(num, am, right, sa, share) \
882 if (((am) & (right)) && !((sa) & (share))) { \
883 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
884 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
885 (unsigned int)(share) )); \
886 return True; \
888 #endif
890 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
891 share_access, FILE_SHARE_WRITE);
892 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
893 entry->share_access, FILE_SHARE_WRITE);
895 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
896 share_access, FILE_SHARE_READ);
897 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
898 entry->share_access, FILE_SHARE_READ);
900 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
901 share_access, FILE_SHARE_DELETE);
902 CHECK_MASK(6, access_mask, DELETE_ACCESS,
903 entry->share_access, FILE_SHARE_DELETE);
905 DEBUG(10,("share_conflict: No conflict.\n"));
906 return False;
909 #if defined(DEVELOPER)
910 static void validate_my_share_entries(struct smbd_server_connection *sconn,
911 int num,
912 struct share_mode_entry *share_entry)
914 struct server_id self = messaging_server_id(sconn->msg_ctx);
915 files_struct *fsp;
917 if (!serverid_equal(&self, &share_entry->pid)) {
918 return;
921 if (is_deferred_open_entry(share_entry) &&
922 !open_was_deferred(sconn, share_entry->op_mid)) {
923 char *str = talloc_asprintf(talloc_tos(),
924 "Got a deferred entry without a request: "
925 "PANIC: %s\n",
926 share_mode_str(talloc_tos(), num, share_entry));
927 smb_panic(str);
930 if (!is_valid_share_mode_entry(share_entry)) {
931 return;
934 fsp = file_find_dif(sconn, share_entry->id,
935 share_entry->share_file_id);
936 if (!fsp) {
937 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
938 share_mode_str(talloc_tos(), num, share_entry) ));
939 smb_panic("validate_my_share_entries: Cannot match a "
940 "share entry with an open file\n");
943 if (is_deferred_open_entry(share_entry)) {
944 goto panic;
947 if ((share_entry->op_type == NO_OPLOCK) &&
948 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
949 /* Someone has already written to it, but I haven't yet
950 * noticed */
951 return;
954 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
955 goto panic;
958 return;
960 panic:
962 char *str;
963 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
964 share_mode_str(talloc_tos(), num, share_entry) ));
965 str = talloc_asprintf(talloc_tos(),
966 "validate_my_share_entries: "
967 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
968 fsp->fsp_name->base_name,
969 (unsigned int)fsp->oplock_type,
970 (unsigned int)share_entry->op_type );
971 smb_panic(str);
974 #endif
976 bool is_stat_open(uint32 access_mask)
978 return (access_mask &&
979 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
980 FILE_WRITE_ATTRIBUTES))==0) &&
981 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
982 FILE_WRITE_ATTRIBUTES)) != 0));
985 /****************************************************************************
986 Deal with share modes
987 Invarient: Share mode must be locked on entry and exit.
988 Returns -1 on error, or number of share modes on success (may be zero).
989 ****************************************************************************/
991 static NTSTATUS open_mode_check(connection_struct *conn,
992 struct share_mode_lock *lck,
993 uint32_t name_hash,
994 uint32 access_mask,
995 uint32 share_access,
996 uint32 create_options,
997 bool *file_existed)
999 int i;
1001 if(lck->data->num_share_modes == 0) {
1002 return NT_STATUS_OK;
1005 /* A delete on close prohibits everything */
1007 if (is_delete_on_close_set(lck, name_hash)) {
1009 * Check the delete on close token
1010 * is valid. It could have been left
1011 * after a server crash.
1013 for(i = 0; i < lck->data->num_share_modes; i++) {
1014 if (!share_mode_stale_pid(lck->data, i)) {
1016 *file_existed = true;
1018 return NT_STATUS_DELETE_PENDING;
1021 return NT_STATUS_OK;
1024 if (is_stat_open(access_mask)) {
1025 /* Stat open that doesn't trigger oplock breaks or share mode
1026 * checks... ! JRA. */
1027 return NT_STATUS_OK;
1031 * Check if the share modes will give us access.
1034 #if defined(DEVELOPER)
1035 for(i = 0; i < lck->data->num_share_modes; i++) {
1036 validate_my_share_entries(conn->sconn, i,
1037 &lck->data->share_modes[i]);
1039 #endif
1041 /* Now we check the share modes, after any oplock breaks. */
1042 for(i = 0; i < lck->data->num_share_modes; i++) {
1044 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1045 continue;
1048 /* someone else has a share lock on it, check to see if we can
1049 * too */
1050 if (share_conflict(&lck->data->share_modes[i],
1051 access_mask, share_access)) {
1053 if (share_mode_stale_pid(lck->data, i)) {
1054 continue;
1057 *file_existed = true;
1059 return NT_STATUS_SHARING_VIOLATION;
1063 if (lck->data->num_share_modes != 0) {
1064 *file_existed = true;
1067 return NT_STATUS_OK;
1070 static bool is_delete_request(files_struct *fsp) {
1071 return ((fsp->access_mask == DELETE_ACCESS) &&
1072 (fsp->oplock_type == NO_OPLOCK));
1076 * Send a break message to the oplock holder and delay the open for
1077 * our client.
1080 static NTSTATUS send_break_message(files_struct *fsp,
1081 struct share_mode_entry *exclusive,
1082 uint64_t mid,
1083 int oplock_request)
1085 NTSTATUS status;
1086 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1088 DEBUG(10, ("Sending break request to PID %s\n",
1089 procid_str_static(&exclusive->pid)));
1090 exclusive->op_mid = mid;
1092 /* Create the message. */
1093 share_mode_entry_to_message(msg, exclusive);
1095 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1096 don't want this set in the share mode struct pointed to by lck. */
1098 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1099 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1100 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1103 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1104 MSG_SMB_BREAK_REQUEST,
1105 (uint8 *)msg,
1106 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1107 if (!NT_STATUS_IS_OK(status)) {
1108 DEBUG(3, ("Could not send oplock break message: %s\n",
1109 nt_errstr(status)));
1112 return status;
1116 * Return share_mode_entry pointers for :
1117 * 1). Batch oplock entry.
1118 * 2). Batch or exclusive oplock entry (may be identical to #1).
1119 * bool have_level2_oplock
1120 * bool have_no_oplock.
1121 * Do internal consistency checks on the share mode for a file.
1124 static void find_oplock_types(files_struct *fsp,
1125 int oplock_request,
1126 const struct share_mode_lock *lck,
1127 struct share_mode_entry **pp_batch,
1128 struct share_mode_entry **pp_ex_or_batch,
1129 bool *got_level2,
1130 bool *got_no_oplock)
1132 int i;
1134 *pp_batch = NULL;
1135 *pp_ex_or_batch = NULL;
1136 *got_level2 = false;
1137 *got_no_oplock = false;
1139 /* Ignore stat or internal opens, as is done in
1140 delay_for_batch_oplocks() and
1141 delay_for_exclusive_oplocks().
1143 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1144 return;
1147 for (i=0; i<lck->data->num_share_modes; i++) {
1148 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1149 continue;
1152 if (lck->data->share_modes[i].op_type == NO_OPLOCK &&
1153 is_stat_open(lck->data->share_modes[i].access_mask)) {
1154 /* We ignore stat opens in the table - they
1155 always have NO_OPLOCK and never get or
1156 cause breaks. JRA. */
1157 continue;
1160 if (BATCH_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1161 /* batch - can only be one. */
1162 if (share_mode_stale_pid(lck->data, i)) {
1163 DEBUG(10, ("Found stale batch oplock\n"));
1164 continue;
1166 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1167 smb_panic("Bad batch oplock entry.");
1169 *pp_batch = &lck->data->share_modes[i];
1172 if (EXCLUSIVE_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1173 if (share_mode_stale_pid(lck->data, i)) {
1174 DEBUG(10, ("Found stale duplicate oplock\n"));
1175 continue;
1177 /* Exclusive or batch - can only be one. */
1178 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1179 smb_panic("Bad exclusive or batch oplock entry.");
1181 *pp_ex_or_batch = &lck->data->share_modes[i];
1184 if (LEVEL_II_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1185 if (*pp_batch || *pp_ex_or_batch) {
1186 if (share_mode_stale_pid(lck->data, i)) {
1187 DEBUG(10, ("Found stale LevelII "
1188 "oplock\n"));
1189 continue;
1191 smb_panic("Bad levelII oplock entry.");
1193 *got_level2 = true;
1196 if (lck->data->share_modes[i].op_type == NO_OPLOCK) {
1197 if (*pp_batch || *pp_ex_or_batch) {
1198 if (share_mode_stale_pid(lck->data, i)) {
1199 DEBUG(10, ("Found stale NO_OPLOCK "
1200 "entry\n"));
1201 continue;
1203 smb_panic("Bad no oplock entry.");
1205 *got_no_oplock = true;
1210 static bool delay_for_batch_oplocks(files_struct *fsp,
1211 uint64_t mid,
1212 int oplock_request,
1213 struct share_mode_entry *batch_entry)
1215 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1216 return false;
1218 if (batch_entry == NULL) {
1219 return false;
1222 /* Found a batch oplock */
1223 send_break_message(fsp, batch_entry, mid, oplock_request);
1224 return true;
1227 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1228 uint64_t mid,
1229 int oplock_request,
1230 struct share_mode_entry *ex_entry)
1232 bool delay_it;
1234 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1235 return false;
1237 if (ex_entry == NULL) {
1238 return false;
1241 /* Found an exclusive or batch oplock */
1243 delay_it = is_delete_request(fsp) ?
1244 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1246 if (!delay_it) {
1247 return false;
1250 send_break_message(fsp, ex_entry, mid, oplock_request);
1251 return true;
1254 static bool file_has_brlocks(files_struct *fsp)
1256 struct byte_range_lock *br_lck;
1258 br_lck = brl_get_locks_readonly(fsp);
1259 if (!br_lck)
1260 return false;
1262 return br_lck->num_locks > 0 ? true : false;
1265 static void grant_fsp_oplock_type(files_struct *fsp,
1266 int oplock_request,
1267 bool got_level2_oplock,
1268 bool got_a_none_oplock)
1270 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1271 lp_level2_oplocks(SNUM(fsp->conn));
1273 /* Start by granting what the client asked for,
1274 but ensure no SAMBA_PRIVATE bits can be set. */
1275 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1277 if (oplock_request & INTERNAL_OPEN_ONLY) {
1278 /* No oplocks on internal open. */
1279 fsp->oplock_type = NO_OPLOCK;
1280 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1281 fsp->oplock_type, fsp_str_dbg(fsp)));
1282 return;
1285 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1286 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1287 fsp_str_dbg(fsp)));
1288 fsp->oplock_type = NO_OPLOCK;
1291 if (is_stat_open(fsp->access_mask)) {
1292 /* Leave the value already set. */
1293 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1294 fsp->oplock_type, fsp_str_dbg(fsp)));
1295 return;
1299 * Match what was requested (fsp->oplock_type) with
1300 * what was found in the existing share modes.
1303 if (got_a_none_oplock) {
1304 fsp->oplock_type = NO_OPLOCK;
1305 } else if (got_level2_oplock) {
1306 if (fsp->oplock_type == NO_OPLOCK ||
1307 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1308 /* Store a level2 oplock, but don't tell the client */
1309 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1310 } else {
1311 fsp->oplock_type = LEVEL_II_OPLOCK;
1313 } else {
1314 /* All share_mode_entries are placeholders or deferred.
1315 * Silently upgrade to fake levelII if the client didn't
1316 * ask for an oplock. */
1317 if (fsp->oplock_type == NO_OPLOCK) {
1318 /* Store a level2 oplock, but don't tell the client */
1319 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1324 * Don't grant level2 to clients that don't want them
1325 * or if we've turned them off.
1327 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1328 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1331 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1332 fsp->oplock_type, fsp_str_dbg(fsp)));
1335 bool request_timed_out(struct timeval request_time,
1336 struct timeval timeout)
1338 struct timeval now, end_time;
1339 GetTimeOfDay(&now);
1340 end_time = timeval_sum(&request_time, &timeout);
1341 return (timeval_compare(&end_time, &now) < 0);
1344 /****************************************************************************
1345 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1346 ****************************************************************************/
1348 static void defer_open(struct share_mode_lock *lck,
1349 struct timeval request_time,
1350 struct timeval timeout,
1351 struct smb_request *req,
1352 struct deferred_open_record *state)
1354 struct server_id self = messaging_server_id(req->sconn->msg_ctx);
1356 /* Paranoia check */
1358 if (lck) {
1359 int i;
1361 for (i=0; i<lck->data->num_share_modes; i++) {
1362 struct share_mode_entry *e = &lck->data->share_modes[i];
1364 if (is_deferred_open_entry(e) &&
1365 serverid_equal(&self, &e->pid) &&
1366 (e->op_mid == req->mid)) {
1367 DEBUG(0, ("Trying to defer an already deferred "
1368 "request: mid=%llu, exiting\n",
1369 (unsigned long long)req->mid));
1370 exit_server("attempt to defer a deferred request");
1375 /* End paranoia check */
1377 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1378 "open entry for mid %llu\n",
1379 (unsigned int)request_time.tv_sec,
1380 (unsigned int)request_time.tv_usec,
1381 (unsigned long long)req->mid));
1383 if (!push_deferred_open_message_smb(req, request_time, timeout,
1384 state->id, (char *)state, sizeof(*state))) {
1385 exit_server("push_deferred_open_message_smb failed");
1387 if (lck) {
1388 add_deferred_open(lck, req->mid, request_time, self, state->id);
1393 /****************************************************************************
1394 On overwrite open ensure that the attributes match.
1395 ****************************************************************************/
1397 bool open_match_attributes(connection_struct *conn,
1398 uint32 old_dos_attr,
1399 uint32 new_dos_attr,
1400 mode_t existing_unx_mode,
1401 mode_t new_unx_mode,
1402 mode_t *returned_unx_mode)
1404 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1406 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1407 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1409 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1410 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1411 *returned_unx_mode = new_unx_mode;
1412 } else {
1413 *returned_unx_mode = (mode_t)0;
1416 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1417 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1418 "returned_unx_mode = 0%o\n",
1419 (unsigned int)old_dos_attr,
1420 (unsigned int)existing_unx_mode,
1421 (unsigned int)new_dos_attr,
1422 (unsigned int)*returned_unx_mode ));
1424 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1425 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1426 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1427 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1428 return False;
1431 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1432 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1433 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1434 return False;
1437 return True;
1440 /****************************************************************************
1441 Special FCB or DOS processing in the case of a sharing violation.
1442 Try and find a duplicated file handle.
1443 ****************************************************************************/
1445 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1446 connection_struct *conn,
1447 files_struct *fsp_to_dup_into,
1448 const struct smb_filename *smb_fname,
1449 struct file_id id,
1450 uint16 file_pid,
1451 uint64_t vuid,
1452 uint32 access_mask,
1453 uint32 share_access,
1454 uint32 create_options)
1456 files_struct *fsp;
1458 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1459 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1461 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1462 fsp = file_find_di_next(fsp)) {
1464 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1465 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1466 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1467 fsp->fh->fd, (unsigned long long)fsp->vuid,
1468 (unsigned int)fsp->file_pid,
1469 (unsigned int)fsp->fh->private_options,
1470 (unsigned int)fsp->access_mask ));
1472 if (fsp->fh->fd != -1 &&
1473 fsp->vuid == vuid &&
1474 fsp->file_pid == file_pid &&
1475 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1476 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1477 (fsp->access_mask & FILE_WRITE_DATA) &&
1478 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1479 strequal(fsp->fsp_name->stream_name,
1480 smb_fname->stream_name)) {
1481 DEBUG(10,("fcb_or_dos_open: file match\n"));
1482 break;
1486 if (!fsp) {
1487 return NT_STATUS_NOT_FOUND;
1490 /* quite an insane set of semantics ... */
1491 if (is_executable(smb_fname->base_name) &&
1492 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1493 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1494 return NT_STATUS_INVALID_PARAMETER;
1497 /* We need to duplicate this fsp. */
1498 return dup_file_fsp(req, fsp, access_mask, share_access,
1499 create_options, fsp_to_dup_into);
1502 static void schedule_defer_open(struct share_mode_lock *lck,
1503 struct timeval request_time,
1504 struct smb_request *req)
1506 struct deferred_open_record state;
1508 /* This is a relative time, added to the absolute
1509 request_time value to get the absolute timeout time.
1510 Note that if this is the second or greater time we enter
1511 this codepath for this particular request mid then
1512 request_time is left as the absolute time of the *first*
1513 time this request mid was processed. This is what allows
1514 the request to eventually time out. */
1516 struct timeval timeout;
1518 /* Normally the smbd we asked should respond within
1519 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1520 * the client did, give twice the timeout as a safety
1521 * measure here in case the other smbd is stuck
1522 * somewhere else. */
1524 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1526 /* Nothing actually uses state.delayed_for_oplocks
1527 but it's handy to differentiate in debug messages
1528 between a 30 second delay due to oplock break, and
1529 a 1 second delay for share mode conflicts. */
1531 state.delayed_for_oplocks = True;
1532 state.async_open = false;
1533 state.id = lck->data->id;
1535 if (!request_timed_out(request_time, timeout)) {
1536 defer_open(lck, request_time, timeout, req, &state);
1540 /****************************************************************************
1541 Reschedule an open call that went asynchronous.
1542 ****************************************************************************/
1544 static void schedule_async_open(struct timeval request_time,
1545 struct smb_request *req)
1547 struct deferred_open_record state;
1548 struct timeval timeout;
1550 timeout = timeval_set(20, 0);
1552 ZERO_STRUCT(state);
1553 state.delayed_for_oplocks = false;
1554 state.async_open = true;
1556 if (!request_timed_out(request_time, timeout)) {
1557 defer_open(NULL, request_time, timeout, req, &state);
1561 /****************************************************************************
1562 Work out what access_mask to use from what the client sent us.
1563 ****************************************************************************/
1565 static NTSTATUS smbd_calculate_maximum_allowed_access(
1566 connection_struct *conn,
1567 const struct smb_filename *smb_fname,
1568 uint32_t *p_access_mask)
1570 struct security_descriptor *sd;
1571 uint32_t access_granted;
1572 NTSTATUS status;
1574 if (get_current_uid(conn) == (uid_t)0) {
1575 *p_access_mask |= FILE_GENERIC_ALL;
1576 return NT_STATUS_OK;
1579 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1580 (SECINFO_OWNER |
1581 SECINFO_GROUP |
1582 SECINFO_DACL),&sd);
1584 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1586 * File did not exist
1588 *p_access_mask = FILE_GENERIC_ALL;
1589 return NT_STATUS_OK;
1591 if (!NT_STATUS_IS_OK(status)) {
1592 DEBUG(10,("smbd_calculate_access_mask: "
1593 "Could not get acl on file %s: %s\n",
1594 smb_fname_str_dbg(smb_fname),
1595 nt_errstr(status)));
1596 return NT_STATUS_ACCESS_DENIED;
1600 * Never test FILE_READ_ATTRIBUTES. se_access_check()
1601 * also takes care of owner WRITE_DAC and READ_CONTROL.
1603 status = se_access_check(sd,
1604 get_current_nttok(conn),
1605 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1606 &access_granted);
1608 TALLOC_FREE(sd);
1610 if (!NT_STATUS_IS_OK(status)) {
1611 DEBUG(10, ("smbd_calculate_access_mask: "
1612 "Access denied on file %s: "
1613 "when calculating maximum access\n",
1614 smb_fname_str_dbg(smb_fname)));
1615 return NT_STATUS_ACCESS_DENIED;
1617 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1618 return NT_STATUS_OK;
1621 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1622 const struct smb_filename *smb_fname,
1623 uint32_t access_mask,
1624 uint32_t *access_mask_out)
1626 NTSTATUS status;
1627 uint32_t orig_access_mask = access_mask;
1628 uint32_t rejected_share_access;
1631 * Convert GENERIC bits to specific bits.
1634 se_map_generic(&access_mask, &file_generic_mapping);
1636 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1637 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1639 status = smbd_calculate_maximum_allowed_access(
1640 conn, smb_fname, &access_mask);
1642 if (!NT_STATUS_IS_OK(status)) {
1643 return status;
1646 access_mask &= conn->share_access;
1649 rejected_share_access = access_mask & ~(conn->share_access);
1651 if (rejected_share_access) {
1652 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1653 "file %s: rejected by share access mask[0x%08X] "
1654 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1655 smb_fname_str_dbg(smb_fname),
1656 conn->share_access,
1657 orig_access_mask, access_mask,
1658 rejected_share_access));
1659 return NT_STATUS_ACCESS_DENIED;
1662 *access_mask_out = access_mask;
1663 return NT_STATUS_OK;
1666 /****************************************************************************
1667 Remove the deferred open entry under lock.
1668 ****************************************************************************/
1670 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1671 struct server_id pid)
1673 struct share_mode_lock *lck = get_existing_share_mode_lock(
1674 talloc_tos(), id);
1675 if (lck == NULL) {
1676 DEBUG(0, ("could not get share mode lock\n"));
1677 return;
1679 del_deferred_open_entry(lck, mid, pid);
1680 TALLOC_FREE(lck);
1683 /****************************************************************************
1684 Return true if this is a state pointer to an asynchronous create.
1685 ****************************************************************************/
1687 bool is_deferred_open_async(const void *ptr)
1689 const struct deferred_open_record *state = (const struct deferred_open_record *)ptr;
1691 return state->async_open;
1694 /****************************************************************************
1695 Open a file with a share mode. Passed in an already created files_struct *.
1696 ****************************************************************************/
1698 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1699 struct smb_request *req,
1700 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1701 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1702 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1703 uint32 create_options, /* options such as delete on close. */
1704 uint32 new_dos_attributes, /* attributes used for new file. */
1705 int oplock_request, /* internal Samba oplock codes. */
1706 /* Information (FILE_EXISTS etc.) */
1707 uint32_t private_flags, /* Samba specific flags. */
1708 int *pinfo,
1709 files_struct *fsp)
1711 struct smb_filename *smb_fname = fsp->fsp_name;
1712 int flags=0;
1713 int flags2=0;
1714 bool file_existed = VALID_STAT(smb_fname->st);
1715 bool def_acl = False;
1716 bool posix_open = False;
1717 bool new_file_created = False;
1718 bool clear_ads = false;
1719 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1720 mode_t new_unx_mode = (mode_t)0;
1721 mode_t unx_mode = (mode_t)0;
1722 int info;
1723 uint32 existing_dos_attributes = 0;
1724 struct timeval request_time = timeval_zero();
1725 struct share_mode_lock *lck = NULL;
1726 uint32 open_access_mask = access_mask;
1727 NTSTATUS status;
1728 char *parent_dir;
1730 if (conn->printer) {
1732 * Printers are handled completely differently.
1733 * Most of the passed parameters are ignored.
1736 if (pinfo) {
1737 *pinfo = FILE_WAS_CREATED;
1740 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1741 smb_fname_str_dbg(smb_fname)));
1743 if (!req) {
1744 DEBUG(0,("open_file_ntcreate: printer open without "
1745 "an SMB request!\n"));
1746 return NT_STATUS_INTERNAL_ERROR;
1749 return print_spool_open(fsp, smb_fname->base_name,
1750 req->vuid);
1753 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1754 NULL)) {
1755 return NT_STATUS_NO_MEMORY;
1758 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1759 posix_open = True;
1760 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1761 new_dos_attributes = 0;
1762 } else {
1763 /* Windows allows a new file to be created and
1764 silently removes a FILE_ATTRIBUTE_DIRECTORY
1765 sent by the client. Do the same. */
1767 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1769 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1770 * created new. */
1771 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1772 smb_fname, parent_dir);
1775 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1776 "access_mask=0x%x share_access=0x%x "
1777 "create_disposition = 0x%x create_options=0x%x "
1778 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1779 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1780 access_mask, share_access, create_disposition,
1781 create_options, (unsigned int)unx_mode, oplock_request,
1782 (unsigned int)private_flags));
1784 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1785 DEBUG(0, ("No smb request but not an internal only open!\n"));
1786 return NT_STATUS_INTERNAL_ERROR;
1790 * Only non-internal opens can be deferred at all
1793 if (req) {
1794 void *ptr;
1795 if (get_deferred_open_message_state(req,
1796 &request_time,
1797 &ptr)) {
1798 /* Remember the absolute time of the original
1799 request with this mid. We'll use it later to
1800 see if this has timed out. */
1802 /* If it was an async create retry, the file
1803 didn't exist. */
1805 if (is_deferred_open_async(ptr)) {
1806 SET_STAT_INVALID(smb_fname->st);
1807 file_existed = false;
1808 } else {
1809 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1810 /* Remove the deferred open entry under lock. */
1811 remove_deferred_open_entry(
1812 state->id, req->mid,
1813 messaging_server_id(req->sconn->msg_ctx));
1816 /* Ensure we don't reprocess this message. */
1817 remove_deferred_open_message_smb(req->sconn, req->mid);
1821 if (!posix_open) {
1822 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1823 if (file_existed) {
1824 existing_dos_attributes = dos_mode(conn, smb_fname);
1828 /* ignore any oplock requests if oplocks are disabled */
1829 if (!lp_oplocks(SNUM(conn)) ||
1830 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1831 /* Mask off everything except the private Samba bits. */
1832 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1835 /* this is for OS/2 long file names - say we don't support them */
1836 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1837 /* OS/2 Workplace shell fix may be main code stream in a later
1838 * release. */
1839 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1840 "supported.\n"));
1841 if (use_nt_status()) {
1842 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1844 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1847 switch( create_disposition ) {
1849 * Currently we're using FILE_SUPERSEDE as the same as
1850 * FILE_OVERWRITE_IF but they really are
1851 * different. FILE_SUPERSEDE deletes an existing file
1852 * (requiring delete access) then recreates it.
1854 case FILE_SUPERSEDE:
1855 /* If file exists replace/overwrite. If file doesn't
1856 * exist create. */
1857 flags2 |= (O_CREAT | O_TRUNC);
1858 clear_ads = true;
1859 break;
1861 case FILE_OVERWRITE_IF:
1862 /* If file exists replace/overwrite. If file doesn't
1863 * exist create. */
1864 flags2 |= (O_CREAT | O_TRUNC);
1865 clear_ads = true;
1866 break;
1868 case FILE_OPEN:
1869 /* If file exists open. If file doesn't exist error. */
1870 if (!file_existed) {
1871 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1872 "requested for file %s and file "
1873 "doesn't exist.\n",
1874 smb_fname_str_dbg(smb_fname)));
1875 errno = ENOENT;
1876 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1878 break;
1880 case FILE_OVERWRITE:
1881 /* If file exists overwrite. If file doesn't exist
1882 * error. */
1883 if (!file_existed) {
1884 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1885 "requested for file %s and file "
1886 "doesn't exist.\n",
1887 smb_fname_str_dbg(smb_fname) ));
1888 errno = ENOENT;
1889 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1891 flags2 |= O_TRUNC;
1892 clear_ads = true;
1893 break;
1895 case FILE_CREATE:
1896 /* If file exists error. If file doesn't exist
1897 * create. */
1898 if (file_existed) {
1899 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1900 "requested for file %s and file "
1901 "already exists.\n",
1902 smb_fname_str_dbg(smb_fname)));
1903 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1904 errno = EISDIR;
1905 } else {
1906 errno = EEXIST;
1908 return map_nt_error_from_unix(errno);
1910 flags2 |= (O_CREAT|O_EXCL);
1911 break;
1913 case FILE_OPEN_IF:
1914 /* If file exists open. If file doesn't exist
1915 * create. */
1916 flags2 |= O_CREAT;
1917 break;
1919 default:
1920 return NT_STATUS_INVALID_PARAMETER;
1923 /* We only care about matching attributes on file exists and
1924 * overwrite. */
1926 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1927 (create_disposition == FILE_OVERWRITE_IF))) {
1928 if (!open_match_attributes(conn, existing_dos_attributes,
1929 new_dos_attributes,
1930 smb_fname->st.st_ex_mode,
1931 unx_mode, &new_unx_mode)) {
1932 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1933 "for file %s (%x %x) (0%o, 0%o)\n",
1934 smb_fname_str_dbg(smb_fname),
1935 existing_dos_attributes,
1936 new_dos_attributes,
1937 (unsigned int)smb_fname->st.st_ex_mode,
1938 (unsigned int)unx_mode ));
1939 errno = EACCES;
1940 return NT_STATUS_ACCESS_DENIED;
1944 status = smbd_calculate_access_mask(conn, smb_fname,
1945 access_mask,
1946 &access_mask);
1947 if (!NT_STATUS_IS_OK(status)) {
1948 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1949 "on file %s returned %s\n",
1950 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1951 return status;
1954 open_access_mask = access_mask;
1956 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1957 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1960 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1961 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1962 access_mask));
1965 * Note that we ignore the append flag as append does not
1966 * mean the same thing under DOS and Unix.
1969 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1970 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1971 /* DENY_DOS opens are always underlying read-write on the
1972 file handle, no matter what the requested access mask
1973 says. */
1974 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1975 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1976 flags = O_RDWR;
1977 } else {
1978 flags = O_WRONLY;
1980 } else {
1981 flags = O_RDONLY;
1985 * Currently we only look at FILE_WRITE_THROUGH for create options.
1988 #if defined(O_SYNC)
1989 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1990 flags2 |= O_SYNC;
1992 #endif /* O_SYNC */
1994 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1995 flags2 |= O_APPEND;
1998 if (!posix_open && !CAN_WRITE(conn)) {
2000 * We should really return a permission denied error if either
2001 * O_CREAT or O_TRUNC are set, but for compatibility with
2002 * older versions of Samba we just AND them out.
2004 flags2 &= ~(O_CREAT|O_TRUNC);
2008 * Ensure we can't write on a read-only share or file.
2011 if (flags != O_RDONLY && file_existed &&
2012 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2013 DEBUG(5,("open_file_ntcreate: write access requested for "
2014 "file %s on read only %s\n",
2015 smb_fname_str_dbg(smb_fname),
2016 !CAN_WRITE(conn) ? "share" : "file" ));
2017 errno = EACCES;
2018 return NT_STATUS_ACCESS_DENIED;
2021 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2022 fsp->share_access = share_access;
2023 fsp->fh->private_options = private_flags;
2024 fsp->access_mask = open_access_mask; /* We change this to the
2025 * requested access_mask after
2026 * the open is done. */
2027 fsp->posix_open = posix_open;
2029 /* Ensure no SAMBA_PRIVATE bits can be set. */
2030 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2032 if (timeval_is_zero(&request_time)) {
2033 request_time = fsp->open_time;
2036 if (file_existed) {
2037 struct share_mode_entry *batch_entry = NULL;
2038 struct share_mode_entry *exclusive_entry = NULL;
2039 bool got_level2_oplock = false;
2040 bool got_a_none_oplock = false;
2041 struct file_id id;
2043 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2044 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2046 lck = get_share_mode_lock(talloc_tos(), id,
2047 conn->connectpath,
2048 smb_fname, &old_write_time);
2049 if (lck == NULL) {
2050 DEBUG(0, ("Could not get share mode lock\n"));
2051 return NT_STATUS_SHARING_VIOLATION;
2054 /* Get the types we need to examine. */
2055 find_oplock_types(fsp,
2056 oplock_request,
2057 lck,
2058 &batch_entry,
2059 &exclusive_entry,
2060 &got_level2_oplock,
2061 &got_a_none_oplock);
2063 /* First pass - send break only on batch oplocks. */
2064 if ((req != NULL) &&
2065 delay_for_batch_oplocks(fsp,
2066 req->mid,
2067 oplock_request,
2068 batch_entry)) {
2069 schedule_defer_open(lck, request_time, req);
2070 TALLOC_FREE(lck);
2071 return NT_STATUS_SHARING_VIOLATION;
2074 /* Use the client requested access mask here, not the one we
2075 * open with. */
2076 status = open_mode_check(conn, lck, fsp->name_hash,
2077 access_mask, share_access,
2078 create_options, &file_existed);
2080 if (NT_STATUS_IS_OK(status)) {
2081 /* We might be going to allow this open. Check oplock
2082 * status again. */
2083 /* Second pass - send break for both batch or
2084 * exclusive oplocks. */
2085 if ((req != NULL) &&
2086 delay_for_exclusive_oplocks(
2087 fsp,
2088 req->mid,
2089 oplock_request,
2090 exclusive_entry)) {
2091 schedule_defer_open(lck, request_time, req);
2092 TALLOC_FREE(lck);
2093 return NT_STATUS_SHARING_VIOLATION;
2097 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2098 /* DELETE_PENDING is not deferred for a second */
2099 TALLOC_FREE(lck);
2100 return status;
2103 grant_fsp_oplock_type(fsp,
2104 oplock_request,
2105 got_level2_oplock,
2106 got_a_none_oplock);
2108 if (!NT_STATUS_IS_OK(status)) {
2109 uint32 can_access_mask;
2110 bool can_access = True;
2112 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2114 /* Check if this can be done with the deny_dos and fcb
2115 * calls. */
2116 if (private_flags &
2117 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2118 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2119 if (req == NULL) {
2120 DEBUG(0, ("DOS open without an SMB "
2121 "request!\n"));
2122 TALLOC_FREE(lck);
2123 return NT_STATUS_INTERNAL_ERROR;
2126 /* Use the client requested access mask here,
2127 * not the one we open with. */
2128 status = fcb_or_dos_open(req,
2129 conn,
2130 fsp,
2131 smb_fname,
2133 req->smbpid,
2134 req->vuid,
2135 access_mask,
2136 share_access,
2137 create_options);
2139 if (NT_STATUS_IS_OK(status)) {
2140 TALLOC_FREE(lck);
2141 if (pinfo) {
2142 *pinfo = FILE_WAS_OPENED;
2144 return NT_STATUS_OK;
2149 * This next line is a subtlety we need for
2150 * MS-Access. If a file open will fail due to share
2151 * permissions and also for security (access) reasons,
2152 * we need to return the access failed error, not the
2153 * share error. We can't open the file due to kernel
2154 * oplock deadlock (it's possible we failed above on
2155 * the open_mode_check()) so use a userspace check.
2158 if (flags & O_RDWR) {
2159 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2160 } else if (flags & O_WRONLY) {
2161 can_access_mask = FILE_WRITE_DATA;
2162 } else {
2163 can_access_mask = FILE_READ_DATA;
2166 if (((can_access_mask & FILE_WRITE_DATA) &&
2167 !CAN_WRITE(conn)) ||
2168 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2169 smb_fname, can_access_mask))) {
2170 can_access = False;
2174 * If we're returning a share violation, ensure we
2175 * cope with the braindead 1 second delay.
2178 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2179 lp_defer_sharing_violations()) {
2180 struct timeval timeout;
2181 struct deferred_open_record state;
2182 int timeout_usecs;
2184 /* this is a hack to speed up torture tests
2185 in 'make test' */
2186 timeout_usecs = lp_parm_int(SNUM(conn),
2187 "smbd","sharedelay",
2188 SHARING_VIOLATION_USEC_WAIT);
2190 /* This is a relative time, added to the absolute
2191 request_time value to get the absolute timeout time.
2192 Note that if this is the second or greater time we enter
2193 this codepath for this particular request mid then
2194 request_time is left as the absolute time of the *first*
2195 time this request mid was processed. This is what allows
2196 the request to eventually time out. */
2198 timeout = timeval_set(0, timeout_usecs);
2200 /* Nothing actually uses state.delayed_for_oplocks
2201 but it's handy to differentiate in debug messages
2202 between a 30 second delay due to oplock break, and
2203 a 1 second delay for share mode conflicts. */
2205 state.delayed_for_oplocks = False;
2206 state.async_open = false;
2207 state.id = id;
2209 if ((req != NULL)
2210 && !request_timed_out(request_time,
2211 timeout)) {
2212 defer_open(lck, request_time, timeout,
2213 req, &state);
2217 TALLOC_FREE(lck);
2218 if (can_access) {
2220 * We have detected a sharing violation here
2221 * so return the correct error code
2223 status = NT_STATUS_SHARING_VIOLATION;
2224 } else {
2225 status = NT_STATUS_ACCESS_DENIED;
2227 return status;
2231 * We exit this block with the share entry *locked*.....
2235 SMB_ASSERT(!file_existed || (lck != NULL));
2238 * Ensure we pay attention to default ACLs on directories if required.
2241 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2242 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2243 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2246 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2247 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2248 (unsigned int)flags, (unsigned int)flags2,
2249 (unsigned int)unx_mode, (unsigned int)access_mask,
2250 (unsigned int)open_access_mask));
2253 * open_file strips any O_TRUNC flags itself.
2256 fsp_open = open_file(fsp, conn, req, parent_dir,
2257 flags|flags2, unx_mode, access_mask,
2258 open_access_mask);
2260 if (!NT_STATUS_IS_OK(fsp_open)) {
2261 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2262 schedule_async_open(request_time, req);
2264 TALLOC_FREE(lck);
2265 return fsp_open;
2268 if (!file_existed) {
2269 struct share_mode_entry *batch_entry = NULL;
2270 struct share_mode_entry *exclusive_entry = NULL;
2271 bool got_level2_oplock = false;
2272 bool got_a_none_oplock = false;
2273 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2274 struct file_id id;
2276 * Deal with the race condition where two smbd's detect the
2277 * file doesn't exist and do the create at the same time. One
2278 * of them will win and set a share mode, the other (ie. this
2279 * one) should check if the requested share mode for this
2280 * create is allowed.
2284 * Now the file exists and fsp is successfully opened,
2285 * fsp->dev and fsp->inode are valid and should replace the
2286 * dev=0,inode=0 from a non existent file. Spotted by
2287 * Nadav Danieli <nadavd@exanet.com>. JRA.
2290 id = fsp->file_id;
2292 lck = get_share_mode_lock(talloc_tos(), id,
2293 conn->connectpath,
2294 smb_fname, &old_write_time);
2296 if (lck == NULL) {
2297 DEBUG(0, ("open_file_ntcreate: Could not get share "
2298 "mode lock for %s\n",
2299 smb_fname_str_dbg(smb_fname)));
2300 fd_close(fsp);
2301 return NT_STATUS_SHARING_VIOLATION;
2304 /* Get the types we need to examine. */
2305 find_oplock_types(fsp,
2306 oplock_request,
2307 lck,
2308 &batch_entry,
2309 &exclusive_entry,
2310 &got_level2_oplock,
2311 &got_a_none_oplock);
2313 /* First pass - send break only on batch oplocks. */
2314 if ((req != NULL) &&
2315 delay_for_batch_oplocks(fsp,
2316 req->mid,
2317 oplock_request,
2318 batch_entry)) {
2319 schedule_defer_open(lck, request_time, req);
2320 TALLOC_FREE(lck);
2321 fd_close(fsp);
2322 return NT_STATUS_SHARING_VIOLATION;
2325 status = open_mode_check(conn, lck, fsp->name_hash,
2326 access_mask, share_access,
2327 create_options, &file_existed);
2329 if (NT_STATUS_IS_OK(status)) {
2330 /* We might be going to allow this open. Check oplock
2331 * status again. */
2332 /* Second pass - send break for both batch or
2333 * exclusive oplocks. */
2334 if ((req != NULL) &&
2335 delay_for_exclusive_oplocks(
2336 fsp,
2337 req->mid,
2338 oplock_request,
2339 exclusive_entry)) {
2340 schedule_defer_open(lck, request_time, req);
2341 TALLOC_FREE(lck);
2342 fd_close(fsp);
2343 return NT_STATUS_SHARING_VIOLATION;
2347 if (!NT_STATUS_IS_OK(status)) {
2348 struct deferred_open_record state;
2350 state.delayed_for_oplocks = False;
2351 state.async_open = false;
2352 state.id = id;
2354 /* Do it all over again immediately. In the second
2355 * round we will find that the file existed and handle
2356 * the DELETE_PENDING and FCB cases correctly. No need
2357 * to duplicate the code here. Essentially this is a
2358 * "goto top of this function", but don't tell
2359 * anybody... */
2361 if (req != NULL) {
2362 defer_open(lck, request_time, timeval_zero(),
2363 req, &state);
2365 TALLOC_FREE(lck);
2366 fd_close(fsp);
2367 return status;
2370 grant_fsp_oplock_type(fsp,
2371 oplock_request,
2372 got_level2_oplock,
2373 got_a_none_oplock);
2376 * We exit this block with the share entry *locked*.....
2381 SMB_ASSERT(lck != NULL);
2383 /* Delete streams if create_disposition requires it */
2384 if (file_existed && clear_ads &&
2385 !is_ntfs_stream_smb_fname(smb_fname)) {
2386 status = delete_all_streams(conn, smb_fname->base_name);
2387 if (!NT_STATUS_IS_OK(status)) {
2388 TALLOC_FREE(lck);
2389 fd_close(fsp);
2390 return status;
2394 /* note that we ignore failure for the following. It is
2395 basically a hack for NFS, and NFS will never set one of
2396 these only read them. Nobody but Samba can ever set a deny
2397 mode and we have already checked our more authoritative
2398 locking database for permission to set this deny mode. If
2399 the kernel refuses the operations then the kernel is wrong.
2400 note that GPFS supports it as well - jmcd */
2402 if (fsp->fh->fd != -1) {
2403 int ret_flock;
2404 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2405 if(ret_flock == -1 ){
2407 TALLOC_FREE(lck);
2408 fd_close(fsp);
2410 return NT_STATUS_SHARING_VIOLATION;
2415 * At this point onwards, we can guarentee that the share entry
2416 * is locked, whether we created the file or not, and that the
2417 * deny mode is compatible with all current opens.
2421 * If requested, truncate the file.
2424 if (file_existed && (flags2&O_TRUNC)) {
2426 * We are modifying the file after open - update the stat
2427 * struct..
2429 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2430 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2431 status = map_nt_error_from_unix(errno);
2432 TALLOC_FREE(lck);
2433 fd_close(fsp);
2434 return status;
2439 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2440 * but we don't have to store this - just ignore it on access check.
2442 if (conn->sconn->using_smb2) {
2444 * SMB2 doesn't return it (according to Microsoft tests).
2445 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2446 * File created with access = 0x7 (Read, Write, Delete)
2447 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2449 fsp->access_mask = access_mask;
2450 } else {
2451 /* But SMB1 does. */
2452 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2455 if (file_existed) {
2456 /* stat opens on existing files don't get oplocks. */
2457 if (is_stat_open(open_access_mask)) {
2458 fsp->oplock_type = NO_OPLOCK;
2461 if (flags2 & O_TRUNC) {
2462 info = FILE_WAS_OVERWRITTEN;
2463 } else {
2464 info = FILE_WAS_OPENED;
2466 } else {
2467 info = FILE_WAS_CREATED;
2470 if (pinfo) {
2471 *pinfo = info;
2475 * Setup the oplock info in both the shared memory and
2476 * file structs.
2479 status = set_file_oplock(fsp, fsp->oplock_type);
2480 if (!NT_STATUS_IS_OK(status)) {
2482 * Could not get the kernel oplock or there are byte-range
2483 * locks on the file.
2485 fsp->oplock_type = NO_OPLOCK;
2488 set_share_mode(lck, fsp, get_current_uid(conn),
2489 req ? req->mid : 0,
2490 fsp->oplock_type);
2492 /* Handle strange delete on close create semantics. */
2493 if (create_options & FILE_DELETE_ON_CLOSE) {
2495 status = can_set_delete_on_close(fsp, new_dos_attributes);
2497 if (!NT_STATUS_IS_OK(status)) {
2498 /* Remember to delete the mode we just added. */
2499 del_share_mode(lck, fsp);
2500 TALLOC_FREE(lck);
2501 fd_close(fsp);
2502 return status;
2504 /* Note that here we set the *inital* delete on close flag,
2505 not the regular one. The magic gets handled in close. */
2506 fsp->initial_delete_on_close = True;
2509 if (info == FILE_WAS_OVERWRITTEN
2510 || info == FILE_WAS_CREATED
2511 || info == FILE_WAS_SUPERSEDED) {
2512 new_file_created = True;
2515 if (new_file_created) {
2516 /* Files should be initially set as archive */
2517 if (lp_map_archive(SNUM(conn)) ||
2518 lp_store_dos_attributes(SNUM(conn))) {
2519 if (!posix_open) {
2520 if (file_set_dosmode(conn, smb_fname,
2521 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2522 parent_dir, true) == 0) {
2523 unx_mode = smb_fname->st.st_ex_mode;
2529 /* Determine sparse flag. */
2530 if (posix_open) {
2531 /* POSIX opens are sparse by default. */
2532 fsp->is_sparse = true;
2533 } else {
2534 fsp->is_sparse = (file_existed &&
2535 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2539 * Take care of inherited ACLs on created files - if default ACL not
2540 * selected.
2543 if (!posix_open && !file_existed && !def_acl) {
2545 int saved_errno = errno; /* We might get ENOSYS in the next
2546 * call.. */
2548 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2549 errno == ENOSYS) {
2550 errno = saved_errno; /* Ignore ENOSYS */
2553 } else if (new_unx_mode) {
2555 int ret = -1;
2557 /* Attributes need changing. File already existed. */
2560 int saved_errno = errno; /* We might get ENOSYS in the
2561 * next call.. */
2562 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2564 if (ret == -1 && errno == ENOSYS) {
2565 errno = saved_errno; /* Ignore ENOSYS */
2566 } else {
2567 DEBUG(5, ("open_file_ntcreate: reset "
2568 "attributes of file %s to 0%o\n",
2569 smb_fname_str_dbg(smb_fname),
2570 (unsigned int)new_unx_mode));
2571 ret = 0; /* Don't do the fchmod below. */
2575 if ((ret == -1) &&
2576 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2577 DEBUG(5, ("open_file_ntcreate: failed to reset "
2578 "attributes of file %s to 0%o\n",
2579 smb_fname_str_dbg(smb_fname),
2580 (unsigned int)new_unx_mode));
2583 /* If this is a successful open, we must remove any deferred open
2584 * records. */
2585 if (req != NULL) {
2586 del_deferred_open_entry(lck, req->mid,
2587 messaging_server_id(req->sconn->msg_ctx));
2589 TALLOC_FREE(lck);
2591 return NT_STATUS_OK;
2595 /****************************************************************************
2596 Open a file for for write to ensure that we can fchmod it.
2597 ****************************************************************************/
2599 NTSTATUS open_file_fchmod(connection_struct *conn,
2600 struct smb_filename *smb_fname,
2601 files_struct **result)
2603 if (!VALID_STAT(smb_fname->st)) {
2604 return NT_STATUS_INVALID_PARAMETER;
2607 return SMB_VFS_CREATE_FILE(
2608 conn, /* conn */
2609 NULL, /* req */
2610 0, /* root_dir_fid */
2611 smb_fname, /* fname */
2612 FILE_WRITE_DATA, /* access_mask */
2613 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2614 FILE_SHARE_DELETE),
2615 FILE_OPEN, /* create_disposition*/
2616 0, /* create_options */
2617 0, /* file_attributes */
2618 INTERNAL_OPEN_ONLY, /* oplock_request */
2619 0, /* allocation_size */
2620 0, /* private_flags */
2621 NULL, /* sd */
2622 NULL, /* ea_list */
2623 result, /* result */
2624 NULL); /* pinfo */
2627 static NTSTATUS mkdir_internal(connection_struct *conn,
2628 struct smb_filename *smb_dname,
2629 uint32 file_attributes)
2631 mode_t mode;
2632 char *parent_dir = NULL;
2633 NTSTATUS status;
2634 bool posix_open = false;
2635 bool need_re_stat = false;
2636 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2638 if(access_mask & ~(conn->share_access)) {
2639 DEBUG(5,("mkdir_internal: failing share access "
2640 "%s\n", lp_servicename(SNUM(conn))));
2641 return NT_STATUS_ACCESS_DENIED;
2644 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2645 NULL)) {
2646 return NT_STATUS_NO_MEMORY;
2649 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2650 posix_open = true;
2651 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2652 } else {
2653 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2656 status = check_parent_access(conn,
2657 smb_dname,
2658 access_mask);
2659 if(!NT_STATUS_IS_OK(status)) {
2660 DEBUG(5,("mkdir_internal: check_parent_access "
2661 "on directory %s for path %s returned %s\n",
2662 parent_dir,
2663 smb_dname->base_name,
2664 nt_errstr(status) ));
2665 return status;
2668 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2669 return map_nt_error_from_unix(errno);
2672 /* Ensure we're checking for a symlink here.... */
2673 /* We don't want to get caught by a symlink racer. */
2675 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2676 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2677 smb_fname_str_dbg(smb_dname), strerror(errno)));
2678 return map_nt_error_from_unix(errno);
2681 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2682 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2683 smb_fname_str_dbg(smb_dname)));
2684 return NT_STATUS_NOT_A_DIRECTORY;
2687 if (lp_store_dos_attributes(SNUM(conn))) {
2688 if (!posix_open) {
2689 file_set_dosmode(conn, smb_dname,
2690 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2691 parent_dir, true);
2695 if (lp_inherit_perms(SNUM(conn))) {
2696 inherit_access_posix_acl(conn, parent_dir,
2697 smb_dname->base_name, mode);
2698 need_re_stat = true;
2701 if (!posix_open) {
2703 * Check if high bits should have been set,
2704 * then (if bits are missing): add them.
2705 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2706 * dir.
2708 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2709 (mode & ~smb_dname->st.st_ex_mode)) {
2710 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2711 (smb_dname->st.st_ex_mode |
2712 (mode & ~smb_dname->st.st_ex_mode)));
2713 need_re_stat = true;
2717 /* Change the owner if required. */
2718 if (lp_inherit_owner(SNUM(conn))) {
2719 change_dir_owner_to_parent(conn, parent_dir,
2720 smb_dname->base_name,
2721 &smb_dname->st);
2722 need_re_stat = true;
2725 if (need_re_stat) {
2726 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2727 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2728 smb_fname_str_dbg(smb_dname), strerror(errno)));
2729 return map_nt_error_from_unix(errno);
2733 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2734 smb_dname->base_name);
2736 return NT_STATUS_OK;
2739 /****************************************************************************
2740 Open a directory from an NT SMB call.
2741 ****************************************************************************/
2743 static NTSTATUS open_directory(connection_struct *conn,
2744 struct smb_request *req,
2745 struct smb_filename *smb_dname,
2746 uint32 access_mask,
2747 uint32 share_access,
2748 uint32 create_disposition,
2749 uint32 create_options,
2750 uint32 file_attributes,
2751 int *pinfo,
2752 files_struct **result)
2754 files_struct *fsp = NULL;
2755 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2756 struct share_mode_lock *lck = NULL;
2757 NTSTATUS status;
2758 struct timespec mtimespec;
2759 int info = 0;
2761 if (is_ntfs_stream_smb_fname(smb_dname)) {
2762 DEBUG(2, ("open_directory: %s is a stream name!\n",
2763 smb_fname_str_dbg(smb_dname)));
2764 return NT_STATUS_NOT_A_DIRECTORY;
2767 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2768 /* Ensure we have a directory attribute. */
2769 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2772 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2773 "share_access = 0x%x create_options = 0x%x, "
2774 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2775 smb_fname_str_dbg(smb_dname),
2776 (unsigned int)access_mask,
2777 (unsigned int)share_access,
2778 (unsigned int)create_options,
2779 (unsigned int)create_disposition,
2780 (unsigned int)file_attributes));
2782 status = smbd_calculate_access_mask(conn, smb_dname,
2783 access_mask, &access_mask);
2784 if (!NT_STATUS_IS_OK(status)) {
2785 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2786 "on file %s returned %s\n",
2787 smb_fname_str_dbg(smb_dname),
2788 nt_errstr(status)));
2789 return status;
2792 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2793 !security_token_has_privilege(get_current_nttok(conn),
2794 SEC_PRIV_SECURITY)) {
2795 DEBUG(10, ("open_directory: open on %s "
2796 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2797 smb_fname_str_dbg(smb_dname)));
2798 return NT_STATUS_PRIVILEGE_NOT_HELD;
2801 switch( create_disposition ) {
2802 case FILE_OPEN:
2804 if (!dir_existed) {
2805 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2808 info = FILE_WAS_OPENED;
2809 break;
2811 case FILE_CREATE:
2813 /* If directory exists error. If directory doesn't
2814 * exist create. */
2816 if (dir_existed) {
2817 status = NT_STATUS_OBJECT_NAME_COLLISION;
2818 DEBUG(2, ("open_directory: unable to create "
2819 "%s. Error was %s\n",
2820 smb_fname_str_dbg(smb_dname),
2821 nt_errstr(status)));
2822 return status;
2825 status = mkdir_internal(conn, smb_dname,
2826 file_attributes);
2828 if (!NT_STATUS_IS_OK(status)) {
2829 DEBUG(2, ("open_directory: unable to create "
2830 "%s. Error was %s\n",
2831 smb_fname_str_dbg(smb_dname),
2832 nt_errstr(status)));
2833 return status;
2836 info = FILE_WAS_CREATED;
2837 break;
2839 case FILE_OPEN_IF:
2841 * If directory exists open. If directory doesn't
2842 * exist create.
2845 if (dir_existed) {
2846 status = NT_STATUS_OK;
2847 info = FILE_WAS_OPENED;
2848 } else {
2849 status = mkdir_internal(conn, smb_dname,
2850 file_attributes);
2852 if (NT_STATUS_IS_OK(status)) {
2853 info = FILE_WAS_CREATED;
2854 } else {
2855 /* Cope with create race. */
2856 if (!NT_STATUS_EQUAL(status,
2857 NT_STATUS_OBJECT_NAME_COLLISION)) {
2858 DEBUG(2, ("open_directory: unable to create "
2859 "%s. Error was %s\n",
2860 smb_fname_str_dbg(smb_dname),
2861 nt_errstr(status)));
2862 return status;
2864 info = FILE_WAS_OPENED;
2868 break;
2870 case FILE_SUPERSEDE:
2871 case FILE_OVERWRITE:
2872 case FILE_OVERWRITE_IF:
2873 default:
2874 DEBUG(5,("open_directory: invalid create_disposition "
2875 "0x%x for directory %s\n",
2876 (unsigned int)create_disposition,
2877 smb_fname_str_dbg(smb_dname)));
2878 return NT_STATUS_INVALID_PARAMETER;
2881 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2882 DEBUG(5,("open_directory: %s is not a directory !\n",
2883 smb_fname_str_dbg(smb_dname)));
2884 return NT_STATUS_NOT_A_DIRECTORY;
2887 if (info == FILE_WAS_OPENED) {
2888 status = smbd_check_access_rights(conn, smb_dname, access_mask);
2889 if (!NT_STATUS_IS_OK(status)) {
2890 DEBUG(10, ("open_directory: smbd_check_access_rights on "
2891 "file %s failed with %s\n",
2892 smb_fname_str_dbg(smb_dname),
2893 nt_errstr(status)));
2894 return status;
2898 status = file_new(req, conn, &fsp);
2899 if(!NT_STATUS_IS_OK(status)) {
2900 return status;
2904 * Setup the files_struct for it.
2907 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2908 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2909 fsp->file_pid = req ? req->smbpid : 0;
2910 fsp->can_lock = False;
2911 fsp->can_read = False;
2912 fsp->can_write = False;
2914 fsp->share_access = share_access;
2915 fsp->fh->private_options = 0;
2917 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2919 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2920 fsp->print_file = NULL;
2921 fsp->modified = False;
2922 fsp->oplock_type = NO_OPLOCK;
2923 fsp->sent_oplock_break = NO_BREAK_SENT;
2924 fsp->is_directory = True;
2925 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2926 status = fsp_set_smb_fname(fsp, smb_dname);
2927 if (!NT_STATUS_IS_OK(status)) {
2928 file_free(req, fsp);
2929 return status;
2932 mtimespec = smb_dname->st.st_ex_mtime;
2934 #ifdef O_DIRECTORY
2935 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2936 #else
2937 /* POSIX allows us to open a directory with O_RDONLY. */
2938 status = fd_open(conn, fsp, O_RDONLY, 0);
2939 #endif
2940 if (!NT_STATUS_IS_OK(status)) {
2941 DEBUG(5, ("open_directory: Could not open fd for "
2942 "%s (%s)\n",
2943 smb_fname_str_dbg(smb_dname),
2944 nt_errstr(status)));
2945 file_free(req, fsp);
2946 return status;
2949 status = vfs_stat_fsp(fsp);
2950 if (!NT_STATUS_IS_OK(status)) {
2951 fd_close(fsp);
2952 file_free(req, fsp);
2953 return status;
2956 /* Ensure there was no race condition. */
2957 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2958 DEBUG(5,("open_directory: stat struct differs for "
2959 "directory %s.\n",
2960 smb_fname_str_dbg(smb_dname)));
2961 fd_close(fsp);
2962 file_free(req, fsp);
2963 return NT_STATUS_ACCESS_DENIED;
2966 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2967 conn->connectpath, smb_dname,
2968 &mtimespec);
2970 if (lck == NULL) {
2971 DEBUG(0, ("open_directory: Could not get share mode lock for "
2972 "%s\n", smb_fname_str_dbg(smb_dname)));
2973 fd_close(fsp);
2974 file_free(req, fsp);
2975 return NT_STATUS_SHARING_VIOLATION;
2978 status = open_mode_check(conn, lck, fsp->name_hash,
2979 access_mask, share_access,
2980 create_options, &dir_existed);
2982 if (!NT_STATUS_IS_OK(status)) {
2983 TALLOC_FREE(lck);
2984 fd_close(fsp);
2985 file_free(req, fsp);
2986 return status;
2989 set_share_mode(lck, fsp, get_current_uid(conn),
2990 req ? req->mid : 0, NO_OPLOCK);
2992 /* For directories the delete on close bit at open time seems
2993 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2994 if (create_options & FILE_DELETE_ON_CLOSE) {
2995 status = can_set_delete_on_close(fsp, 0);
2996 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2997 TALLOC_FREE(lck);
2998 fd_close(fsp);
2999 file_free(req, fsp);
3000 return status;
3003 if (NT_STATUS_IS_OK(status)) {
3004 /* Note that here we set the *inital* delete on close flag,
3005 not the regular one. The magic gets handled in close. */
3006 fsp->initial_delete_on_close = True;
3010 TALLOC_FREE(lck);
3012 if (pinfo) {
3013 *pinfo = info;
3016 *result = fsp;
3017 return NT_STATUS_OK;
3020 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3021 struct smb_filename *smb_dname)
3023 NTSTATUS status;
3024 files_struct *fsp;
3026 status = SMB_VFS_CREATE_FILE(
3027 conn, /* conn */
3028 req, /* req */
3029 0, /* root_dir_fid */
3030 smb_dname, /* fname */
3031 FILE_READ_ATTRIBUTES, /* access_mask */
3032 FILE_SHARE_NONE, /* share_access */
3033 FILE_CREATE, /* create_disposition*/
3034 FILE_DIRECTORY_FILE, /* create_options */
3035 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3036 0, /* oplock_request */
3037 0, /* allocation_size */
3038 0, /* private_flags */
3039 NULL, /* sd */
3040 NULL, /* ea_list */
3041 &fsp, /* result */
3042 NULL); /* pinfo */
3044 if (NT_STATUS_IS_OK(status)) {
3045 close_file(req, fsp, NORMAL_CLOSE);
3048 return status;
3051 /****************************************************************************
3052 Receive notification that one of our open files has been renamed by another
3053 smbd process.
3054 ****************************************************************************/
3056 void msg_file_was_renamed(struct messaging_context *msg,
3057 void *private_data,
3058 uint32_t msg_type,
3059 struct server_id server_id,
3060 DATA_BLOB *data)
3062 files_struct *fsp;
3063 char *frm = (char *)data->data;
3064 struct file_id id;
3065 const char *sharepath;
3066 const char *base_name;
3067 const char *stream_name;
3068 struct smb_filename *smb_fname = NULL;
3069 size_t sp_len, bn_len;
3070 NTSTATUS status;
3071 struct smbd_server_connection *sconn =
3072 talloc_get_type_abort(private_data,
3073 struct smbd_server_connection);
3075 if (data->data == NULL
3076 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3077 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3078 (int)data->length));
3079 return;
3082 /* Unpack the message. */
3083 pull_file_id_24(frm, &id);
3084 sharepath = &frm[24];
3085 sp_len = strlen(sharepath);
3086 base_name = sharepath + sp_len + 1;
3087 bn_len = strlen(base_name);
3088 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3090 /* stream_name must always be NULL if there is no stream. */
3091 if (stream_name[0] == '\0') {
3092 stream_name = NULL;
3095 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3096 stream_name, NULL, &smb_fname);
3097 if (!NT_STATUS_IS_OK(status)) {
3098 return;
3101 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3102 "file_id %s\n",
3103 sharepath, smb_fname_str_dbg(smb_fname),
3104 file_id_string_tos(&id)));
3106 for(fsp = file_find_di_first(sconn, id); fsp;
3107 fsp = file_find_di_next(fsp)) {
3108 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3110 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3111 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3112 smb_fname_str_dbg(smb_fname)));
3113 status = fsp_set_smb_fname(fsp, smb_fname);
3114 if (!NT_STATUS_IS_OK(status)) {
3115 goto out;
3117 } else {
3118 /* TODO. JRA. */
3119 /* Now we have the complete path we can work out if this is
3120 actually within this share and adjust newname accordingly. */
3121 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3122 "not sharepath %s) "
3123 "%s from %s -> %s\n",
3124 fsp->conn->connectpath,
3125 sharepath,
3126 fsp_fnum_dbg(fsp),
3127 fsp_str_dbg(fsp),
3128 smb_fname_str_dbg(smb_fname)));
3131 out:
3132 TALLOC_FREE(smb_fname);
3133 return;
3137 * If a main file is opened for delete, all streams need to be checked for
3138 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3139 * If that works, delete them all by setting the delete on close and close.
3142 NTSTATUS open_streams_for_delete(connection_struct *conn,
3143 const char *fname)
3145 struct stream_struct *stream_info = NULL;
3146 files_struct **streams = NULL;
3147 int i;
3148 unsigned int num_streams = 0;
3149 TALLOC_CTX *frame = talloc_stackframe();
3150 NTSTATUS status;
3152 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3153 &num_streams, &stream_info);
3155 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3156 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3157 DEBUG(10, ("no streams around\n"));
3158 TALLOC_FREE(frame);
3159 return NT_STATUS_OK;
3162 if (!NT_STATUS_IS_OK(status)) {
3163 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3164 nt_errstr(status)));
3165 goto fail;
3168 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3169 num_streams));
3171 if (num_streams == 0) {
3172 TALLOC_FREE(frame);
3173 return NT_STATUS_OK;
3176 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3177 if (streams == NULL) {
3178 DEBUG(0, ("talloc failed\n"));
3179 status = NT_STATUS_NO_MEMORY;
3180 goto fail;
3183 for (i=0; i<num_streams; i++) {
3184 struct smb_filename *smb_fname = NULL;
3186 if (strequal(stream_info[i].name, "::$DATA")) {
3187 streams[i] = NULL;
3188 continue;
3191 status = create_synthetic_smb_fname(talloc_tos(), fname,
3192 stream_info[i].name,
3193 NULL, &smb_fname);
3194 if (!NT_STATUS_IS_OK(status)) {
3195 goto fail;
3198 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3199 DEBUG(10, ("Unable to stat stream: %s\n",
3200 smb_fname_str_dbg(smb_fname)));
3203 status = SMB_VFS_CREATE_FILE(
3204 conn, /* conn */
3205 NULL, /* req */
3206 0, /* root_dir_fid */
3207 smb_fname, /* fname */
3208 DELETE_ACCESS, /* access_mask */
3209 (FILE_SHARE_READ | /* share_access */
3210 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3211 FILE_OPEN, /* create_disposition*/
3212 0, /* create_options */
3213 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3214 0, /* oplock_request */
3215 0, /* allocation_size */
3216 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3217 NULL, /* sd */
3218 NULL, /* ea_list */
3219 &streams[i], /* result */
3220 NULL); /* pinfo */
3222 if (!NT_STATUS_IS_OK(status)) {
3223 DEBUG(10, ("Could not open stream %s: %s\n",
3224 smb_fname_str_dbg(smb_fname),
3225 nt_errstr(status)));
3227 TALLOC_FREE(smb_fname);
3228 break;
3230 TALLOC_FREE(smb_fname);
3234 * don't touch the variable "status" beyond this point :-)
3237 for (i -= 1 ; i >= 0; i--) {
3238 if (streams[i] == NULL) {
3239 continue;
3242 DEBUG(10, ("Closing stream # %d, %s\n", i,
3243 fsp_str_dbg(streams[i])));
3244 close_file(NULL, streams[i], NORMAL_CLOSE);
3247 fail:
3248 TALLOC_FREE(frame);
3249 return status;
3252 /*********************************************************************
3253 Create a default ACL by inheriting from the parent. If no inheritance
3254 from the parent available, don't set anything. This will leave the actual
3255 permissions the new file or directory already got from the filesystem
3256 as the NT ACL when read.
3257 *********************************************************************/
3259 static NTSTATUS inherit_new_acl(files_struct *fsp)
3261 TALLOC_CTX *ctx = talloc_tos();
3262 char *parent_name = NULL;
3263 struct security_descriptor *parent_desc = NULL;
3264 NTSTATUS status = NT_STATUS_OK;
3265 struct security_descriptor *psd = NULL;
3266 struct dom_sid *owner_sid = NULL;
3267 struct dom_sid *group_sid = NULL;
3268 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3269 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3270 bool inheritable_components = false;
3271 size_t size = 0;
3273 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3274 return NT_STATUS_NO_MEMORY;
3277 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3278 parent_name,
3279 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3280 &parent_desc);
3281 if (!NT_STATUS_IS_OK(status)) {
3282 return status;
3285 inheritable_components = sd_has_inheritable_components(parent_desc,
3286 fsp->is_directory);
3288 if (!inheritable_components && !inherit_owner) {
3289 /* Nothing to inherit and not setting owner. */
3290 return NT_STATUS_OK;
3293 /* Create an inherited descriptor from the parent. */
3295 if (DEBUGLEVEL >= 10) {
3296 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3297 fsp_str_dbg(fsp) ));
3298 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3301 /* Inherit from parent descriptor if "inherit owner" set. */
3302 if (inherit_owner) {
3303 owner_sid = parent_desc->owner_sid;
3304 group_sid = parent_desc->group_sid;
3307 if (owner_sid == NULL) {
3308 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3310 if (group_sid == NULL) {
3311 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3314 status = se_create_child_secdesc(ctx,
3315 &psd,
3316 &size,
3317 parent_desc,
3318 owner_sid,
3319 group_sid,
3320 fsp->is_directory);
3321 if (!NT_STATUS_IS_OK(status)) {
3322 return status;
3325 /* If inheritable_components == false,
3326 se_create_child_secdesc()
3327 creates a security desriptor with a NULL dacl
3328 entry, but with SEC_DESC_DACL_PRESENT. We need
3329 to remove that flag. */
3331 if (!inheritable_components) {
3332 security_info_sent &= ~SECINFO_DACL;
3333 psd->type &= ~SEC_DESC_DACL_PRESENT;
3336 if (DEBUGLEVEL >= 10) {
3337 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3338 fsp_str_dbg(fsp) ));
3339 NDR_PRINT_DEBUG(security_descriptor, psd);
3342 if (inherit_owner) {
3343 /* We need to be root to force this. */
3344 become_root();
3346 status = SMB_VFS_FSET_NT_ACL(fsp,
3347 security_info_sent,
3348 psd);
3349 if (inherit_owner) {
3350 unbecome_root();
3352 return status;
3356 * Wrapper around open_file_ntcreate and open_directory
3359 static NTSTATUS create_file_unixpath(connection_struct *conn,
3360 struct smb_request *req,
3361 struct smb_filename *smb_fname,
3362 uint32_t access_mask,
3363 uint32_t share_access,
3364 uint32_t create_disposition,
3365 uint32_t create_options,
3366 uint32_t file_attributes,
3367 uint32_t oplock_request,
3368 uint64_t allocation_size,
3369 uint32_t private_flags,
3370 struct security_descriptor *sd,
3371 struct ea_list *ea_list,
3373 files_struct **result,
3374 int *pinfo)
3376 int info = FILE_WAS_OPENED;
3377 files_struct *base_fsp = NULL;
3378 files_struct *fsp = NULL;
3379 NTSTATUS status;
3381 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3382 "file_attributes = 0x%x, share_access = 0x%x, "
3383 "create_disposition = 0x%x create_options = 0x%x "
3384 "oplock_request = 0x%x private_flags = 0x%x "
3385 "ea_list = 0x%p, sd = 0x%p, "
3386 "fname = %s\n",
3387 (unsigned int)access_mask,
3388 (unsigned int)file_attributes,
3389 (unsigned int)share_access,
3390 (unsigned int)create_disposition,
3391 (unsigned int)create_options,
3392 (unsigned int)oplock_request,
3393 (unsigned int)private_flags,
3394 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3396 if (create_options & FILE_OPEN_BY_FILE_ID) {
3397 status = NT_STATUS_NOT_SUPPORTED;
3398 goto fail;
3401 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3402 status = NT_STATUS_INVALID_PARAMETER;
3403 goto fail;
3406 if (req == NULL) {
3407 oplock_request |= INTERNAL_OPEN_ONLY;
3410 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3411 && (access_mask & DELETE_ACCESS)
3412 && !is_ntfs_stream_smb_fname(smb_fname)) {
3414 * We can't open a file with DELETE access if any of the
3415 * streams is open without FILE_SHARE_DELETE
3417 status = open_streams_for_delete(conn, smb_fname->base_name);
3419 if (!NT_STATUS_IS_OK(status)) {
3420 goto fail;
3424 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3425 !security_token_has_privilege(get_current_nttok(conn),
3426 SEC_PRIV_SECURITY)) {
3427 DEBUG(10, ("create_file_unixpath: open on %s "
3428 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3429 smb_fname_str_dbg(smb_fname)));
3430 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3431 goto fail;
3434 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3435 && is_ntfs_stream_smb_fname(smb_fname)
3436 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3437 uint32 base_create_disposition;
3438 struct smb_filename *smb_fname_base = NULL;
3440 if (create_options & FILE_DIRECTORY_FILE) {
3441 status = NT_STATUS_NOT_A_DIRECTORY;
3442 goto fail;
3445 switch (create_disposition) {
3446 case FILE_OPEN:
3447 base_create_disposition = FILE_OPEN;
3448 break;
3449 default:
3450 base_create_disposition = FILE_OPEN_IF;
3451 break;
3454 /* Create an smb_filename with stream_name == NULL. */
3455 status = create_synthetic_smb_fname(talloc_tos(),
3456 smb_fname->base_name,
3457 NULL, NULL,
3458 &smb_fname_base);
3459 if (!NT_STATUS_IS_OK(status)) {
3460 goto fail;
3463 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3464 DEBUG(10, ("Unable to stat stream: %s\n",
3465 smb_fname_str_dbg(smb_fname_base)));
3468 /* Open the base file. */
3469 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3470 FILE_SHARE_READ
3471 | FILE_SHARE_WRITE
3472 | FILE_SHARE_DELETE,
3473 base_create_disposition,
3474 0, 0, 0, 0, 0, NULL, NULL,
3475 &base_fsp, NULL);
3476 TALLOC_FREE(smb_fname_base);
3478 if (!NT_STATUS_IS_OK(status)) {
3479 DEBUG(10, ("create_file_unixpath for base %s failed: "
3480 "%s\n", smb_fname->base_name,
3481 nt_errstr(status)));
3482 goto fail;
3484 /* we don't need to low level fd */
3485 fd_close(base_fsp);
3489 * If it's a request for a directory open, deal with it separately.
3492 if (create_options & FILE_DIRECTORY_FILE) {
3494 if (create_options & FILE_NON_DIRECTORY_FILE) {
3495 status = NT_STATUS_INVALID_PARAMETER;
3496 goto fail;
3499 /* Can't open a temp directory. IFS kit test. */
3500 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3501 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3502 status = NT_STATUS_INVALID_PARAMETER;
3503 goto fail;
3507 * We will get a create directory here if the Win32
3508 * app specified a security descriptor in the
3509 * CreateDirectory() call.
3512 oplock_request = 0;
3513 status = open_directory(
3514 conn, req, smb_fname, access_mask, share_access,
3515 create_disposition, create_options, file_attributes,
3516 &info, &fsp);
3517 } else {
3520 * Ordinary file case.
3523 status = file_new(req, conn, &fsp);
3524 if(!NT_STATUS_IS_OK(status)) {
3525 goto fail;
3528 status = fsp_set_smb_fname(fsp, smb_fname);
3529 if (!NT_STATUS_IS_OK(status)) {
3530 goto fail;
3534 * We're opening the stream element of a base_fsp
3535 * we already opened. Set up the base_fsp pointer.
3537 if (base_fsp) {
3538 fsp->base_fsp = base_fsp;
3541 status = open_file_ntcreate(conn,
3542 req,
3543 access_mask,
3544 share_access,
3545 create_disposition,
3546 create_options,
3547 file_attributes,
3548 oplock_request,
3549 private_flags,
3550 &info,
3551 fsp);
3553 if(!NT_STATUS_IS_OK(status)) {
3554 file_free(req, fsp);
3555 fsp = NULL;
3558 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3560 /* A stream open never opens a directory */
3562 if (base_fsp) {
3563 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3564 goto fail;
3568 * Fail the open if it was explicitly a non-directory
3569 * file.
3572 if (create_options & FILE_NON_DIRECTORY_FILE) {
3573 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3574 goto fail;
3577 oplock_request = 0;
3578 status = open_directory(
3579 conn, req, smb_fname, access_mask,
3580 share_access, create_disposition,
3581 create_options, file_attributes,
3582 &info, &fsp);
3586 if (!NT_STATUS_IS_OK(status)) {
3587 goto fail;
3590 fsp->base_fsp = base_fsp;
3592 if ((ea_list != NULL) &&
3593 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3594 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3595 if (!NT_STATUS_IS_OK(status)) {
3596 goto fail;
3600 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3601 status = NT_STATUS_ACCESS_DENIED;
3602 goto fail;
3605 /* Save the requested allocation size. */
3606 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3607 if (allocation_size
3608 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3609 fsp->initial_allocation_size = smb_roundup(
3610 fsp->conn, allocation_size);
3611 if (fsp->is_directory) {
3612 /* Can't set allocation size on a directory. */
3613 status = NT_STATUS_ACCESS_DENIED;
3614 goto fail;
3616 if (vfs_allocate_file_space(
3617 fsp, fsp->initial_allocation_size) == -1) {
3618 status = NT_STATUS_DISK_FULL;
3619 goto fail;
3621 } else {
3622 fsp->initial_allocation_size = smb_roundup(
3623 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3627 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3628 fsp->base_fsp == NULL) {
3629 if (sd != NULL) {
3631 * According to the MS documentation, the only time the security
3632 * descriptor is applied to the opened file is iff we *created* the
3633 * file; an existing file stays the same.
3635 * Also, it seems (from observation) that you can open the file with
3636 * any access mask but you can still write the sd. We need to override
3637 * the granted access before we call set_sd
3638 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3641 uint32_t sec_info_sent;
3642 uint32_t saved_access_mask = fsp->access_mask;
3644 sec_info_sent = get_sec_info(sd);
3646 fsp->access_mask = FILE_GENERIC_ALL;
3648 /* Convert all the generic bits. */
3649 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3650 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3652 if (sec_info_sent & (SECINFO_OWNER|
3653 SECINFO_GROUP|
3654 SECINFO_DACL|
3655 SECINFO_SACL)) {
3656 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3659 fsp->access_mask = saved_access_mask;
3661 if (!NT_STATUS_IS_OK(status)) {
3662 goto fail;
3664 } else if (lp_inherit_acls(SNUM(conn))) {
3665 /* Inherit from parent. Errors here are not fatal. */
3666 status = inherit_new_acl(fsp);
3667 if (!NT_STATUS_IS_OK(status)) {
3668 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3669 fsp_str_dbg(fsp),
3670 nt_errstr(status) ));
3675 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3677 *result = fsp;
3678 if (pinfo != NULL) {
3679 *pinfo = info;
3682 smb_fname->st = fsp->fsp_name->st;
3684 return NT_STATUS_OK;
3686 fail:
3687 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3689 if (fsp != NULL) {
3690 if (base_fsp && fsp->base_fsp == base_fsp) {
3692 * The close_file below will close
3693 * fsp->base_fsp.
3695 base_fsp = NULL;
3697 close_file(req, fsp, ERROR_CLOSE);
3698 fsp = NULL;
3700 if (base_fsp != NULL) {
3701 close_file(req, base_fsp, ERROR_CLOSE);
3702 base_fsp = NULL;
3704 return status;
3708 * Calculate the full path name given a relative fid.
3710 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3711 struct smb_request *req,
3712 uint16_t root_dir_fid,
3713 const struct smb_filename *smb_fname,
3714 struct smb_filename **smb_fname_out)
3716 files_struct *dir_fsp;
3717 char *parent_fname = NULL;
3718 char *new_base_name = NULL;
3719 NTSTATUS status;
3721 if (root_dir_fid == 0 || !smb_fname) {
3722 status = NT_STATUS_INTERNAL_ERROR;
3723 goto out;
3726 dir_fsp = file_fsp(req, root_dir_fid);
3728 if (dir_fsp == NULL) {
3729 status = NT_STATUS_INVALID_HANDLE;
3730 goto out;
3733 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3734 status = NT_STATUS_INVALID_HANDLE;
3735 goto out;
3738 if (!dir_fsp->is_directory) {
3741 * Check to see if this is a mac fork of some kind.
3744 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3745 is_ntfs_stream_smb_fname(smb_fname)) {
3746 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3747 goto out;
3751 we need to handle the case when we get a
3752 relative open relative to a file and the
3753 pathname is blank - this is a reopen!
3754 (hint from demyn plantenberg)
3757 status = NT_STATUS_INVALID_HANDLE;
3758 goto out;
3761 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3763 * We're at the toplevel dir, the final file name
3764 * must not contain ./, as this is filtered out
3765 * normally by srvstr_get_path and unix_convert
3766 * explicitly rejects paths containing ./.
3768 parent_fname = talloc_strdup(talloc_tos(), "");
3769 if (parent_fname == NULL) {
3770 status = NT_STATUS_NO_MEMORY;
3771 goto out;
3773 } else {
3774 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3777 * Copy in the base directory name.
3780 parent_fname = talloc_array(talloc_tos(), char,
3781 dir_name_len+2);
3782 if (parent_fname == NULL) {
3783 status = NT_STATUS_NO_MEMORY;
3784 goto out;
3786 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3787 dir_name_len+1);
3790 * Ensure it ends in a '/'.
3791 * We used TALLOC_SIZE +2 to add space for the '/'.
3794 if(dir_name_len
3795 && (parent_fname[dir_name_len-1] != '\\')
3796 && (parent_fname[dir_name_len-1] != '/')) {
3797 parent_fname[dir_name_len] = '/';
3798 parent_fname[dir_name_len+1] = '\0';
3802 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3803 smb_fname->base_name);
3804 if (new_base_name == NULL) {
3805 status = NT_STATUS_NO_MEMORY;
3806 goto out;
3809 status = filename_convert(req,
3810 conn,
3811 req->flags2 & FLAGS2_DFS_PATHNAMES,
3812 new_base_name,
3814 NULL,
3815 smb_fname_out);
3816 if (!NT_STATUS_IS_OK(status)) {
3817 goto out;
3820 out:
3821 TALLOC_FREE(parent_fname);
3822 TALLOC_FREE(new_base_name);
3823 return status;
3826 NTSTATUS create_file_default(connection_struct *conn,
3827 struct smb_request *req,
3828 uint16_t root_dir_fid,
3829 struct smb_filename *smb_fname,
3830 uint32_t access_mask,
3831 uint32_t share_access,
3832 uint32_t create_disposition,
3833 uint32_t create_options,
3834 uint32_t file_attributes,
3835 uint32_t oplock_request,
3836 uint64_t allocation_size,
3837 uint32_t private_flags,
3838 struct security_descriptor *sd,
3839 struct ea_list *ea_list,
3840 files_struct **result,
3841 int *pinfo)
3843 int info = FILE_WAS_OPENED;
3844 files_struct *fsp = NULL;
3845 NTSTATUS status;
3846 bool stream_name = false;
3848 DEBUG(10,("create_file: access_mask = 0x%x "
3849 "file_attributes = 0x%x, share_access = 0x%x, "
3850 "create_disposition = 0x%x create_options = 0x%x "
3851 "oplock_request = 0x%x "
3852 "private_flags = 0x%x "
3853 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3854 "fname = %s\n",
3855 (unsigned int)access_mask,
3856 (unsigned int)file_attributes,
3857 (unsigned int)share_access,
3858 (unsigned int)create_disposition,
3859 (unsigned int)create_options,
3860 (unsigned int)oplock_request,
3861 (unsigned int)private_flags,
3862 (unsigned int)root_dir_fid,
3863 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3866 * Calculate the filename from the root_dir_if if necessary.
3869 if (root_dir_fid != 0) {
3870 struct smb_filename *smb_fname_out = NULL;
3871 status = get_relative_fid_filename(conn, req, root_dir_fid,
3872 smb_fname, &smb_fname_out);
3873 if (!NT_STATUS_IS_OK(status)) {
3874 goto fail;
3876 smb_fname = smb_fname_out;
3880 * Check to see if this is a mac fork of some kind.
3883 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3884 if (stream_name) {
3885 enum FAKE_FILE_TYPE fake_file_type;
3887 fake_file_type = is_fake_file(smb_fname);
3889 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3892 * Here we go! support for changing the disk quotas
3893 * --metze
3895 * We need to fake up to open this MAGIC QUOTA file
3896 * and return a valid FID.
3898 * w2k close this file directly after openening xp
3899 * also tries a QUERY_FILE_INFO on the file and then
3900 * close it
3902 status = open_fake_file(req, conn, req->vuid,
3903 fake_file_type, smb_fname,
3904 access_mask, &fsp);
3905 if (!NT_STATUS_IS_OK(status)) {
3906 goto fail;
3909 ZERO_STRUCT(smb_fname->st);
3910 goto done;
3913 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3914 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3915 goto fail;
3919 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
3920 int ret;
3921 smb_fname->stream_name = NULL;
3922 /* We have to handle this error here. */
3923 if (create_options & FILE_DIRECTORY_FILE) {
3924 status = NT_STATUS_NOT_A_DIRECTORY;
3925 goto fail;
3927 if (lp_posix_pathnames()) {
3928 ret = SMB_VFS_LSTAT(conn, smb_fname);
3929 } else {
3930 ret = SMB_VFS_STAT(conn, smb_fname);
3933 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3934 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3935 goto fail;
3939 status = create_file_unixpath(
3940 conn, req, smb_fname, access_mask, share_access,
3941 create_disposition, create_options, file_attributes,
3942 oplock_request, allocation_size, private_flags,
3943 sd, ea_list,
3944 &fsp, &info);
3946 if (!NT_STATUS_IS_OK(status)) {
3947 goto fail;
3950 done:
3951 DEBUG(10, ("create_file: info=%d\n", info));
3953 *result = fsp;
3954 if (pinfo != NULL) {
3955 *pinfo = info;
3957 return NT_STATUS_OK;
3959 fail:
3960 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3962 if (fsp != NULL) {
3963 close_file(req, fsp, ERROR_CLOSE);
3964 fsp = NULL;
3966 return status;