s3: No code change, just re-indent
[Samba/gebeck_regimport.git] / source3 / smbd / open.c
blob8fe87d5a5118e11054ee5e01f42ee2e125f52408
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 "serverid.h"
33 #include "messages.h"
35 extern const struct generic_mapping file_generic_mapping;
37 struct deferred_open_record {
38 bool delayed_for_oplocks;
39 bool async_open;
40 struct file_id id;
43 /****************************************************************************
44 If the requester wanted DELETE_ACCESS and was rejected because
45 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
46 overrides this.
47 ****************************************************************************/
49 static bool parent_override_delete(connection_struct *conn,
50 const struct smb_filename *smb_fname,
51 uint32_t access_mask,
52 uint32_t rejected_mask)
54 if ((access_mask & DELETE_ACCESS) &&
55 (rejected_mask & DELETE_ACCESS) &&
56 can_delete_file_in_directory(conn, smb_fname)) {
57 return true;
59 return false;
62 /****************************************************************************
63 Check if we have open rights.
64 ****************************************************************************/
66 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
67 const struct smb_filename *smb_fname,
68 bool use_privs,
69 uint32_t access_mask)
71 /* Check if we have rights to open. */
72 NTSTATUS status;
73 struct security_descriptor *sd = NULL;
74 uint32_t rejected_share_access;
75 uint32_t rejected_mask = access_mask;
77 rejected_share_access = access_mask & ~(conn->share_access);
79 if (rejected_share_access) {
80 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
81 "on %s (0x%x)\n",
82 (unsigned int)access_mask,
83 smb_fname_str_dbg(smb_fname),
84 (unsigned int)rejected_share_access ));
85 return NT_STATUS_ACCESS_DENIED;
88 if (!use_privs && get_current_uid(conn) == (uid_t)0) {
89 /* I'm sorry sir, I didn't know you were root... */
90 DEBUG(10,("smbd_check_access_rights: root override "
91 "on %s. Granting 0x%x\n",
92 smb_fname_str_dbg(smb_fname),
93 (unsigned int)access_mask ));
94 return NT_STATUS_OK;
97 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
98 DEBUG(10,("smbd_check_access_rights: not checking ACL "
99 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
100 smb_fname_str_dbg(smb_fname),
101 (unsigned int)access_mask ));
102 return NT_STATUS_OK;
105 if (access_mask == DELETE_ACCESS &&
106 VALID_STAT(smb_fname->st) &&
107 S_ISLNK(smb_fname->st.st_ex_mode)) {
108 /* We can always delete a symlink. */
109 DEBUG(10,("smbd_check_access_rights: not checking ACL "
110 "on DELETE_ACCESS on symlink %s.\n",
111 smb_fname_str_dbg(smb_fname) ));
112 return NT_STATUS_OK;
115 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
116 (SECINFO_OWNER |
117 SECINFO_GROUP |
118 SECINFO_DACL),&sd);
120 if (!NT_STATUS_IS_OK(status)) {
121 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
122 "on %s: %s\n",
123 smb_fname_str_dbg(smb_fname),
124 nt_errstr(status)));
126 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
127 goto access_denied;
130 return status;
134 * Never test FILE_READ_ATTRIBUTES. se_file_access_check() also takes care of
135 * owner WRITE_DAC and READ_CONTROL.
137 status = se_file_access_check(sd,
138 get_current_nttok(conn),
139 use_privs,
140 (access_mask & ~FILE_READ_ATTRIBUTES),
141 &rejected_mask);
143 DEBUG(10,("smbd_check_access_rights: file %s requesting "
144 "0x%x returning 0x%x (%s)\n",
145 smb_fname_str_dbg(smb_fname),
146 (unsigned int)access_mask,
147 (unsigned int)rejected_mask,
148 nt_errstr(status) ));
150 if (!NT_STATUS_IS_OK(status)) {
151 if (DEBUGLEVEL >= 10) {
152 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
153 smb_fname_str_dbg(smb_fname) ));
154 NDR_PRINT_DEBUG(security_descriptor, sd);
158 TALLOC_FREE(sd);
160 if (NT_STATUS_IS_OK(status) ||
161 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
162 return status;
165 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
167 access_denied:
169 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
170 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
171 !lp_store_dos_attributes(SNUM(conn)) &&
172 (lp_map_readonly(SNUM(conn)) ||
173 lp_map_archive(SNUM(conn)) ||
174 lp_map_hidden(SNUM(conn)) ||
175 lp_map_system(SNUM(conn)))) {
176 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
178 DEBUG(10,("smbd_check_access_rights: "
179 "overrode "
180 "FILE_WRITE_ATTRIBUTES "
181 "on file %s\n",
182 smb_fname_str_dbg(smb_fname)));
185 if (parent_override_delete(conn,
186 smb_fname,
187 access_mask,
188 rejected_mask)) {
189 /* Were we trying to do an open
190 * for delete and didn't get DELETE
191 * access (only) ? Check if the
192 * directory allows DELETE_CHILD.
193 * See here:
194 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
195 * for details. */
197 rejected_mask &= ~DELETE_ACCESS;
199 DEBUG(10,("smbd_check_access_rights: "
200 "overrode "
201 "DELETE_ACCESS on "
202 "file %s\n",
203 smb_fname_str_dbg(smb_fname)));
206 if (rejected_mask != 0) {
207 return NT_STATUS_ACCESS_DENIED;
209 return NT_STATUS_OK;
212 static NTSTATUS check_parent_access(struct connection_struct *conn,
213 struct smb_filename *smb_fname,
214 uint32_t access_mask)
216 NTSTATUS status;
217 char *parent_dir = NULL;
218 struct security_descriptor *parent_sd = NULL;
219 uint32_t access_granted = 0;
221 if (!parent_dirname(talloc_tos(),
222 smb_fname->base_name,
223 &parent_dir,
224 NULL)) {
225 return NT_STATUS_NO_MEMORY;
228 if (get_current_uid(conn) == (uid_t)0) {
229 /* I'm sorry sir, I didn't know you were root... */
230 DEBUG(10,("check_parent_access: root override "
231 "on %s. Granting 0x%x\n",
232 smb_fname_str_dbg(smb_fname),
233 (unsigned int)access_mask ));
234 return NT_STATUS_OK;
237 status = SMB_VFS_GET_NT_ACL(conn,
238 parent_dir,
239 SECINFO_DACL,
240 &parent_sd);
242 if (!NT_STATUS_IS_OK(status)) {
243 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
244 "%s with error %s\n",
245 parent_dir,
246 nt_errstr(status)));
247 return status;
251 * Never test FILE_READ_ATTRIBUTES. se_file_access_check() also takes care of
252 * owner WRITE_DAC and READ_CONTROL.
254 status = se_file_access_check(parent_sd,
255 get_current_nttok(conn),
256 false,
257 (access_mask & ~FILE_READ_ATTRIBUTES),
258 &access_granted);
259 if(!NT_STATUS_IS_OK(status)) {
260 DEBUG(5,("check_parent_access: access check "
261 "on directory %s for "
262 "path %s for mask 0x%x returned (0x%x) %s\n",
263 parent_dir,
264 smb_fname->base_name,
265 access_mask,
266 access_granted,
267 nt_errstr(status) ));
268 return status;
271 return NT_STATUS_OK;
274 /****************************************************************************
275 fd support routines - attempt to do a dos_open.
276 ****************************************************************************/
278 NTSTATUS fd_open(struct connection_struct *conn,
279 files_struct *fsp,
280 int flags,
281 mode_t mode)
283 struct smb_filename *smb_fname = fsp->fsp_name;
284 NTSTATUS status = NT_STATUS_OK;
286 #ifdef O_NOFOLLOW
288 * Never follow symlinks on a POSIX client. The
289 * client should be doing this.
292 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
293 flags |= O_NOFOLLOW;
295 #endif
297 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
298 if (fsp->fh->fd == -1) {
299 int posix_errno = errno;
300 #ifdef O_NOFOLLOW
301 #if defined(ENOTSUP) && defined(OSF1)
302 /* handle special Tru64 errno */
303 if (errno == ENOTSUP) {
304 posix_errno = ELOOP;
306 #endif /* ENOTSUP */
307 #ifdef EFTYPE
308 /* fix broken NetBSD errno */
309 if (errno == EFTYPE) {
310 posix_errno = ELOOP;
312 #endif /* EFTYPE */
313 /* fix broken FreeBSD errno */
314 if (errno == EMLINK) {
315 posix_errno = ELOOP;
317 #endif /* O_NOFOLLOW */
318 status = map_nt_error_from_unix(posix_errno);
319 if (errno == EMFILE) {
320 static time_t last_warned = 0L;
322 if (time((time_t *) NULL) > last_warned) {
323 DEBUG(0,("Too many open files, unable "
324 "to open more! smbd's max "
325 "open files = %d\n",
326 lp_max_open_files()));
327 last_warned = time((time_t *) NULL);
333 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
334 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
335 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
337 return status;
340 /****************************************************************************
341 Close the file associated with a fsp.
342 ****************************************************************************/
344 NTSTATUS fd_close(files_struct *fsp)
346 int ret;
348 if (fsp->dptr) {
349 dptr_CloseDir(fsp);
351 if (fsp->fh->fd == -1) {
352 return NT_STATUS_OK; /* What we used to call a stat open. */
354 if (fsp->fh->ref_count > 1) {
355 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
358 ret = SMB_VFS_CLOSE(fsp);
359 fsp->fh->fd = -1;
360 if (ret == -1) {
361 return map_nt_error_from_unix(errno);
363 return NT_STATUS_OK;
366 /****************************************************************************
367 Change the ownership of a file to that of the parent directory.
368 Do this by fd if possible.
369 ****************************************************************************/
371 void change_file_owner_to_parent(connection_struct *conn,
372 const char *inherit_from_dir,
373 files_struct *fsp)
375 struct smb_filename *smb_fname_parent = NULL;
376 NTSTATUS status;
377 int ret;
379 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
380 NULL, NULL, &smb_fname_parent);
381 if (!NT_STATUS_IS_OK(status)) {
382 return;
385 ret = SMB_VFS_STAT(conn, smb_fname_parent);
386 if (ret == -1) {
387 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
388 "directory %s. Error was %s\n",
389 smb_fname_str_dbg(smb_fname_parent),
390 strerror(errno)));
391 TALLOC_FREE(smb_fname_parent);
392 return;
395 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
396 /* Already this uid - no need to change. */
397 DEBUG(10,("change_file_owner_to_parent: file %s "
398 "is already owned by uid %d\n",
399 fsp_str_dbg(fsp),
400 (int)fsp->fsp_name->st.st_ex_uid ));
401 TALLOC_FREE(smb_fname_parent);
402 return;
405 become_root();
406 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
407 unbecome_root();
408 if (ret == -1) {
409 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
410 "file %s to parent directory uid %u. Error "
411 "was %s\n", fsp_str_dbg(fsp),
412 (unsigned int)smb_fname_parent->st.st_ex_uid,
413 strerror(errno) ));
414 } else {
415 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
416 "parent directory uid %u.\n", fsp_str_dbg(fsp),
417 (unsigned int)smb_fname_parent->st.st_ex_uid));
418 /* Ensure the uid entry is updated. */
419 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
422 TALLOC_FREE(smb_fname_parent);
425 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
426 const char *inherit_from_dir,
427 const char *fname,
428 SMB_STRUCT_STAT *psbuf)
430 struct smb_filename *smb_fname_parent = NULL;
431 struct smb_filename *smb_fname_cwd = NULL;
432 char *saved_dir = NULL;
433 TALLOC_CTX *ctx = talloc_tos();
434 NTSTATUS status = NT_STATUS_OK;
435 int ret;
437 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
438 &smb_fname_parent);
439 if (!NT_STATUS_IS_OK(status)) {
440 return status;
443 ret = SMB_VFS_STAT(conn, smb_fname_parent);
444 if (ret == -1) {
445 status = map_nt_error_from_unix(errno);
446 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
447 "directory %s. Error was %s\n",
448 smb_fname_str_dbg(smb_fname_parent),
449 strerror(errno)));
450 goto out;
453 /* We've already done an lstat into psbuf, and we know it's a
454 directory. If we can cd into the directory and the dev/ino
455 are the same then we can safely chown without races as
456 we're locking the directory in place by being in it. This
457 should work on any UNIX (thanks tridge :-). JRA.
460 saved_dir = vfs_GetWd(ctx,conn);
461 if (!saved_dir) {
462 status = map_nt_error_from_unix(errno);
463 DEBUG(0,("change_dir_owner_to_parent: failed to get "
464 "current working directory. Error was %s\n",
465 strerror(errno)));
466 goto out;
469 /* Chdir into the new path. */
470 if (vfs_ChDir(conn, fname) == -1) {
471 status = map_nt_error_from_unix(errno);
472 DEBUG(0,("change_dir_owner_to_parent: failed to change "
473 "current working directory to %s. Error "
474 "was %s\n", fname, strerror(errno) ));
475 goto chdir;
478 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
479 &smb_fname_cwd);
480 if (!NT_STATUS_IS_OK(status)) {
481 return status;
484 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
485 if (ret == -1) {
486 status = map_nt_error_from_unix(errno);
487 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
488 "directory '.' (%s) Error was %s\n",
489 fname, strerror(errno)));
490 goto chdir;
493 /* Ensure we're pointing at the same place. */
494 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
495 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
496 DEBUG(0,("change_dir_owner_to_parent: "
497 "device/inode on directory %s changed. "
498 "Refusing to chown !\n", fname ));
499 status = NT_STATUS_ACCESS_DENIED;
500 goto chdir;
503 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
504 /* Already this uid - no need to change. */
505 DEBUG(10,("change_dir_owner_to_parent: directory %s "
506 "is already owned by uid %d\n",
507 fname,
508 (int)smb_fname_cwd->st.st_ex_uid ));
509 status = NT_STATUS_OK;
510 goto chdir;
513 become_root();
514 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
515 (gid_t)-1);
516 unbecome_root();
517 if (ret == -1) {
518 status = map_nt_error_from_unix(errno);
519 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
520 "directory %s to parent directory uid %u. "
521 "Error was %s\n", fname,
522 (unsigned int)smb_fname_parent->st.st_ex_uid,
523 strerror(errno) ));
524 } else {
525 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
526 "directory %s to parent directory uid %u.\n",
527 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
528 /* Ensure the uid entry is updated. */
529 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
532 chdir:
533 vfs_ChDir(conn,saved_dir);
534 out:
535 TALLOC_FREE(smb_fname_parent);
536 TALLOC_FREE(smb_fname_cwd);
537 return status;
540 /****************************************************************************
541 Open a file - returning a guaranteed ATOMIC indication of if the
542 file was created or not.
543 ****************************************************************************/
545 static NTSTATUS fd_open_atomic(struct connection_struct *conn,
546 files_struct *fsp,
547 int flags,
548 mode_t mode,
549 bool *file_created)
551 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
552 bool file_existed = VALID_STAT(fsp->fsp_name->st);
554 *file_created = false;
556 if (!(flags & O_CREAT)) {
558 * We're not creating the file, just pass through.
560 return fd_open(conn, fsp, flags, mode);
563 if (flags & O_EXCL) {
565 * Fail if already exists, just pass through.
567 status = fd_open(conn, fsp, flags, mode);
570 * Here we've opened with O_CREAT|O_EXCL. If that went
571 * NT_STATUS_OK, we *know* we created this file.
573 *file_created = NT_STATUS_IS_OK(status);
575 return status;
579 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
580 * To know absolutely if we created the file or not,
581 * we can never call O_CREAT without O_EXCL. So if
582 * we think the file existed, try without O_CREAT|O_EXCL.
583 * If we think the file didn't exist, try with
584 * O_CREAT|O_EXCL. Keep bouncing between these two
585 * requests until either the file is created, or
586 * opened. Either way, we keep going until we get
587 * a returnable result (error, or open/create).
590 while(1) {
591 int curr_flags = flags;
593 if (file_existed) {
594 /* Just try open, do not create. */
595 curr_flags &= ~(O_CREAT);
596 status = fd_open(conn, fsp, curr_flags, mode);
597 if (NT_STATUS_EQUAL(status,
598 NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
600 * Someone deleted it in the meantime.
601 * Retry with O_EXCL.
603 file_existed = false;
604 DEBUG(10,("fd_open_atomic: file %s existed. "
605 "Retry.\n",
606 smb_fname_str_dbg(fsp->fsp_name)));
607 continue;
609 } else {
610 /* Try create exclusively, fail if it exists. */
611 curr_flags |= O_EXCL;
612 status = fd_open(conn, fsp, curr_flags, mode);
613 if (NT_STATUS_EQUAL(status,
614 NT_STATUS_OBJECT_NAME_COLLISION)) {
616 * Someone created it in the meantime.
617 * Retry without O_CREAT.
619 file_existed = true;
620 DEBUG(10,("fd_open_atomic: file %s "
621 "did not exist. Retry.\n",
622 smb_fname_str_dbg(fsp->fsp_name)));
623 continue;
625 if (NT_STATUS_IS_OK(status)) {
627 * Here we've opened with O_CREAT|O_EXCL
628 * and got success. We *know* we created
629 * this file.
631 *file_created = true;
634 /* Create is done, or failed. */
635 break;
637 return status;
640 /****************************************************************************
641 Open a file.
642 ****************************************************************************/
644 static NTSTATUS open_file(files_struct *fsp,
645 connection_struct *conn,
646 struct smb_request *req,
647 const char *parent_dir,
648 int flags,
649 mode_t unx_mode,
650 uint32 access_mask, /* client requested access mask. */
651 uint32 open_access_mask, /* what we're actually using in the open. */
652 bool *p_file_created)
654 struct smb_filename *smb_fname = fsp->fsp_name;
655 NTSTATUS status = NT_STATUS_OK;
656 int accmode = (flags & O_ACCMODE);
657 int local_flags = flags;
658 bool file_existed = VALID_STAT(fsp->fsp_name->st);
660 fsp->fh->fd = -1;
661 errno = EPERM;
663 /* Check permissions */
666 * This code was changed after seeing a client open request
667 * containing the open mode of (DENY_WRITE/read-only) with
668 * the 'create if not exist' bit set. The previous code
669 * would fail to open the file read only on a read-only share
670 * as it was checking the flags parameter directly against O_RDONLY,
671 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
672 * JRA.
675 if (!CAN_WRITE(conn)) {
676 /* It's a read-only share - fail if we wanted to write. */
677 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
678 DEBUG(3,("Permission denied opening %s\n",
679 smb_fname_str_dbg(smb_fname)));
680 return NT_STATUS_ACCESS_DENIED;
682 if (flags & O_CREAT) {
683 /* We don't want to write - but we must make sure that
684 O_CREAT doesn't create the file if we have write
685 access into the directory.
687 flags &= ~(O_CREAT|O_EXCL);
688 local_flags &= ~(O_CREAT|O_EXCL);
693 * This little piece of insanity is inspired by the
694 * fact that an NT client can open a file for O_RDONLY,
695 * but set the create disposition to FILE_EXISTS_TRUNCATE.
696 * If the client *can* write to the file, then it expects to
697 * truncate the file, even though it is opening for readonly.
698 * Quicken uses this stupid trick in backup file creation...
699 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
700 * for helping track this one down. It didn't bite us in 2.0.x
701 * as we always opened files read-write in that release. JRA.
704 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
705 DEBUG(10,("open_file: truncate requested on read-only open "
706 "for file %s\n", smb_fname_str_dbg(smb_fname)));
707 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
710 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
711 (!file_existed && (local_flags & O_CREAT)) ||
712 ((local_flags & O_TRUNC) == O_TRUNC) ) {
713 const char *wild;
714 int ret;
716 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
718 * We would block on opening a FIFO with no one else on the
719 * other end. Do what we used to do and add O_NONBLOCK to the
720 * open flags. JRA.
723 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
724 local_flags &= ~O_TRUNC; /* Can't truncate a FIFO. */
725 local_flags |= O_NONBLOCK;
727 #endif
729 /* Don't create files with Microsoft wildcard characters. */
730 if (fsp->base_fsp) {
732 * wildcard characters are allowed in stream names
733 * only test the basefilename
735 wild = fsp->base_fsp->fsp_name->base_name;
736 } else {
737 wild = smb_fname->base_name;
739 if ((local_flags & O_CREAT) && !file_existed &&
740 ms_has_wild(wild)) {
741 return NT_STATUS_OBJECT_NAME_INVALID;
744 /* Can we access this file ? */
745 if (!fsp->base_fsp) {
746 /* Only do this check on non-stream open. */
747 if (file_existed) {
748 status = smbd_check_access_rights(conn,
749 smb_fname,
750 false,
751 access_mask);
752 } else if (local_flags & O_CREAT){
753 status = check_parent_access(conn,
754 smb_fname,
755 SEC_DIR_ADD_FILE);
756 } else {
757 /* File didn't exist and no O_CREAT. */
758 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
760 if (!NT_STATUS_IS_OK(status)) {
761 DEBUG(10,("open_file: "
762 "%s on file "
763 "%s returned %s\n",
764 file_existed ?
765 "smbd_check_access_rights" :
766 "check_parent_access",
767 smb_fname_str_dbg(smb_fname),
768 nt_errstr(status) ));
769 return status;
773 /* Actually do the open */
774 status = fd_open_atomic(conn, fsp, local_flags,
775 unx_mode, p_file_created);
776 if (!NT_STATUS_IS_OK(status)) {
777 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
778 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
779 nt_errstr(status),local_flags,flags));
780 return status;
783 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
784 if (ret == -1) {
785 /* If we have an fd, this stat should succeed. */
786 DEBUG(0,("Error doing fstat on open file %s "
787 "(%s)\n",
788 smb_fname_str_dbg(smb_fname),
789 strerror(errno) ));
790 status = map_nt_error_from_unix(errno);
791 fd_close(fsp);
792 return status;
795 if (*p_file_created) {
796 /* We created this file. */
798 bool need_re_stat = false;
799 /* Do all inheritance work after we've
800 done a successful fstat call and filled
801 in the stat struct in fsp->fsp_name. */
803 /* Inherit the ACL if required */
804 if (lp_inherit_perms(SNUM(conn))) {
805 inherit_access_posix_acl(conn, parent_dir,
806 smb_fname->base_name,
807 unx_mode);
808 need_re_stat = true;
811 /* Change the owner if required. */
812 if (lp_inherit_owner(SNUM(conn))) {
813 change_file_owner_to_parent(conn, parent_dir,
814 fsp);
815 need_re_stat = true;
818 if (need_re_stat) {
819 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
820 /* If we have an fd, this stat should succeed. */
821 if (ret == -1) {
822 DEBUG(0,("Error doing fstat on open file %s "
823 "(%s)\n",
824 smb_fname_str_dbg(smb_fname),
825 strerror(errno) ));
829 notify_fname(conn, NOTIFY_ACTION_ADDED,
830 FILE_NOTIFY_CHANGE_FILE_NAME,
831 smb_fname->base_name);
833 } else {
834 fsp->fh->fd = -1; /* What we used to call a stat open. */
835 if (!file_existed) {
836 /* File must exist for a stat open. */
837 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
840 status = smbd_check_access_rights(conn,
841 smb_fname,
842 false,
843 access_mask);
845 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
846 fsp->posix_open &&
847 S_ISLNK(smb_fname->st.st_ex_mode)) {
848 /* This is a POSIX stat open for delete
849 * or rename on a symlink that points
850 * nowhere. Allow. */
851 DEBUG(10,("open_file: allowing POSIX "
852 "open on bad symlink %s\n",
853 smb_fname_str_dbg(smb_fname)));
854 status = NT_STATUS_OK;
857 if (!NT_STATUS_IS_OK(status)) {
858 DEBUG(10,("open_file: "
859 "smbd_check_access_rights on file "
860 "%s returned %s\n",
861 smb_fname_str_dbg(smb_fname),
862 nt_errstr(status) ));
863 return status;
868 * POSIX allows read-only opens of directories. We don't
869 * want to do this (we use a different code path for this)
870 * so catch a directory open and return an EISDIR. JRA.
873 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
874 fd_close(fsp);
875 errno = EISDIR;
876 return NT_STATUS_FILE_IS_A_DIRECTORY;
879 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
880 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
881 fsp->file_pid = req ? req->smbpid : 0;
882 fsp->can_lock = True;
883 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
884 fsp->can_write =
885 CAN_WRITE(conn) &&
886 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
887 fsp->print_file = NULL;
888 fsp->modified = False;
889 fsp->sent_oplock_break = NO_BREAK_SENT;
890 fsp->is_directory = False;
891 if (conn->aio_write_behind_list &&
892 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
893 conn->case_sensitive)) {
894 fsp->aio_write_behind = True;
897 fsp->wcp = NULL; /* Write cache pointer. */
899 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
900 conn->session_info->unix_info->unix_name,
901 smb_fname_str_dbg(smb_fname),
902 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
903 conn->num_files_open));
905 errno = 0;
906 return NT_STATUS_OK;
909 /****************************************************************************
910 Check if we can open a file with a share mode.
911 Returns True if conflict, False if not.
912 ****************************************************************************/
914 static bool share_conflict(struct share_mode_entry *entry,
915 uint32 access_mask,
916 uint32 share_access)
918 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
919 "entry->share_access = 0x%x, "
920 "entry->private_options = 0x%x\n",
921 (unsigned int)entry->access_mask,
922 (unsigned int)entry->share_access,
923 (unsigned int)entry->private_options));
925 if (server_id_is_disconnected(&entry->pid)) {
927 * note: cleanup should have been done by
928 * delay_for_batch_oplocks()
930 return false;
933 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
934 (unsigned int)access_mask, (unsigned int)share_access));
936 if ((entry->access_mask & (FILE_WRITE_DATA|
937 FILE_APPEND_DATA|
938 FILE_READ_DATA|
939 FILE_EXECUTE|
940 DELETE_ACCESS)) == 0) {
941 DEBUG(10,("share_conflict: No conflict due to "
942 "entry->access_mask = 0x%x\n",
943 (unsigned int)entry->access_mask ));
944 return False;
947 if ((access_mask & (FILE_WRITE_DATA|
948 FILE_APPEND_DATA|
949 FILE_READ_DATA|
950 FILE_EXECUTE|
951 DELETE_ACCESS)) == 0) {
952 DEBUG(10,("share_conflict: No conflict due to "
953 "access_mask = 0x%x\n",
954 (unsigned int)access_mask ));
955 return False;
958 #if 1 /* JRA TEST - Superdebug. */
959 #define CHECK_MASK(num, am, right, sa, share) \
960 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
961 (unsigned int)(num), (unsigned int)(am), \
962 (unsigned int)(right), (unsigned int)(am)&(right) )); \
963 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
964 (unsigned int)(num), (unsigned int)(sa), \
965 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
966 if (((am) & (right)) && !((sa) & (share))) { \
967 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
968 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
969 (unsigned int)(share) )); \
970 return True; \
972 #else
973 #define CHECK_MASK(num, am, right, sa, share) \
974 if (((am) & (right)) && !((sa) & (share))) { \
975 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
976 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
977 (unsigned int)(share) )); \
978 return True; \
980 #endif
982 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
983 share_access, FILE_SHARE_WRITE);
984 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
985 entry->share_access, FILE_SHARE_WRITE);
987 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
988 share_access, FILE_SHARE_READ);
989 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
990 entry->share_access, FILE_SHARE_READ);
992 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
993 share_access, FILE_SHARE_DELETE);
994 CHECK_MASK(6, access_mask, DELETE_ACCESS,
995 entry->share_access, FILE_SHARE_DELETE);
997 DEBUG(10,("share_conflict: No conflict.\n"));
998 return False;
1001 #if defined(DEVELOPER)
1002 static void validate_my_share_entries(struct smbd_server_connection *sconn,
1003 int num,
1004 struct share_mode_entry *share_entry)
1006 struct server_id self = messaging_server_id(sconn->msg_ctx);
1007 files_struct *fsp;
1009 if (!serverid_equal(&self, &share_entry->pid)) {
1010 return;
1013 if (is_deferred_open_entry(share_entry) &&
1014 !open_was_deferred(sconn, share_entry->op_mid)) {
1015 char *str = talloc_asprintf(talloc_tos(),
1016 "Got a deferred entry without a request: "
1017 "PANIC: %s\n",
1018 share_mode_str(talloc_tos(), num, share_entry));
1019 smb_panic(str);
1022 if (!is_valid_share_mode_entry(share_entry)) {
1023 return;
1026 fsp = file_find_dif(sconn, share_entry->id,
1027 share_entry->share_file_id);
1028 if (!fsp) {
1029 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1030 share_mode_str(talloc_tos(), num, share_entry) ));
1031 smb_panic("validate_my_share_entries: Cannot match a "
1032 "share entry with an open file\n");
1035 if (is_deferred_open_entry(share_entry)) {
1036 goto panic;
1039 if ((share_entry->op_type == NO_OPLOCK) &&
1040 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
1041 /* Someone has already written to it, but I haven't yet
1042 * noticed */
1043 return;
1046 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
1047 goto panic;
1050 return;
1052 panic:
1054 char *str;
1055 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1056 share_mode_str(talloc_tos(), num, share_entry) ));
1057 str = talloc_asprintf(talloc_tos(),
1058 "validate_my_share_entries: "
1059 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1060 fsp->fsp_name->base_name,
1061 (unsigned int)fsp->oplock_type,
1062 (unsigned int)share_entry->op_type );
1063 smb_panic(str);
1066 #endif
1068 bool is_stat_open(uint32 access_mask)
1070 const uint32_t stat_open_bits =
1071 (SYNCHRONIZE_ACCESS|
1072 FILE_READ_ATTRIBUTES|
1073 FILE_WRITE_ATTRIBUTES);
1075 return (((access_mask & stat_open_bits) != 0) &&
1076 ((access_mask & ~stat_open_bits) == 0));
1079 /****************************************************************************
1080 Deal with share modes
1081 Invarient: Share mode must be locked on entry and exit.
1082 Returns -1 on error, or number of share modes on success (may be zero).
1083 ****************************************************************************/
1085 static NTSTATUS open_mode_check(connection_struct *conn,
1086 struct share_mode_lock *lck,
1087 uint32_t name_hash,
1088 uint32 access_mask,
1089 uint32 share_access,
1090 uint32 create_options,
1091 bool *file_existed)
1093 int i;
1095 if(lck->data->num_share_modes == 0) {
1096 return NT_STATUS_OK;
1099 /* A delete on close prohibits everything */
1101 if (is_delete_on_close_set(lck, name_hash)) {
1103 * Check the delete on close token
1104 * is valid. It could have been left
1105 * after a server crash.
1107 for(i = 0; i < lck->data->num_share_modes; i++) {
1108 if (!share_mode_stale_pid(lck->data, i)) {
1110 *file_existed = true;
1112 return NT_STATUS_DELETE_PENDING;
1115 return NT_STATUS_OK;
1118 if (is_stat_open(access_mask)) {
1119 /* Stat open that doesn't trigger oplock breaks or share mode
1120 * checks... ! JRA. */
1121 return NT_STATUS_OK;
1125 * Check if the share modes will give us access.
1128 #if defined(DEVELOPER)
1129 for(i = 0; i < lck->data->num_share_modes; i++) {
1130 validate_my_share_entries(conn->sconn, i,
1131 &lck->data->share_modes[i]);
1133 #endif
1135 /* Now we check the share modes, after any oplock breaks. */
1136 for(i = 0; i < lck->data->num_share_modes; i++) {
1138 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1139 continue;
1142 /* someone else has a share lock on it, check to see if we can
1143 * too */
1144 if (share_conflict(&lck->data->share_modes[i],
1145 access_mask, share_access)) {
1147 if (share_mode_stale_pid(lck->data, i)) {
1148 continue;
1151 *file_existed = true;
1153 return NT_STATUS_SHARING_VIOLATION;
1157 if (lck->data->num_share_modes != 0) {
1158 *file_existed = true;
1161 return NT_STATUS_OK;
1165 * Send a break message to the oplock holder and delay the open for
1166 * our client.
1169 static NTSTATUS send_break_message(files_struct *fsp,
1170 struct share_mode_entry *exclusive,
1171 uint64_t mid,
1172 int oplock_request)
1174 NTSTATUS status;
1175 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1177 DEBUG(10, ("Sending break request to PID %s\n",
1178 procid_str_static(&exclusive->pid)));
1179 exclusive->op_mid = mid;
1181 /* Create the message. */
1182 share_mode_entry_to_message(msg, exclusive);
1184 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1185 don't want this set in the share mode struct pointed to by lck. */
1187 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1188 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1189 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1192 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1193 MSG_SMB_BREAK_REQUEST,
1194 (uint8 *)msg,
1195 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1196 if (!NT_STATUS_IS_OK(status)) {
1197 DEBUG(3, ("Could not send oplock break message: %s\n",
1198 nt_errstr(status)));
1201 return status;
1205 * Return share_mode_entry pointers for :
1206 * 1). Batch oplock entry.
1207 * 2). Batch or exclusive oplock entry (may be identical to #1).
1208 * bool have_level2_oplock
1209 * bool have_no_oplock.
1210 * Do internal consistency checks on the share mode for a file.
1213 static void find_oplock_types(files_struct *fsp,
1214 int oplock_request,
1215 const struct share_mode_lock *lck,
1216 struct share_mode_entry **pp_batch,
1217 struct share_mode_entry **pp_ex_or_batch,
1218 bool *got_level2,
1219 bool *got_no_oplock)
1221 int i;
1223 *pp_batch = NULL;
1224 *pp_ex_or_batch = NULL;
1225 *got_level2 = false;
1226 *got_no_oplock = false;
1228 /* Ignore stat or internal opens, as is done in
1229 delay_for_batch_oplocks() and
1230 delay_for_exclusive_oplocks().
1232 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1233 return;
1236 for (i=0; i<lck->data->num_share_modes; i++) {
1237 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1238 continue;
1241 if (lck->data->share_modes[i].op_type == NO_OPLOCK &&
1242 is_stat_open(lck->data->share_modes[i].access_mask)) {
1243 /* We ignore stat opens in the table - they
1244 always have NO_OPLOCK and never get or
1245 cause breaks. JRA. */
1246 continue;
1249 if (BATCH_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1250 /* batch - can only be one. */
1251 if (share_mode_stale_pid(lck->data, i)) {
1252 DEBUG(10, ("Found stale batch oplock\n"));
1253 continue;
1255 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1256 smb_panic("Bad batch oplock entry.");
1258 *pp_batch = &lck->data->share_modes[i];
1261 if (EXCLUSIVE_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1262 if (share_mode_stale_pid(lck->data, i)) {
1263 DEBUG(10, ("Found stale duplicate oplock\n"));
1264 continue;
1266 /* Exclusive or batch - can only be one. */
1267 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1268 smb_panic("Bad exclusive or batch oplock entry.");
1270 *pp_ex_or_batch = &lck->data->share_modes[i];
1273 if (LEVEL_II_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1274 if (*pp_batch || *pp_ex_or_batch) {
1275 if (share_mode_stale_pid(lck->data, i)) {
1276 DEBUG(10, ("Found stale LevelII "
1277 "oplock\n"));
1278 continue;
1280 smb_panic("Bad levelII oplock entry.");
1282 *got_level2 = true;
1285 if (lck->data->share_modes[i].op_type == NO_OPLOCK) {
1286 if (*pp_batch || *pp_ex_or_batch) {
1287 if (share_mode_stale_pid(lck->data, i)) {
1288 DEBUG(10, ("Found stale NO_OPLOCK "
1289 "entry\n"));
1290 continue;
1292 smb_panic("Bad no oplock entry.");
1294 *got_no_oplock = true;
1299 static bool delay_for_batch_oplocks(files_struct *fsp,
1300 uint64_t mid,
1301 int oplock_request,
1302 struct share_mode_entry *batch_entry)
1304 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1305 return false;
1307 if (batch_entry == NULL) {
1308 return false;
1311 if (server_id_is_disconnected(&batch_entry->pid)) {
1313 * TODO: clean up.
1314 * This could be achieved by sending a break message
1315 * to ourselves. Special considerations for files
1316 * with delete_on_close flag set!
1318 * For now we keep it simple and do not
1319 * allow delete on close for durable handles.
1321 return false;
1324 /* Found a batch oplock */
1325 send_break_message(fsp, batch_entry, mid, oplock_request);
1326 return true;
1329 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1330 uint64_t mid,
1331 int oplock_request,
1332 struct share_mode_entry *ex_entry)
1334 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1335 return false;
1337 if (ex_entry == NULL) {
1338 return false;
1341 if (server_id_is_disconnected(&ex_entry->pid)) {
1343 * since only durable handles can get disconnected,
1344 * and we can only get durable handles with batch oplocks,
1345 * this should actually never be reached...
1347 return false;
1350 send_break_message(fsp, ex_entry, mid, oplock_request);
1351 return true;
1354 static bool file_has_brlocks(files_struct *fsp)
1356 struct byte_range_lock *br_lck;
1358 br_lck = brl_get_locks_readonly(fsp);
1359 if (!br_lck)
1360 return false;
1362 return br_lck->num_locks > 0 ? true : false;
1365 static void grant_fsp_oplock_type(files_struct *fsp,
1366 int oplock_request,
1367 bool got_level2_oplock,
1368 bool got_a_none_oplock)
1370 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1371 lp_level2_oplocks(SNUM(fsp->conn));
1373 /* Start by granting what the client asked for,
1374 but ensure no SAMBA_PRIVATE bits can be set. */
1375 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1377 if (oplock_request & INTERNAL_OPEN_ONLY) {
1378 /* No oplocks on internal open. */
1379 fsp->oplock_type = NO_OPLOCK;
1380 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1381 fsp->oplock_type, fsp_str_dbg(fsp)));
1382 return;
1385 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1386 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1387 fsp_str_dbg(fsp)));
1388 fsp->oplock_type = NO_OPLOCK;
1391 if (is_stat_open(fsp->access_mask)) {
1392 /* Leave the value already set. */
1393 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1394 fsp->oplock_type, fsp_str_dbg(fsp)));
1395 return;
1399 * Match what was requested (fsp->oplock_type) with
1400 * what was found in the existing share modes.
1403 if (got_a_none_oplock) {
1404 fsp->oplock_type = NO_OPLOCK;
1405 } else if (got_level2_oplock) {
1406 if (fsp->oplock_type == NO_OPLOCK ||
1407 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1408 /* Store a level2 oplock, but don't tell the client */
1409 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1410 } else {
1411 fsp->oplock_type = LEVEL_II_OPLOCK;
1413 } else {
1414 /* All share_mode_entries are placeholders or deferred.
1415 * Silently upgrade to fake levelII if the client didn't
1416 * ask for an oplock. */
1417 if (fsp->oplock_type == NO_OPLOCK) {
1418 /* Store a level2 oplock, but don't tell the client */
1419 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1424 * Don't grant level2 to clients that don't want them
1425 * or if we've turned them off.
1427 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1428 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1431 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1432 fsp->oplock_type, fsp_str_dbg(fsp)));
1435 static bool request_timed_out(struct timeval request_time,
1436 struct timeval timeout)
1438 struct timeval now, end_time;
1439 GetTimeOfDay(&now);
1440 end_time = timeval_sum(&request_time, &timeout);
1441 return (timeval_compare(&end_time, &now) < 0);
1444 /****************************************************************************
1445 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1446 ****************************************************************************/
1448 static void defer_open(struct share_mode_lock *lck,
1449 struct timeval request_time,
1450 struct timeval timeout,
1451 struct smb_request *req,
1452 struct deferred_open_record *state)
1454 struct server_id self = messaging_server_id(req->sconn->msg_ctx);
1456 /* Paranoia check */
1458 if (lck) {
1459 int i;
1461 for (i=0; i<lck->data->num_share_modes; i++) {
1462 struct share_mode_entry *e = &lck->data->share_modes[i];
1464 if (is_deferred_open_entry(e) &&
1465 serverid_equal(&self, &e->pid) &&
1466 (e->op_mid == req->mid)) {
1467 DEBUG(0, ("Trying to defer an already deferred "
1468 "request: mid=%llu, exiting\n",
1469 (unsigned long long)req->mid));
1470 TALLOC_FREE(lck);
1471 exit_server("attempt to defer a deferred request");
1476 /* End paranoia check */
1478 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1479 "open entry for mid %llu\n",
1480 (unsigned int)request_time.tv_sec,
1481 (unsigned int)request_time.tv_usec,
1482 (unsigned long long)req->mid));
1484 if (!push_deferred_open_message_smb(req, request_time, timeout,
1485 state->id, (char *)state, sizeof(*state))) {
1486 TALLOC_FREE(lck);
1487 exit_server("push_deferred_open_message_smb failed");
1489 if (lck) {
1490 add_deferred_open(lck, req->mid, request_time, self, state->id);
1495 /****************************************************************************
1496 On overwrite open ensure that the attributes match.
1497 ****************************************************************************/
1499 static bool open_match_attributes(connection_struct *conn,
1500 uint32 old_dos_attr,
1501 uint32 new_dos_attr,
1502 mode_t existing_unx_mode,
1503 mode_t new_unx_mode,
1504 mode_t *returned_unx_mode)
1506 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1508 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1509 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1511 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1512 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1513 *returned_unx_mode = new_unx_mode;
1514 } else {
1515 *returned_unx_mode = (mode_t)0;
1518 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1519 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1520 "returned_unx_mode = 0%o\n",
1521 (unsigned int)old_dos_attr,
1522 (unsigned int)existing_unx_mode,
1523 (unsigned int)new_dos_attr,
1524 (unsigned int)*returned_unx_mode ));
1526 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1527 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1528 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1529 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1530 return False;
1533 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1534 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1535 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1536 return False;
1539 return True;
1542 /****************************************************************************
1543 Special FCB or DOS processing in the case of a sharing violation.
1544 Try and find a duplicated file handle.
1545 ****************************************************************************/
1547 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1548 connection_struct *conn,
1549 files_struct *fsp_to_dup_into,
1550 const struct smb_filename *smb_fname,
1551 struct file_id id,
1552 uint16 file_pid,
1553 uint64_t vuid,
1554 uint32 access_mask,
1555 uint32 share_access,
1556 uint32 create_options)
1558 files_struct *fsp;
1560 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1561 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1563 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1564 fsp = file_find_di_next(fsp)) {
1566 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1567 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1568 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1569 fsp->fh->fd, (unsigned long long)fsp->vuid,
1570 (unsigned int)fsp->file_pid,
1571 (unsigned int)fsp->fh->private_options,
1572 (unsigned int)fsp->access_mask ));
1574 if (fsp != fsp_to_dup_into &&
1575 fsp->fh->fd != -1 &&
1576 fsp->vuid == vuid &&
1577 fsp->file_pid == file_pid &&
1578 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1579 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1580 (fsp->access_mask & FILE_WRITE_DATA) &&
1581 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1582 strequal(fsp->fsp_name->stream_name,
1583 smb_fname->stream_name)) {
1584 DEBUG(10,("fcb_or_dos_open: file match\n"));
1585 break;
1589 if (!fsp) {
1590 return NT_STATUS_NOT_FOUND;
1593 /* quite an insane set of semantics ... */
1594 if (is_executable(smb_fname->base_name) &&
1595 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1596 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1597 return NT_STATUS_INVALID_PARAMETER;
1600 /* We need to duplicate this fsp. */
1601 return dup_file_fsp(req, fsp, access_mask, share_access,
1602 create_options, fsp_to_dup_into);
1605 static void schedule_defer_open(struct share_mode_lock *lck,
1606 struct timeval request_time,
1607 struct smb_request *req)
1609 struct deferred_open_record state;
1611 /* This is a relative time, added to the absolute
1612 request_time value to get the absolute timeout time.
1613 Note that if this is the second or greater time we enter
1614 this codepath for this particular request mid then
1615 request_time is left as the absolute time of the *first*
1616 time this request mid was processed. This is what allows
1617 the request to eventually time out. */
1619 struct timeval timeout;
1621 /* Normally the smbd we asked should respond within
1622 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1623 * the client did, give twice the timeout as a safety
1624 * measure here in case the other smbd is stuck
1625 * somewhere else. */
1627 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1629 /* Nothing actually uses state.delayed_for_oplocks
1630 but it's handy to differentiate in debug messages
1631 between a 30 second delay due to oplock break, and
1632 a 1 second delay for share mode conflicts. */
1634 state.delayed_for_oplocks = True;
1635 state.async_open = false;
1636 state.id = lck->data->id;
1638 if (!request_timed_out(request_time, timeout)) {
1639 defer_open(lck, request_time, timeout, req, &state);
1643 /****************************************************************************
1644 Reschedule an open call that went asynchronous.
1645 ****************************************************************************/
1647 static void schedule_async_open(struct timeval request_time,
1648 struct smb_request *req)
1650 struct deferred_open_record state;
1651 struct timeval timeout;
1653 timeout = timeval_set(20, 0);
1655 ZERO_STRUCT(state);
1656 state.delayed_for_oplocks = false;
1657 state.async_open = true;
1659 if (!request_timed_out(request_time, timeout)) {
1660 defer_open(NULL, request_time, timeout, req, &state);
1664 /****************************************************************************
1665 Work out what access_mask to use from what the client sent us.
1666 ****************************************************************************/
1668 static NTSTATUS smbd_calculate_maximum_allowed_access(
1669 connection_struct *conn,
1670 const struct smb_filename *smb_fname,
1671 bool use_privs,
1672 uint32_t *p_access_mask)
1674 struct security_descriptor *sd;
1675 uint32_t access_granted;
1676 NTSTATUS status;
1678 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
1679 *p_access_mask |= FILE_GENERIC_ALL;
1680 return NT_STATUS_OK;
1683 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1684 (SECINFO_OWNER |
1685 SECINFO_GROUP |
1686 SECINFO_DACL),&sd);
1688 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1690 * File did not exist
1692 *p_access_mask = FILE_GENERIC_ALL;
1693 return NT_STATUS_OK;
1695 if (!NT_STATUS_IS_OK(status)) {
1696 DEBUG(10,("Could not get acl on file %s: %s\n",
1697 smb_fname_str_dbg(smb_fname),
1698 nt_errstr(status)));
1699 return NT_STATUS_ACCESS_DENIED;
1703 * Never test FILE_READ_ATTRIBUTES. se_file_access_check()
1704 * also takes care of owner WRITE_DAC and READ_CONTROL.
1706 status = se_file_access_check(sd,
1707 get_current_nttok(conn),
1708 use_privs,
1709 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1710 &access_granted);
1712 TALLOC_FREE(sd);
1714 if (!NT_STATUS_IS_OK(status)) {
1715 DEBUG(10, ("Access denied on file %s: "
1716 "when calculating maximum access\n",
1717 smb_fname_str_dbg(smb_fname)));
1718 return NT_STATUS_ACCESS_DENIED;
1720 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1722 if (!(access_granted & DELETE_ACCESS)) {
1723 if (can_delete_file_in_directory(conn, smb_fname)) {
1724 *p_access_mask |= DELETE_ACCESS;
1728 return NT_STATUS_OK;
1731 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1732 const struct smb_filename *smb_fname,
1733 bool use_privs,
1734 uint32_t access_mask,
1735 uint32_t *access_mask_out)
1737 NTSTATUS status;
1738 uint32_t orig_access_mask = access_mask;
1739 uint32_t rejected_share_access;
1742 * Convert GENERIC bits to specific bits.
1745 se_map_generic(&access_mask, &file_generic_mapping);
1747 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1748 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1750 status = smbd_calculate_maximum_allowed_access(
1751 conn, smb_fname, use_privs, &access_mask);
1753 if (!NT_STATUS_IS_OK(status)) {
1754 return status;
1757 access_mask &= conn->share_access;
1760 rejected_share_access = access_mask & ~(conn->share_access);
1762 if (rejected_share_access) {
1763 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1764 "file %s: rejected by share access mask[0x%08X] "
1765 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1766 smb_fname_str_dbg(smb_fname),
1767 conn->share_access,
1768 orig_access_mask, access_mask,
1769 rejected_share_access));
1770 return NT_STATUS_ACCESS_DENIED;
1773 *access_mask_out = access_mask;
1774 return NT_STATUS_OK;
1777 /****************************************************************************
1778 Remove the deferred open entry under lock.
1779 ****************************************************************************/
1781 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1782 struct server_id pid)
1784 struct share_mode_lock *lck = get_existing_share_mode_lock(
1785 talloc_tos(), id);
1786 if (lck == NULL) {
1787 DEBUG(0, ("could not get share mode lock\n"));
1788 return;
1790 del_deferred_open_entry(lck, mid, pid);
1791 TALLOC_FREE(lck);
1794 /****************************************************************************
1795 Return true if this is a state pointer to an asynchronous create.
1796 ****************************************************************************/
1798 bool is_deferred_open_async(const void *ptr)
1800 const struct deferred_open_record *state = (const struct deferred_open_record *)ptr;
1802 return state->async_open;
1805 static bool clear_ads(uint32_t create_disposition)
1807 bool ret = false;
1809 switch (create_disposition) {
1810 case FILE_SUPERSEDE:
1811 case FILE_OVERWRITE_IF:
1812 case FILE_OVERWRITE:
1813 ret = true;
1814 break;
1815 default:
1816 break;
1818 return ret;
1821 static int disposition_to_open_flags(uint32_t create_disposition)
1823 int ret = 0;
1826 * Currently we're using FILE_SUPERSEDE as the same as
1827 * FILE_OVERWRITE_IF but they really are
1828 * different. FILE_SUPERSEDE deletes an existing file
1829 * (requiring delete access) then recreates it.
1832 switch (create_disposition) {
1833 case FILE_SUPERSEDE:
1834 case FILE_OVERWRITE_IF:
1836 * If file exists replace/overwrite. If file doesn't
1837 * exist create.
1839 ret = O_CREAT|O_TRUNC;
1840 break;
1842 case FILE_OPEN:
1844 * If file exists open. If file doesn't exist error.
1846 ret = 0;
1847 break;
1849 case FILE_OVERWRITE:
1851 * If file exists overwrite. If file doesn't exist
1852 * error.
1854 ret = O_TRUNC;
1855 break;
1857 case FILE_CREATE:
1859 * If file exists error. If file doesn't exist create.
1861 ret = O_CREAT|O_EXCL;
1862 break;
1864 case FILE_OPEN_IF:
1866 * If file exists open. If file doesn't exist create.
1868 ret = O_CREAT;
1869 break;
1871 return ret;
1874 static int calculate_open_access_flags(uint32_t access_mask,
1875 int oplock_request,
1876 uint32_t private_flags)
1878 bool need_write, need_read;
1881 * Note that we ignore the append flag as append does not
1882 * mean the same thing under DOS and Unix.
1885 need_write =
1886 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1887 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE));
1889 if (!need_write) {
1890 return O_RDONLY;
1893 /* DENY_DOS opens are always underlying read-write on the
1894 file handle, no matter what the requested access mask
1895 says. */
1897 need_read =
1898 ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1899 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
1900 FILE_READ_EA|FILE_EXECUTE));
1902 if (!need_read) {
1903 return O_WRONLY;
1905 return O_RDWR;
1908 /****************************************************************************
1909 Open a file with a share mode. Passed in an already created files_struct *.
1910 ****************************************************************************/
1912 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1913 struct smb_request *req,
1914 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1915 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1916 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1917 uint32 create_options, /* options such as delete on close. */
1918 uint32 new_dos_attributes, /* attributes used for new file. */
1919 int oplock_request, /* internal Samba oplock codes. */
1920 /* Information (FILE_EXISTS etc.) */
1921 uint32_t private_flags, /* Samba specific flags. */
1922 int *pinfo,
1923 files_struct *fsp)
1925 struct smb_filename *smb_fname = fsp->fsp_name;
1926 int flags=0;
1927 int flags2=0;
1928 bool file_existed = VALID_STAT(smb_fname->st);
1929 bool def_acl = False;
1930 bool posix_open = False;
1931 bool new_file_created = False;
1932 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1933 mode_t new_unx_mode = (mode_t)0;
1934 mode_t unx_mode = (mode_t)0;
1935 int info;
1936 uint32 existing_dos_attributes = 0;
1937 struct timeval request_time = timeval_zero();
1938 struct share_mode_lock *lck = NULL;
1939 uint32 open_access_mask = access_mask;
1940 NTSTATUS status;
1941 char *parent_dir;
1942 SMB_STRUCT_STAT saved_stat = smb_fname->st;
1943 struct share_mode_entry *batch_entry = NULL;
1944 struct share_mode_entry *exclusive_entry = NULL;
1945 bool got_level2_oplock = false;
1946 bool got_a_none_oplock = false;
1947 struct timespec old_write_time;
1948 struct file_id id;
1950 if (conn->printer) {
1952 * Printers are handled completely differently.
1953 * Most of the passed parameters are ignored.
1956 if (pinfo) {
1957 *pinfo = FILE_WAS_CREATED;
1960 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1961 smb_fname_str_dbg(smb_fname)));
1963 if (!req) {
1964 DEBUG(0,("open_file_ntcreate: printer open without "
1965 "an SMB request!\n"));
1966 return NT_STATUS_INTERNAL_ERROR;
1969 return print_spool_open(fsp, smb_fname->base_name,
1970 req->vuid);
1973 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1974 NULL)) {
1975 return NT_STATUS_NO_MEMORY;
1978 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1979 posix_open = True;
1980 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1981 new_dos_attributes = 0;
1982 } else {
1983 /* Windows allows a new file to be created and
1984 silently removes a FILE_ATTRIBUTE_DIRECTORY
1985 sent by the client. Do the same. */
1987 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1989 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1990 * created new. */
1991 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1992 smb_fname, parent_dir);
1995 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1996 "access_mask=0x%x share_access=0x%x "
1997 "create_disposition = 0x%x create_options=0x%x "
1998 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1999 smb_fname_str_dbg(smb_fname), new_dos_attributes,
2000 access_mask, share_access, create_disposition,
2001 create_options, (unsigned int)unx_mode, oplock_request,
2002 (unsigned int)private_flags));
2004 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
2005 DEBUG(0, ("No smb request but not an internal only open!\n"));
2006 return NT_STATUS_INTERNAL_ERROR;
2010 * Only non-internal opens can be deferred at all
2013 if (req) {
2014 void *ptr;
2015 if (get_deferred_open_message_state(req,
2016 &request_time,
2017 &ptr)) {
2018 /* Remember the absolute time of the original
2019 request with this mid. We'll use it later to
2020 see if this has timed out. */
2022 /* If it was an async create retry, the file
2023 didn't exist. */
2025 if (is_deferred_open_async(ptr)) {
2026 SET_STAT_INVALID(smb_fname->st);
2027 file_existed = false;
2028 } else {
2029 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
2030 /* Remove the deferred open entry under lock. */
2031 remove_deferred_open_entry(
2032 state->id, req->mid,
2033 messaging_server_id(req->sconn->msg_ctx));
2036 /* Ensure we don't reprocess this message. */
2037 remove_deferred_open_message_smb(req->sconn, req->mid);
2041 if (!posix_open) {
2042 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
2043 if (file_existed) {
2044 existing_dos_attributes = dos_mode(conn, smb_fname);
2048 /* ignore any oplock requests if oplocks are disabled */
2049 if (!lp_oplocks(SNUM(conn)) ||
2050 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
2051 /* Mask off everything except the private Samba bits. */
2052 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2055 /* this is for OS/2 long file names - say we don't support them */
2056 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
2057 /* OS/2 Workplace shell fix may be main code stream in a later
2058 * release. */
2059 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2060 "supported.\n"));
2061 if (use_nt_status()) {
2062 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2064 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2067 switch( create_disposition ) {
2068 case FILE_OPEN:
2069 /* If file exists open. If file doesn't exist error. */
2070 if (!file_existed) {
2071 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2072 "requested for file %s and file "
2073 "doesn't exist.\n",
2074 smb_fname_str_dbg(smb_fname)));
2075 errno = ENOENT;
2076 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2078 break;
2080 case FILE_OVERWRITE:
2081 /* If file exists overwrite. If file doesn't exist
2082 * error. */
2083 if (!file_existed) {
2084 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2085 "requested for file %s and file "
2086 "doesn't exist.\n",
2087 smb_fname_str_dbg(smb_fname) ));
2088 errno = ENOENT;
2089 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2091 break;
2093 case FILE_CREATE:
2094 /* If file exists error. If file doesn't exist
2095 * create. */
2096 if (file_existed) {
2097 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2098 "requested for file %s and file "
2099 "already exists.\n",
2100 smb_fname_str_dbg(smb_fname)));
2101 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2102 errno = EISDIR;
2103 } else {
2104 errno = EEXIST;
2106 return map_nt_error_from_unix(errno);
2108 break;
2110 case FILE_SUPERSEDE:
2111 case FILE_OVERWRITE_IF:
2112 case FILE_OPEN_IF:
2113 break;
2114 default:
2115 return NT_STATUS_INVALID_PARAMETER;
2118 flags2 = disposition_to_open_flags(create_disposition);
2120 /* We only care about matching attributes on file exists and
2121 * overwrite. */
2123 if (!posix_open && file_existed &&
2124 ((create_disposition == FILE_OVERWRITE) ||
2125 (create_disposition == FILE_OVERWRITE_IF))) {
2126 if (!open_match_attributes(conn, existing_dos_attributes,
2127 new_dos_attributes,
2128 smb_fname->st.st_ex_mode,
2129 unx_mode, &new_unx_mode)) {
2130 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2131 "for file %s (%x %x) (0%o, 0%o)\n",
2132 smb_fname_str_dbg(smb_fname),
2133 existing_dos_attributes,
2134 new_dos_attributes,
2135 (unsigned int)smb_fname->st.st_ex_mode,
2136 (unsigned int)unx_mode ));
2137 errno = EACCES;
2138 return NT_STATUS_ACCESS_DENIED;
2142 status = smbd_calculate_access_mask(conn, smb_fname,
2143 false,
2144 access_mask,
2145 &access_mask);
2146 if (!NT_STATUS_IS_OK(status)) {
2147 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2148 "on file %s returned %s\n",
2149 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2150 return status;
2153 open_access_mask = access_mask;
2155 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
2156 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2159 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2160 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2161 access_mask));
2164 * Note that we ignore the append flag as append does not
2165 * mean the same thing under DOS and Unix.
2168 flags = calculate_open_access_flags(access_mask, oplock_request,
2169 private_flags);
2172 * Currently we only look at FILE_WRITE_THROUGH for create options.
2175 #if defined(O_SYNC)
2176 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2177 flags2 |= O_SYNC;
2179 #endif /* O_SYNC */
2181 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2182 flags2 |= O_APPEND;
2185 if (!posix_open && !CAN_WRITE(conn)) {
2187 * We should really return a permission denied error if either
2188 * O_CREAT or O_TRUNC are set, but for compatibility with
2189 * older versions of Samba we just AND them out.
2191 flags2 &= ~(O_CREAT|O_TRUNC);
2195 * Ensure we can't write on a read-only share or file.
2198 if (flags != O_RDONLY && file_existed &&
2199 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2200 DEBUG(5,("open_file_ntcreate: write access requested for "
2201 "file %s on read only %s\n",
2202 smb_fname_str_dbg(smb_fname),
2203 !CAN_WRITE(conn) ? "share" : "file" ));
2204 errno = EACCES;
2205 return NT_STATUS_ACCESS_DENIED;
2208 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2209 fsp->share_access = share_access;
2210 fsp->fh->private_options = private_flags;
2211 fsp->access_mask = open_access_mask; /* We change this to the
2212 * requested access_mask after
2213 * the open is done. */
2214 fsp->posix_open = posix_open;
2216 /* Ensure no SAMBA_PRIVATE bits can be set. */
2217 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2219 if (timeval_is_zero(&request_time)) {
2220 request_time = fsp->open_time;
2224 * Ensure we pay attention to default ACLs on directories if required.
2227 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2228 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2229 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2232 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2233 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2234 (unsigned int)flags, (unsigned int)flags2,
2235 (unsigned int)unx_mode, (unsigned int)access_mask,
2236 (unsigned int)open_access_mask));
2238 fsp_open = open_file(fsp, conn, req, parent_dir,
2239 flags|flags2, unx_mode, access_mask,
2240 open_access_mask, &new_file_created);
2242 if (!NT_STATUS_IS_OK(fsp_open)) {
2243 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2244 schedule_async_open(request_time, req);
2246 TALLOC_FREE(lck);
2247 return fsp_open;
2250 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2252 * The file did exist, but some other (local or NFS)
2253 * process either renamed/unlinked and re-created the
2254 * file with different dev/ino after we walked the path,
2255 * but before we did the open. We could retry the
2256 * open but it's a rare enough case it's easier to
2257 * just fail the open to prevent creating any problems
2258 * in the open file db having the wrong dev/ino key.
2260 TALLOC_FREE(lck);
2261 fd_close(fsp);
2262 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2263 "Old (dev=0x%llu, ino =0x%llu). "
2264 "New (dev=0x%llu, ino=0x%llu). Failing open "
2265 " with NT_STATUS_ACCESS_DENIED.\n",
2266 smb_fname_str_dbg(smb_fname),
2267 (unsigned long long)saved_stat.st_ex_dev,
2268 (unsigned long long)saved_stat.st_ex_ino,
2269 (unsigned long long)smb_fname->st.st_ex_dev,
2270 (unsigned long long)smb_fname->st.st_ex_ino));
2271 return NT_STATUS_ACCESS_DENIED;
2274 old_write_time = smb_fname->st.st_ex_mtime;
2277 * Deal with the race condition where two smbd's detect the
2278 * file doesn't exist and do the create at the same time. One
2279 * of them will win and set a share mode, the other (ie. this
2280 * one) should check if the requested share mode for this
2281 * create is allowed.
2285 * Now the file exists and fsp is successfully opened,
2286 * fsp->dev and fsp->inode are valid and should replace the
2287 * dev=0,inode=0 from a non existent file. Spotted by
2288 * Nadav Danieli <nadavd@exanet.com>. JRA.
2291 id = fsp->file_id;
2293 lck = get_share_mode_lock(talloc_tos(), id,
2294 conn->connectpath,
2295 smb_fname, &old_write_time);
2297 if (lck == NULL) {
2298 DEBUG(0, ("open_file_ntcreate: Could not get share "
2299 "mode lock for %s\n",
2300 smb_fname_str_dbg(smb_fname)));
2301 fd_close(fsp);
2302 return NT_STATUS_SHARING_VIOLATION;
2305 /* Get the types we need to examine. */
2306 find_oplock_types(fsp,
2307 oplock_request,
2308 lck,
2309 &batch_entry,
2310 &exclusive_entry,
2311 &got_level2_oplock,
2312 &got_a_none_oplock);
2314 /* First pass - send break only on batch oplocks. */
2315 if ((req != NULL) &&
2316 delay_for_batch_oplocks(fsp,
2317 req->mid,
2318 oplock_request,
2319 batch_entry)) {
2320 schedule_defer_open(lck, request_time, req);
2321 TALLOC_FREE(lck);
2322 fd_close(fsp);
2323 return NT_STATUS_SHARING_VIOLATION;
2326 status = open_mode_check(conn, lck, fsp->name_hash,
2327 access_mask, share_access,
2328 create_options, &file_existed);
2330 if (NT_STATUS_IS_OK(status)) {
2331 /* We might be going to allow this open. Check oplock
2332 * status again. */
2333 /* Second pass - send break for both batch or
2334 * exclusive oplocks. */
2335 if ((req != NULL) &&
2336 delay_for_exclusive_oplocks(
2337 fsp,
2338 req->mid,
2339 oplock_request,
2340 exclusive_entry)) {
2341 schedule_defer_open(lck, request_time, req);
2342 TALLOC_FREE(lck);
2343 fd_close(fsp);
2344 return NT_STATUS_SHARING_VIOLATION;
2348 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2349 /* DELETE_PENDING is not deferred for a second */
2350 TALLOC_FREE(lck);
2351 return status;
2354 if (!NT_STATUS_IS_OK(status)) {
2355 uint32 can_access_mask;
2356 bool can_access = True;
2358 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2360 /* Check if this can be done with the deny_dos and fcb
2361 * calls. */
2362 if (private_flags &
2363 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2364 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2365 if (req == NULL) {
2366 DEBUG(0, ("DOS open without an SMB "
2367 "request!\n"));
2368 TALLOC_FREE(lck);
2369 return NT_STATUS_INTERNAL_ERROR;
2372 /* Use the client requested access mask here,
2373 * not the one we open with. */
2374 status = fcb_or_dos_open(req,
2375 conn,
2376 fsp,
2377 smb_fname,
2379 req->smbpid,
2380 req->vuid,
2381 access_mask,
2382 share_access,
2383 create_options);
2385 if (NT_STATUS_IS_OK(status)) {
2386 TALLOC_FREE(lck);
2387 if (pinfo) {
2388 *pinfo = FILE_WAS_OPENED;
2390 return NT_STATUS_OK;
2395 * This next line is a subtlety we need for
2396 * MS-Access. If a file open will fail due to share
2397 * permissions and also for security (access) reasons,
2398 * we need to return the access failed error, not the
2399 * share error. We can't open the file due to kernel
2400 * oplock deadlock (it's possible we failed above on
2401 * the open_mode_check()) so use a userspace check.
2404 if (flags & O_RDWR) {
2405 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2406 } else if (flags & O_WRONLY) {
2407 can_access_mask = FILE_WRITE_DATA;
2408 } else {
2409 can_access_mask = FILE_READ_DATA;
2412 if (((can_access_mask & FILE_WRITE_DATA) &&
2413 !CAN_WRITE(conn)) ||
2414 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2415 smb_fname,
2416 false,
2417 can_access_mask))) {
2418 can_access = False;
2422 * If we're returning a share violation, ensure we
2423 * cope with the braindead 1 second delay.
2426 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2427 lp_defer_sharing_violations()) {
2428 struct timeval timeout;
2429 struct deferred_open_record state;
2430 int timeout_usecs;
2432 /* this is a hack to speed up torture tests
2433 in 'make test' */
2434 timeout_usecs = lp_parm_int(SNUM(conn),
2435 "smbd","sharedelay",
2436 SHARING_VIOLATION_USEC_WAIT);
2438 /* This is a relative time, added to the absolute
2439 request_time value to get the absolute timeout time.
2440 Note that if this is the second or greater time we enter
2441 this codepath for this particular request mid then
2442 request_time is left as the absolute time of the *first*
2443 time this request mid was processed. This is what allows
2444 the request to eventually time out. */
2446 timeout = timeval_set(0, timeout_usecs);
2448 /* Nothing actually uses state.delayed_for_oplocks
2449 but it's handy to differentiate in debug messages
2450 between a 30 second delay due to oplock break, and
2451 a 1 second delay for share mode conflicts. */
2453 state.delayed_for_oplocks = False;
2454 state.async_open = false;
2455 state.id = id;
2457 if ((req != NULL)
2458 && !request_timed_out(request_time,
2459 timeout)) {
2460 defer_open(lck, request_time, timeout,
2461 req, &state);
2465 TALLOC_FREE(lck);
2466 if (can_access) {
2468 * We have detected a sharing violation here
2469 * so return the correct error code
2471 status = NT_STATUS_SHARING_VIOLATION;
2472 } else {
2473 status = NT_STATUS_ACCESS_DENIED;
2475 return status;
2478 grant_fsp_oplock_type(fsp,
2479 oplock_request,
2480 got_level2_oplock,
2481 got_a_none_oplock);
2484 * We have the share entry *locked*.....
2487 SMB_ASSERT(lck != NULL);
2489 /* Delete streams if create_disposition requires it */
2490 if (!new_file_created && clear_ads(create_disposition) &&
2491 !is_ntfs_stream_smb_fname(smb_fname)) {
2492 status = delete_all_streams(conn, smb_fname->base_name);
2493 if (!NT_STATUS_IS_OK(status)) {
2494 TALLOC_FREE(lck);
2495 fd_close(fsp);
2496 return status;
2500 /* note that we ignore failure for the following. It is
2501 basically a hack for NFS, and NFS will never set one of
2502 these only read them. Nobody but Samba can ever set a deny
2503 mode and we have already checked our more authoritative
2504 locking database for permission to set this deny mode. If
2505 the kernel refuses the operations then the kernel is wrong.
2506 note that GPFS supports it as well - jmcd */
2508 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
2509 int ret_flock;
2510 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2511 if(ret_flock == -1 ){
2513 TALLOC_FREE(lck);
2514 fd_close(fsp);
2516 return NT_STATUS_SHARING_VIOLATION;
2521 * At this point onwards, we can guarantee that the share entry
2522 * is locked, whether we created the file or not, and that the
2523 * deny mode is compatible with all current opens.
2527 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2528 * but we don't have to store this - just ignore it on access check.
2530 if (conn->sconn->using_smb2) {
2532 * SMB2 doesn't return it (according to Microsoft tests).
2533 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2534 * File created with access = 0x7 (Read, Write, Delete)
2535 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2537 fsp->access_mask = access_mask;
2538 } else {
2539 /* But SMB1 does. */
2540 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2543 if (file_existed) {
2544 /* stat opens on existing files don't get oplocks. */
2545 if (is_stat_open(open_access_mask)) {
2546 fsp->oplock_type = NO_OPLOCK;
2550 if (new_file_created) {
2551 info = FILE_WAS_CREATED;
2552 } else {
2553 if (flags2 & O_TRUNC) {
2554 info = FILE_WAS_OVERWRITTEN;
2555 } else {
2556 info = FILE_WAS_OPENED;
2560 if (pinfo) {
2561 *pinfo = info;
2565 * Setup the oplock info in both the shared memory and
2566 * file structs.
2569 status = set_file_oplock(fsp, fsp->oplock_type);
2570 if (!NT_STATUS_IS_OK(status)) {
2572 * Could not get the kernel oplock or there are byte-range
2573 * locks on the file.
2575 fsp->oplock_type = NO_OPLOCK;
2578 set_share_mode(lck, fsp, get_current_uid(conn),
2579 req ? req->mid : 0,
2580 fsp->oplock_type);
2582 /* Handle strange delete on close create semantics. */
2583 if (create_options & FILE_DELETE_ON_CLOSE) {
2585 status = can_set_delete_on_close(fsp, new_dos_attributes);
2587 if (!NT_STATUS_IS_OK(status)) {
2588 /* Remember to delete the mode we just added. */
2589 del_share_mode(lck, fsp);
2590 TALLOC_FREE(lck);
2591 fd_close(fsp);
2592 return status;
2594 /* Note that here we set the *inital* delete on close flag,
2595 not the regular one. The magic gets handled in close. */
2596 fsp->initial_delete_on_close = True;
2599 if (info != FILE_WAS_OPENED) {
2600 /* Files should be initially set as archive */
2601 if (lp_map_archive(SNUM(conn)) ||
2602 lp_store_dos_attributes(SNUM(conn))) {
2603 if (!posix_open) {
2604 if (file_set_dosmode(conn, smb_fname,
2605 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2606 parent_dir, true) == 0) {
2607 unx_mode = smb_fname->st.st_ex_mode;
2613 /* Determine sparse flag. */
2614 if (posix_open) {
2615 /* POSIX opens are sparse by default. */
2616 fsp->is_sparse = true;
2617 } else {
2618 fsp->is_sparse = (file_existed &&
2619 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2623 * Take care of inherited ACLs on created files - if default ACL not
2624 * selected.
2627 if (!posix_open && new_file_created && !def_acl) {
2629 int saved_errno = errno; /* We might get ENOSYS in the next
2630 * call.. */
2632 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2633 errno == ENOSYS) {
2634 errno = saved_errno; /* Ignore ENOSYS */
2637 } else if (new_unx_mode) {
2639 int ret = -1;
2641 /* Attributes need changing. File already existed. */
2644 int saved_errno = errno; /* We might get ENOSYS in the
2645 * next call.. */
2646 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2648 if (ret == -1 && errno == ENOSYS) {
2649 errno = saved_errno; /* Ignore ENOSYS */
2650 } else {
2651 DEBUG(5, ("open_file_ntcreate: reset "
2652 "attributes of file %s to 0%o\n",
2653 smb_fname_str_dbg(smb_fname),
2654 (unsigned int)new_unx_mode));
2655 ret = 0; /* Don't do the fchmod below. */
2659 if ((ret == -1) &&
2660 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2661 DEBUG(5, ("open_file_ntcreate: failed to reset "
2662 "attributes of file %s to 0%o\n",
2663 smb_fname_str_dbg(smb_fname),
2664 (unsigned int)new_unx_mode));
2667 /* If this is a successful open, we must remove any deferred open
2668 * records. */
2669 if (req != NULL) {
2670 del_deferred_open_entry(lck, req->mid,
2671 messaging_server_id(req->sconn->msg_ctx));
2673 TALLOC_FREE(lck);
2675 return NT_STATUS_OK;
2679 /****************************************************************************
2680 Open a file for for write to ensure that we can fchmod it.
2681 ****************************************************************************/
2683 NTSTATUS open_file_fchmod(connection_struct *conn,
2684 struct smb_filename *smb_fname,
2685 files_struct **result)
2687 if (!VALID_STAT(smb_fname->st)) {
2688 return NT_STATUS_INVALID_PARAMETER;
2691 return SMB_VFS_CREATE_FILE(
2692 conn, /* conn */
2693 NULL, /* req */
2694 0, /* root_dir_fid */
2695 smb_fname, /* fname */
2696 FILE_WRITE_DATA, /* access_mask */
2697 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2698 FILE_SHARE_DELETE),
2699 FILE_OPEN, /* create_disposition*/
2700 0, /* create_options */
2701 0, /* file_attributes */
2702 INTERNAL_OPEN_ONLY, /* oplock_request */
2703 0, /* allocation_size */
2704 0, /* private_flags */
2705 NULL, /* sd */
2706 NULL, /* ea_list */
2707 result, /* result */
2708 NULL); /* pinfo */
2711 static NTSTATUS mkdir_internal(connection_struct *conn,
2712 struct smb_filename *smb_dname,
2713 uint32 file_attributes)
2715 mode_t mode;
2716 char *parent_dir = NULL;
2717 NTSTATUS status;
2718 bool posix_open = false;
2719 bool need_re_stat = false;
2720 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2722 if (!CAN_WRITE(conn) || (access_mask & ~(conn->share_access))) {
2723 DEBUG(5,("mkdir_internal: failing share access "
2724 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
2725 return NT_STATUS_ACCESS_DENIED;
2728 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2729 NULL)) {
2730 return NT_STATUS_NO_MEMORY;
2733 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2734 posix_open = true;
2735 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2736 } else {
2737 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2740 status = check_parent_access(conn,
2741 smb_dname,
2742 access_mask);
2743 if(!NT_STATUS_IS_OK(status)) {
2744 DEBUG(5,("mkdir_internal: check_parent_access "
2745 "on directory %s for path %s returned %s\n",
2746 parent_dir,
2747 smb_dname->base_name,
2748 nt_errstr(status) ));
2749 return status;
2752 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2753 return map_nt_error_from_unix(errno);
2756 /* Ensure we're checking for a symlink here.... */
2757 /* We don't want to get caught by a symlink racer. */
2759 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2760 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2761 smb_fname_str_dbg(smb_dname), strerror(errno)));
2762 return map_nt_error_from_unix(errno);
2765 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2766 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2767 smb_fname_str_dbg(smb_dname)));
2768 return NT_STATUS_NOT_A_DIRECTORY;
2771 if (lp_store_dos_attributes(SNUM(conn))) {
2772 if (!posix_open) {
2773 file_set_dosmode(conn, smb_dname,
2774 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2775 parent_dir, true);
2779 if (lp_inherit_perms(SNUM(conn))) {
2780 inherit_access_posix_acl(conn, parent_dir,
2781 smb_dname->base_name, mode);
2782 need_re_stat = true;
2785 if (!posix_open) {
2787 * Check if high bits should have been set,
2788 * then (if bits are missing): add them.
2789 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2790 * dir.
2792 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2793 (mode & ~smb_dname->st.st_ex_mode)) {
2794 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2795 (smb_dname->st.st_ex_mode |
2796 (mode & ~smb_dname->st.st_ex_mode)));
2797 need_re_stat = true;
2801 /* Change the owner if required. */
2802 if (lp_inherit_owner(SNUM(conn))) {
2803 change_dir_owner_to_parent(conn, parent_dir,
2804 smb_dname->base_name,
2805 &smb_dname->st);
2806 need_re_stat = true;
2809 if (need_re_stat) {
2810 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2811 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2812 smb_fname_str_dbg(smb_dname), strerror(errno)));
2813 return map_nt_error_from_unix(errno);
2817 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2818 smb_dname->base_name);
2820 return NT_STATUS_OK;
2823 /****************************************************************************
2824 Open a directory from an NT SMB call.
2825 ****************************************************************************/
2827 static NTSTATUS open_directory(connection_struct *conn,
2828 struct smb_request *req,
2829 struct smb_filename *smb_dname,
2830 uint32 access_mask,
2831 uint32 share_access,
2832 uint32 create_disposition,
2833 uint32 create_options,
2834 uint32 file_attributes,
2835 int *pinfo,
2836 files_struct **result)
2838 files_struct *fsp = NULL;
2839 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2840 struct share_mode_lock *lck = NULL;
2841 NTSTATUS status;
2842 struct timespec mtimespec;
2843 int info = 0;
2845 if (is_ntfs_stream_smb_fname(smb_dname)) {
2846 DEBUG(2, ("open_directory: %s is a stream name!\n",
2847 smb_fname_str_dbg(smb_dname)));
2848 return NT_STATUS_NOT_A_DIRECTORY;
2851 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2852 /* Ensure we have a directory attribute. */
2853 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2856 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2857 "share_access = 0x%x create_options = 0x%x, "
2858 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2859 smb_fname_str_dbg(smb_dname),
2860 (unsigned int)access_mask,
2861 (unsigned int)share_access,
2862 (unsigned int)create_options,
2863 (unsigned int)create_disposition,
2864 (unsigned int)file_attributes));
2866 status = smbd_calculate_access_mask(conn, smb_dname, false,
2867 access_mask, &access_mask);
2868 if (!NT_STATUS_IS_OK(status)) {
2869 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2870 "on file %s returned %s\n",
2871 smb_fname_str_dbg(smb_dname),
2872 nt_errstr(status)));
2873 return status;
2876 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2877 !security_token_has_privilege(get_current_nttok(conn),
2878 SEC_PRIV_SECURITY)) {
2879 DEBUG(10, ("open_directory: open on %s "
2880 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2881 smb_fname_str_dbg(smb_dname)));
2882 return NT_STATUS_PRIVILEGE_NOT_HELD;
2885 switch( create_disposition ) {
2886 case FILE_OPEN:
2888 if (!dir_existed) {
2889 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2892 info = FILE_WAS_OPENED;
2893 break;
2895 case FILE_CREATE:
2897 /* If directory exists error. If directory doesn't
2898 * exist create. */
2900 if (dir_existed) {
2901 status = NT_STATUS_OBJECT_NAME_COLLISION;
2902 DEBUG(2, ("open_directory: unable to create "
2903 "%s. Error was %s\n",
2904 smb_fname_str_dbg(smb_dname),
2905 nt_errstr(status)));
2906 return status;
2909 status = mkdir_internal(conn, smb_dname,
2910 file_attributes);
2912 if (!NT_STATUS_IS_OK(status)) {
2913 DEBUG(2, ("open_directory: unable to create "
2914 "%s. Error was %s\n",
2915 smb_fname_str_dbg(smb_dname),
2916 nt_errstr(status)));
2917 return status;
2920 info = FILE_WAS_CREATED;
2921 break;
2923 case FILE_OPEN_IF:
2925 * If directory exists open. If directory doesn't
2926 * exist create.
2929 if (dir_existed) {
2930 status = NT_STATUS_OK;
2931 info = FILE_WAS_OPENED;
2932 } else {
2933 status = mkdir_internal(conn, smb_dname,
2934 file_attributes);
2936 if (NT_STATUS_IS_OK(status)) {
2937 info = FILE_WAS_CREATED;
2938 } else {
2939 /* Cope with create race. */
2940 if (!NT_STATUS_EQUAL(status,
2941 NT_STATUS_OBJECT_NAME_COLLISION)) {
2942 DEBUG(2, ("open_directory: unable to create "
2943 "%s. Error was %s\n",
2944 smb_fname_str_dbg(smb_dname),
2945 nt_errstr(status)));
2946 return status;
2948 info = FILE_WAS_OPENED;
2952 break;
2954 case FILE_SUPERSEDE:
2955 case FILE_OVERWRITE:
2956 case FILE_OVERWRITE_IF:
2957 default:
2958 DEBUG(5,("open_directory: invalid create_disposition "
2959 "0x%x for directory %s\n",
2960 (unsigned int)create_disposition,
2961 smb_fname_str_dbg(smb_dname)));
2962 return NT_STATUS_INVALID_PARAMETER;
2965 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2966 DEBUG(5,("open_directory: %s is not a directory !\n",
2967 smb_fname_str_dbg(smb_dname)));
2968 return NT_STATUS_NOT_A_DIRECTORY;
2971 if (info == FILE_WAS_OPENED) {
2972 status = smbd_check_access_rights(conn,
2973 smb_dname,
2974 false,
2975 access_mask);
2976 if (!NT_STATUS_IS_OK(status)) {
2977 DEBUG(10, ("open_directory: smbd_check_access_rights on "
2978 "file %s failed with %s\n",
2979 smb_fname_str_dbg(smb_dname),
2980 nt_errstr(status)));
2981 return status;
2985 status = file_new(req, conn, &fsp);
2986 if(!NT_STATUS_IS_OK(status)) {
2987 return status;
2991 * Setup the files_struct for it.
2994 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2995 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2996 fsp->file_pid = req ? req->smbpid : 0;
2997 fsp->can_lock = False;
2998 fsp->can_read = False;
2999 fsp->can_write = False;
3001 fsp->share_access = share_access;
3002 fsp->fh->private_options = 0;
3004 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3006 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3007 fsp->print_file = NULL;
3008 fsp->modified = False;
3009 fsp->oplock_type = NO_OPLOCK;
3010 fsp->sent_oplock_break = NO_BREAK_SENT;
3011 fsp->is_directory = True;
3012 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
3013 status = fsp_set_smb_fname(fsp, smb_dname);
3014 if (!NT_STATUS_IS_OK(status)) {
3015 file_free(req, fsp);
3016 return status;
3019 mtimespec = smb_dname->st.st_ex_mtime;
3021 #ifdef O_DIRECTORY
3022 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3023 #else
3024 /* POSIX allows us to open a directory with O_RDONLY. */
3025 status = fd_open(conn, fsp, O_RDONLY, 0);
3026 #endif
3027 if (!NT_STATUS_IS_OK(status)) {
3028 DEBUG(5, ("open_directory: Could not open fd for "
3029 "%s (%s)\n",
3030 smb_fname_str_dbg(smb_dname),
3031 nt_errstr(status)));
3032 file_free(req, fsp);
3033 return status;
3036 status = vfs_stat_fsp(fsp);
3037 if (!NT_STATUS_IS_OK(status)) {
3038 fd_close(fsp);
3039 file_free(req, fsp);
3040 return status;
3043 /* Ensure there was no race condition. */
3044 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
3045 DEBUG(5,("open_directory: stat struct differs for "
3046 "directory %s.\n",
3047 smb_fname_str_dbg(smb_dname)));
3048 fd_close(fsp);
3049 file_free(req, fsp);
3050 return NT_STATUS_ACCESS_DENIED;
3053 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3054 conn->connectpath, smb_dname,
3055 &mtimespec);
3057 if (lck == NULL) {
3058 DEBUG(0, ("open_directory: Could not get share mode lock for "
3059 "%s\n", smb_fname_str_dbg(smb_dname)));
3060 fd_close(fsp);
3061 file_free(req, fsp);
3062 return NT_STATUS_SHARING_VIOLATION;
3065 status = open_mode_check(conn, lck, fsp->name_hash,
3066 access_mask, share_access,
3067 create_options, &dir_existed);
3069 if (!NT_STATUS_IS_OK(status)) {
3070 TALLOC_FREE(lck);
3071 fd_close(fsp);
3072 file_free(req, fsp);
3073 return status;
3076 set_share_mode(lck, fsp, get_current_uid(conn),
3077 req ? req->mid : 0, NO_OPLOCK);
3079 /* For directories the delete on close bit at open time seems
3080 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3081 if (create_options & FILE_DELETE_ON_CLOSE) {
3082 status = can_set_delete_on_close(fsp, 0);
3083 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3084 TALLOC_FREE(lck);
3085 fd_close(fsp);
3086 file_free(req, fsp);
3087 return status;
3090 if (NT_STATUS_IS_OK(status)) {
3091 /* Note that here we set the *inital* delete on close flag,
3092 not the regular one. The magic gets handled in close. */
3093 fsp->initial_delete_on_close = True;
3097 TALLOC_FREE(lck);
3099 if (pinfo) {
3100 *pinfo = info;
3103 *result = fsp;
3104 return NT_STATUS_OK;
3107 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3108 struct smb_filename *smb_dname)
3110 NTSTATUS status;
3111 files_struct *fsp;
3113 status = SMB_VFS_CREATE_FILE(
3114 conn, /* conn */
3115 req, /* req */
3116 0, /* root_dir_fid */
3117 smb_dname, /* fname */
3118 FILE_READ_ATTRIBUTES, /* access_mask */
3119 FILE_SHARE_NONE, /* share_access */
3120 FILE_CREATE, /* create_disposition*/
3121 FILE_DIRECTORY_FILE, /* create_options */
3122 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3123 0, /* oplock_request */
3124 0, /* allocation_size */
3125 0, /* private_flags */
3126 NULL, /* sd */
3127 NULL, /* ea_list */
3128 &fsp, /* result */
3129 NULL); /* pinfo */
3131 if (NT_STATUS_IS_OK(status)) {
3132 close_file(req, fsp, NORMAL_CLOSE);
3135 return status;
3138 /****************************************************************************
3139 Receive notification that one of our open files has been renamed by another
3140 smbd process.
3141 ****************************************************************************/
3143 void msg_file_was_renamed(struct messaging_context *msg,
3144 void *private_data,
3145 uint32_t msg_type,
3146 struct server_id server_id,
3147 DATA_BLOB *data)
3149 files_struct *fsp;
3150 char *frm = (char *)data->data;
3151 struct file_id id;
3152 const char *sharepath;
3153 const char *base_name;
3154 const char *stream_name;
3155 struct smb_filename *smb_fname = NULL;
3156 size_t sp_len, bn_len;
3157 NTSTATUS status;
3158 struct smbd_server_connection *sconn =
3159 talloc_get_type_abort(private_data,
3160 struct smbd_server_connection);
3162 if (data->data == NULL
3163 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3164 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3165 (int)data->length));
3166 return;
3169 /* Unpack the message. */
3170 pull_file_id_24(frm, &id);
3171 sharepath = &frm[24];
3172 sp_len = strlen(sharepath);
3173 base_name = sharepath + sp_len + 1;
3174 bn_len = strlen(base_name);
3175 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3177 /* stream_name must always be NULL if there is no stream. */
3178 if (stream_name[0] == '\0') {
3179 stream_name = NULL;
3182 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3183 stream_name, NULL, &smb_fname);
3184 if (!NT_STATUS_IS_OK(status)) {
3185 return;
3188 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3189 "file_id %s\n",
3190 sharepath, smb_fname_str_dbg(smb_fname),
3191 file_id_string_tos(&id)));
3193 for(fsp = file_find_di_first(sconn, id); fsp;
3194 fsp = file_find_di_next(fsp)) {
3195 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3197 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3198 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3199 smb_fname_str_dbg(smb_fname)));
3200 status = fsp_set_smb_fname(fsp, smb_fname);
3201 if (!NT_STATUS_IS_OK(status)) {
3202 goto out;
3204 } else {
3205 /* TODO. JRA. */
3206 /* Now we have the complete path we can work out if this is
3207 actually within this share and adjust newname accordingly. */
3208 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3209 "not sharepath %s) "
3210 "%s from %s -> %s\n",
3211 fsp->conn->connectpath,
3212 sharepath,
3213 fsp_fnum_dbg(fsp),
3214 fsp_str_dbg(fsp),
3215 smb_fname_str_dbg(smb_fname)));
3218 out:
3219 TALLOC_FREE(smb_fname);
3220 return;
3224 * If a main file is opened for delete, all streams need to be checked for
3225 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3226 * If that works, delete them all by setting the delete on close and close.
3229 NTSTATUS open_streams_for_delete(connection_struct *conn,
3230 const char *fname)
3232 struct stream_struct *stream_info = NULL;
3233 files_struct **streams = NULL;
3234 int i;
3235 unsigned int num_streams = 0;
3236 TALLOC_CTX *frame = talloc_stackframe();
3237 NTSTATUS status;
3239 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3240 &num_streams, &stream_info);
3242 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3243 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3244 DEBUG(10, ("no streams around\n"));
3245 TALLOC_FREE(frame);
3246 return NT_STATUS_OK;
3249 if (!NT_STATUS_IS_OK(status)) {
3250 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3251 nt_errstr(status)));
3252 goto fail;
3255 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3256 num_streams));
3258 if (num_streams == 0) {
3259 TALLOC_FREE(frame);
3260 return NT_STATUS_OK;
3263 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3264 if (streams == NULL) {
3265 DEBUG(0, ("talloc failed\n"));
3266 status = NT_STATUS_NO_MEMORY;
3267 goto fail;
3270 for (i=0; i<num_streams; i++) {
3271 struct smb_filename *smb_fname = NULL;
3273 if (strequal(stream_info[i].name, "::$DATA")) {
3274 streams[i] = NULL;
3275 continue;
3278 status = create_synthetic_smb_fname(talloc_tos(), fname,
3279 stream_info[i].name,
3280 NULL, &smb_fname);
3281 if (!NT_STATUS_IS_OK(status)) {
3282 goto fail;
3285 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3286 DEBUG(10, ("Unable to stat stream: %s\n",
3287 smb_fname_str_dbg(smb_fname)));
3290 status = SMB_VFS_CREATE_FILE(
3291 conn, /* conn */
3292 NULL, /* req */
3293 0, /* root_dir_fid */
3294 smb_fname, /* fname */
3295 DELETE_ACCESS, /* access_mask */
3296 (FILE_SHARE_READ | /* share_access */
3297 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3298 FILE_OPEN, /* create_disposition*/
3299 0, /* create_options */
3300 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3301 0, /* oplock_request */
3302 0, /* allocation_size */
3303 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3304 NULL, /* sd */
3305 NULL, /* ea_list */
3306 &streams[i], /* result */
3307 NULL); /* pinfo */
3309 if (!NT_STATUS_IS_OK(status)) {
3310 DEBUG(10, ("Could not open stream %s: %s\n",
3311 smb_fname_str_dbg(smb_fname),
3312 nt_errstr(status)));
3314 TALLOC_FREE(smb_fname);
3315 break;
3317 TALLOC_FREE(smb_fname);
3321 * don't touch the variable "status" beyond this point :-)
3324 for (i -= 1 ; i >= 0; i--) {
3325 if (streams[i] == NULL) {
3326 continue;
3329 DEBUG(10, ("Closing stream # %d, %s\n", i,
3330 fsp_str_dbg(streams[i])));
3331 close_file(NULL, streams[i], NORMAL_CLOSE);
3334 fail:
3335 TALLOC_FREE(frame);
3336 return status;
3339 /*********************************************************************
3340 Create a default ACL by inheriting from the parent. If no inheritance
3341 from the parent available, don't set anything. This will leave the actual
3342 permissions the new file or directory already got from the filesystem
3343 as the NT ACL when read.
3344 *********************************************************************/
3346 static NTSTATUS inherit_new_acl(files_struct *fsp)
3348 TALLOC_CTX *ctx = talloc_tos();
3349 char *parent_name = NULL;
3350 struct security_descriptor *parent_desc = NULL;
3351 NTSTATUS status = NT_STATUS_OK;
3352 struct security_descriptor *psd = NULL;
3353 struct dom_sid *owner_sid = NULL;
3354 struct dom_sid *group_sid = NULL;
3355 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3356 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3357 bool inheritable_components = false;
3358 size_t size = 0;
3360 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3361 return NT_STATUS_NO_MEMORY;
3364 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3365 parent_name,
3366 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3367 &parent_desc);
3368 if (!NT_STATUS_IS_OK(status)) {
3369 return status;
3372 inheritable_components = sd_has_inheritable_components(parent_desc,
3373 fsp->is_directory);
3375 if (!inheritable_components && !inherit_owner) {
3376 /* Nothing to inherit and not setting owner. */
3377 return NT_STATUS_OK;
3380 /* Create an inherited descriptor from the parent. */
3382 if (DEBUGLEVEL >= 10) {
3383 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3384 fsp_str_dbg(fsp) ));
3385 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3388 /* Inherit from parent descriptor if "inherit owner" set. */
3389 if (inherit_owner) {
3390 owner_sid = parent_desc->owner_sid;
3391 group_sid = parent_desc->group_sid;
3394 if (owner_sid == NULL) {
3395 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3397 if (group_sid == NULL) {
3398 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3401 status = se_create_child_secdesc(ctx,
3402 &psd,
3403 &size,
3404 parent_desc,
3405 owner_sid,
3406 group_sid,
3407 fsp->is_directory);
3408 if (!NT_STATUS_IS_OK(status)) {
3409 return status;
3412 /* If inheritable_components == false,
3413 se_create_child_secdesc()
3414 creates a security desriptor with a NULL dacl
3415 entry, but with SEC_DESC_DACL_PRESENT. We need
3416 to remove that flag. */
3418 if (!inheritable_components) {
3419 security_info_sent &= ~SECINFO_DACL;
3420 psd->type &= ~SEC_DESC_DACL_PRESENT;
3423 if (DEBUGLEVEL >= 10) {
3424 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3425 fsp_str_dbg(fsp) ));
3426 NDR_PRINT_DEBUG(security_descriptor, psd);
3429 if (inherit_owner) {
3430 /* We need to be root to force this. */
3431 become_root();
3433 status = SMB_VFS_FSET_NT_ACL(fsp,
3434 security_info_sent,
3435 psd);
3436 if (inherit_owner) {
3437 unbecome_root();
3439 return status;
3443 * Wrapper around open_file_ntcreate and open_directory
3446 static NTSTATUS create_file_unixpath(connection_struct *conn,
3447 struct smb_request *req,
3448 struct smb_filename *smb_fname,
3449 uint32_t access_mask,
3450 uint32_t share_access,
3451 uint32_t create_disposition,
3452 uint32_t create_options,
3453 uint32_t file_attributes,
3454 uint32_t oplock_request,
3455 uint64_t allocation_size,
3456 uint32_t private_flags,
3457 struct security_descriptor *sd,
3458 struct ea_list *ea_list,
3460 files_struct **result,
3461 int *pinfo)
3463 int info = FILE_WAS_OPENED;
3464 files_struct *base_fsp = NULL;
3465 files_struct *fsp = NULL;
3466 NTSTATUS status;
3468 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3469 "file_attributes = 0x%x, share_access = 0x%x, "
3470 "create_disposition = 0x%x create_options = 0x%x "
3471 "oplock_request = 0x%x private_flags = 0x%x "
3472 "ea_list = 0x%p, sd = 0x%p, "
3473 "fname = %s\n",
3474 (unsigned int)access_mask,
3475 (unsigned int)file_attributes,
3476 (unsigned int)share_access,
3477 (unsigned int)create_disposition,
3478 (unsigned int)create_options,
3479 (unsigned int)oplock_request,
3480 (unsigned int)private_flags,
3481 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3483 if (create_options & FILE_OPEN_BY_FILE_ID) {
3484 status = NT_STATUS_NOT_SUPPORTED;
3485 goto fail;
3488 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3489 status = NT_STATUS_INVALID_PARAMETER;
3490 goto fail;
3493 if (req == NULL) {
3494 oplock_request |= INTERNAL_OPEN_ONLY;
3497 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3498 && (access_mask & DELETE_ACCESS)
3499 && !is_ntfs_stream_smb_fname(smb_fname)) {
3501 * We can't open a file with DELETE access if any of the
3502 * streams is open without FILE_SHARE_DELETE
3504 status = open_streams_for_delete(conn, smb_fname->base_name);
3506 if (!NT_STATUS_IS_OK(status)) {
3507 goto fail;
3511 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3512 !security_token_has_privilege(get_current_nttok(conn),
3513 SEC_PRIV_SECURITY)) {
3514 DEBUG(10, ("create_file_unixpath: open on %s "
3515 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3516 smb_fname_str_dbg(smb_fname)));
3517 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3518 goto fail;
3521 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3522 && is_ntfs_stream_smb_fname(smb_fname)
3523 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3524 uint32 base_create_disposition;
3525 struct smb_filename *smb_fname_base = NULL;
3527 if (create_options & FILE_DIRECTORY_FILE) {
3528 status = NT_STATUS_NOT_A_DIRECTORY;
3529 goto fail;
3532 switch (create_disposition) {
3533 case FILE_OPEN:
3534 base_create_disposition = FILE_OPEN;
3535 break;
3536 default:
3537 base_create_disposition = FILE_OPEN_IF;
3538 break;
3541 /* Create an smb_filename with stream_name == NULL. */
3542 status = create_synthetic_smb_fname(talloc_tos(),
3543 smb_fname->base_name,
3544 NULL, NULL,
3545 &smb_fname_base);
3546 if (!NT_STATUS_IS_OK(status)) {
3547 goto fail;
3550 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3551 DEBUG(10, ("Unable to stat stream: %s\n",
3552 smb_fname_str_dbg(smb_fname_base)));
3555 /* Open the base file. */
3556 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3557 FILE_SHARE_READ
3558 | FILE_SHARE_WRITE
3559 | FILE_SHARE_DELETE,
3560 base_create_disposition,
3561 0, 0, 0, 0, 0, NULL, NULL,
3562 &base_fsp, NULL);
3563 TALLOC_FREE(smb_fname_base);
3565 if (!NT_STATUS_IS_OK(status)) {
3566 DEBUG(10, ("create_file_unixpath for base %s failed: "
3567 "%s\n", smb_fname->base_name,
3568 nt_errstr(status)));
3569 goto fail;
3571 /* we don't need to low level fd */
3572 fd_close(base_fsp);
3576 * If it's a request for a directory open, deal with it separately.
3579 if (create_options & FILE_DIRECTORY_FILE) {
3581 if (create_options & FILE_NON_DIRECTORY_FILE) {
3582 status = NT_STATUS_INVALID_PARAMETER;
3583 goto fail;
3586 /* Can't open a temp directory. IFS kit test. */
3587 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3588 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3589 status = NT_STATUS_INVALID_PARAMETER;
3590 goto fail;
3594 * We will get a create directory here if the Win32
3595 * app specified a security descriptor in the
3596 * CreateDirectory() call.
3599 oplock_request = 0;
3600 status = open_directory(
3601 conn, req, smb_fname, access_mask, share_access,
3602 create_disposition, create_options, file_attributes,
3603 &info, &fsp);
3604 } else {
3607 * Ordinary file case.
3610 status = file_new(req, conn, &fsp);
3611 if(!NT_STATUS_IS_OK(status)) {
3612 goto fail;
3615 status = fsp_set_smb_fname(fsp, smb_fname);
3616 if (!NT_STATUS_IS_OK(status)) {
3617 goto fail;
3620 if (base_fsp) {
3622 * We're opening the stream element of a
3623 * base_fsp we already opened. Set up the
3624 * base_fsp pointer.
3626 fsp->base_fsp = base_fsp;
3629 if (allocation_size) {
3630 fsp->initial_allocation_size = smb_roundup(fsp->conn,
3631 allocation_size);
3634 status = open_file_ntcreate(conn,
3635 req,
3636 access_mask,
3637 share_access,
3638 create_disposition,
3639 create_options,
3640 file_attributes,
3641 oplock_request,
3642 private_flags,
3643 &info,
3644 fsp);
3646 if(!NT_STATUS_IS_OK(status)) {
3647 file_free(req, fsp);
3648 fsp = NULL;
3651 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3653 /* A stream open never opens a directory */
3655 if (base_fsp) {
3656 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3657 goto fail;
3661 * Fail the open if it was explicitly a non-directory
3662 * file.
3665 if (create_options & FILE_NON_DIRECTORY_FILE) {
3666 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3667 goto fail;
3670 oplock_request = 0;
3671 status = open_directory(
3672 conn, req, smb_fname, access_mask,
3673 share_access, create_disposition,
3674 create_options, file_attributes,
3675 &info, &fsp);
3679 if (!NT_STATUS_IS_OK(status)) {
3680 goto fail;
3683 fsp->base_fsp = base_fsp;
3685 if ((ea_list != NULL) &&
3686 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3687 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3688 if (!NT_STATUS_IS_OK(status)) {
3689 goto fail;
3693 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3694 status = NT_STATUS_ACCESS_DENIED;
3695 goto fail;
3698 /* Save the requested allocation size. */
3699 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3700 if (allocation_size
3701 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3702 fsp->initial_allocation_size = smb_roundup(
3703 fsp->conn, allocation_size);
3704 if (fsp->is_directory) {
3705 /* Can't set allocation size on a directory. */
3706 status = NT_STATUS_ACCESS_DENIED;
3707 goto fail;
3709 if (vfs_allocate_file_space(
3710 fsp, fsp->initial_allocation_size) == -1) {
3711 status = NT_STATUS_DISK_FULL;
3712 goto fail;
3714 } else {
3715 fsp->initial_allocation_size = smb_roundup(
3716 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3718 } else {
3719 fsp->initial_allocation_size = 0;
3722 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3723 fsp->base_fsp == NULL) {
3724 if (sd != NULL) {
3726 * According to the MS documentation, the only time the security
3727 * descriptor is applied to the opened file is iff we *created* the
3728 * file; an existing file stays the same.
3730 * Also, it seems (from observation) that you can open the file with
3731 * any access mask but you can still write the sd. We need to override
3732 * the granted access before we call set_sd
3733 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3736 uint32_t sec_info_sent;
3737 uint32_t saved_access_mask = fsp->access_mask;
3739 sec_info_sent = get_sec_info(sd);
3741 fsp->access_mask = FILE_GENERIC_ALL;
3743 if (sec_info_sent & (SECINFO_OWNER|
3744 SECINFO_GROUP|
3745 SECINFO_DACL|
3746 SECINFO_SACL)) {
3747 status = set_sd(fsp, sd, sec_info_sent);
3750 fsp->access_mask = saved_access_mask;
3752 if (!NT_STATUS_IS_OK(status)) {
3753 goto fail;
3755 } else if (lp_inherit_acls(SNUM(conn))) {
3756 /* Inherit from parent. Errors here are not fatal. */
3757 status = inherit_new_acl(fsp);
3758 if (!NT_STATUS_IS_OK(status)) {
3759 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3760 fsp_str_dbg(fsp),
3761 nt_errstr(status) ));
3766 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3768 *result = fsp;
3769 if (pinfo != NULL) {
3770 *pinfo = info;
3773 smb_fname->st = fsp->fsp_name->st;
3775 return NT_STATUS_OK;
3777 fail:
3778 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3780 if (fsp != NULL) {
3781 if (base_fsp && fsp->base_fsp == base_fsp) {
3783 * The close_file below will close
3784 * fsp->base_fsp.
3786 base_fsp = NULL;
3788 close_file(req, fsp, ERROR_CLOSE);
3789 fsp = NULL;
3791 if (base_fsp != NULL) {
3792 close_file(req, base_fsp, ERROR_CLOSE);
3793 base_fsp = NULL;
3795 return status;
3799 * Calculate the full path name given a relative fid.
3801 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3802 struct smb_request *req,
3803 uint16_t root_dir_fid,
3804 const struct smb_filename *smb_fname,
3805 struct smb_filename **smb_fname_out)
3807 files_struct *dir_fsp;
3808 char *parent_fname = NULL;
3809 char *new_base_name = NULL;
3810 NTSTATUS status;
3812 if (root_dir_fid == 0 || !smb_fname) {
3813 status = NT_STATUS_INTERNAL_ERROR;
3814 goto out;
3817 dir_fsp = file_fsp(req, root_dir_fid);
3819 if (dir_fsp == NULL) {
3820 status = NT_STATUS_INVALID_HANDLE;
3821 goto out;
3824 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3825 status = NT_STATUS_INVALID_HANDLE;
3826 goto out;
3829 if (!dir_fsp->is_directory) {
3832 * Check to see if this is a mac fork of some kind.
3835 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3836 is_ntfs_stream_smb_fname(smb_fname)) {
3837 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3838 goto out;
3842 we need to handle the case when we get a
3843 relative open relative to a file and the
3844 pathname is blank - this is a reopen!
3845 (hint from demyn plantenberg)
3848 status = NT_STATUS_INVALID_HANDLE;
3849 goto out;
3852 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3854 * We're at the toplevel dir, the final file name
3855 * must not contain ./, as this is filtered out
3856 * normally by srvstr_get_path and unix_convert
3857 * explicitly rejects paths containing ./.
3859 parent_fname = talloc_strdup(talloc_tos(), "");
3860 if (parent_fname == NULL) {
3861 status = NT_STATUS_NO_MEMORY;
3862 goto out;
3864 } else {
3865 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3868 * Copy in the base directory name.
3871 parent_fname = talloc_array(talloc_tos(), char,
3872 dir_name_len+2);
3873 if (parent_fname == NULL) {
3874 status = NT_STATUS_NO_MEMORY;
3875 goto out;
3877 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3878 dir_name_len+1);
3881 * Ensure it ends in a '/'.
3882 * We used TALLOC_SIZE +2 to add space for the '/'.
3885 if(dir_name_len
3886 && (parent_fname[dir_name_len-1] != '\\')
3887 && (parent_fname[dir_name_len-1] != '/')) {
3888 parent_fname[dir_name_len] = '/';
3889 parent_fname[dir_name_len+1] = '\0';
3893 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3894 smb_fname->base_name);
3895 if (new_base_name == NULL) {
3896 status = NT_STATUS_NO_MEMORY;
3897 goto out;
3900 status = filename_convert(req,
3901 conn,
3902 req->flags2 & FLAGS2_DFS_PATHNAMES,
3903 new_base_name,
3905 NULL,
3906 smb_fname_out);
3907 if (!NT_STATUS_IS_OK(status)) {
3908 goto out;
3911 out:
3912 TALLOC_FREE(parent_fname);
3913 TALLOC_FREE(new_base_name);
3914 return status;
3917 NTSTATUS create_file_default(connection_struct *conn,
3918 struct smb_request *req,
3919 uint16_t root_dir_fid,
3920 struct smb_filename *smb_fname,
3921 uint32_t access_mask,
3922 uint32_t share_access,
3923 uint32_t create_disposition,
3924 uint32_t create_options,
3925 uint32_t file_attributes,
3926 uint32_t oplock_request,
3927 uint64_t allocation_size,
3928 uint32_t private_flags,
3929 struct security_descriptor *sd,
3930 struct ea_list *ea_list,
3931 files_struct **result,
3932 int *pinfo)
3934 int info = FILE_WAS_OPENED;
3935 files_struct *fsp = NULL;
3936 NTSTATUS status;
3937 bool stream_name = false;
3939 DEBUG(10,("create_file: access_mask = 0x%x "
3940 "file_attributes = 0x%x, share_access = 0x%x, "
3941 "create_disposition = 0x%x create_options = 0x%x "
3942 "oplock_request = 0x%x "
3943 "private_flags = 0x%x "
3944 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3945 "fname = %s\n",
3946 (unsigned int)access_mask,
3947 (unsigned int)file_attributes,
3948 (unsigned int)share_access,
3949 (unsigned int)create_disposition,
3950 (unsigned int)create_options,
3951 (unsigned int)oplock_request,
3952 (unsigned int)private_flags,
3953 (unsigned int)root_dir_fid,
3954 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3957 * Calculate the filename from the root_dir_if if necessary.
3960 if (root_dir_fid != 0) {
3961 struct smb_filename *smb_fname_out = NULL;
3962 status = get_relative_fid_filename(conn, req, root_dir_fid,
3963 smb_fname, &smb_fname_out);
3964 if (!NT_STATUS_IS_OK(status)) {
3965 goto fail;
3967 smb_fname = smb_fname_out;
3971 * Check to see if this is a mac fork of some kind.
3974 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3975 if (stream_name) {
3976 enum FAKE_FILE_TYPE fake_file_type;
3978 fake_file_type = is_fake_file(smb_fname);
3980 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3983 * Here we go! support for changing the disk quotas
3984 * --metze
3986 * We need to fake up to open this MAGIC QUOTA file
3987 * and return a valid FID.
3989 * w2k close this file directly after openening xp
3990 * also tries a QUERY_FILE_INFO on the file and then
3991 * close it
3993 status = open_fake_file(req, conn, req->vuid,
3994 fake_file_type, smb_fname,
3995 access_mask, &fsp);
3996 if (!NT_STATUS_IS_OK(status)) {
3997 goto fail;
4000 ZERO_STRUCT(smb_fname->st);
4001 goto done;
4004 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
4005 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4006 goto fail;
4010 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
4011 int ret;
4012 smb_fname->stream_name = NULL;
4013 /* We have to handle this error here. */
4014 if (create_options & FILE_DIRECTORY_FILE) {
4015 status = NT_STATUS_NOT_A_DIRECTORY;
4016 goto fail;
4018 if (lp_posix_pathnames()) {
4019 ret = SMB_VFS_LSTAT(conn, smb_fname);
4020 } else {
4021 ret = SMB_VFS_STAT(conn, smb_fname);
4024 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
4025 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4026 goto fail;
4030 status = create_file_unixpath(
4031 conn, req, smb_fname, access_mask, share_access,
4032 create_disposition, create_options, file_attributes,
4033 oplock_request, allocation_size, private_flags,
4034 sd, ea_list,
4035 &fsp, &info);
4037 if (!NT_STATUS_IS_OK(status)) {
4038 goto fail;
4041 done:
4042 DEBUG(10, ("create_file: info=%d\n", info));
4044 *result = fsp;
4045 if (pinfo != NULL) {
4046 *pinfo = info;
4048 return NT_STATUS_OK;
4050 fail:
4051 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
4053 if (fsp != NULL) {
4054 close_file(req, fsp, ERROR_CLOSE);
4055 fsp = NULL;
4057 return status;