****************************************************************************/
static BOOL fd_open(struct connection_struct *conn,
- const char *fname,
- files_struct *fsp,
- int flags,
- mode_t mode)
+ const char *fname,
+ files_struct *fsp,
+ int flags,
+ mode_t mode)
{
int sav;
****************************************************************************/
int fd_close(struct connection_struct *conn,
- files_struct *fsp)
+ files_struct *fsp)
{
if (fsp->fh->fd == -1) {
return 0; /* What we used to call a stat open. */
****************************************************************************/
void change_owner_to_parent(connection_struct *conn,
- files_struct *fsp,
- const char *fname,
- SMB_STRUCT_STAT *psbuf)
+ files_struct *fsp,
+ const char *fname,
+ SMB_STRUCT_STAT *psbuf)
{
const char *parent_path = parent_dirname(fname);
SMB_STRUCT_STAT parent_st;
****************************************************************************/
static NTSTATUS open_file(files_struct *fsp,
- connection_struct *conn,
- const char *fname,
- SMB_STRUCT_STAT *psbuf,
- int flags,
- mode_t unx_mode,
- uint32 access_mask)
+ connection_struct *conn,
+ const char *fname,
+ SMB_STRUCT_STAT *psbuf,
+ int flags,
+ mode_t unx_mode,
+ uint32 access_mask, /* client requested access mask. */
+ uint32 open_access_mask) /* what we're actually using in the open. */
{
int accmode = (flags & O_ACCMODE);
int local_flags = flags;
local_flags = (flags & ~O_ACCMODE)|O_RDWR;
}
- if ((access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
+ if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
(!file_existed && (local_flags & O_CREAT)) ||
((local_flags & O_TRUNC) == O_TRUNC) ) {
}
/* Inherit the ACL if the file was created. */
- if ((local_flags & O_CREAT) && !file_existed) {
+ if ((local_flags & O_CREAT)
+ && !file_existed
+ && lp_inherit_perms(SNUM(conn))) {
inherit_access_acl(conn, fname, unx_mode);
}
if (!CAN_WRITE(conn)) {
fsp->can_write = False;
} else {
- fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False;
+ fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
+ True : False;
}
fsp->print_file = False;
fsp->modified = False;
fsp->wcp = NULL; /* Write cache pointer. */
DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
- *current_user_info.smb_name ? current_user_info.smb_name : conn->user,fsp->fsp_name,
+ *current_user_info.smb_name ?
+ current_user_info.smb_name : conn->user,fsp->fsp_name,
BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
conn->num_files_open + 1));
if (is_deferred_open_entry(share_entry) &&
!open_was_deferred(share_entry->op_mid)) {
pstring str;
- DEBUG(0, ("Got a deferred entry without a request: "
- "PANIC: %s\n", share_mode_str(num, share_entry)));
+ pstr_sprintf(str, "Got a deferred entry without a request: "
+ "PANIC: %s\n", share_mode_str(num, share_entry));
smb_panic(str);
}
*/
static BOOL delay_for_oplocks(struct share_mode_lock *lck,
- files_struct *fsp,
- int pass_number,
- int oplock_request)
+ files_struct *fsp,
+ int pass_number,
+ int oplock_request)
{
int i;
struct share_mode_entry *exclusive = NULL;
BOOL valid_entry = False;
BOOL delay_it = False;
BOOL have_level2 = False;
+ BOOL ret;
+ char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
if (oplock_request & INTERNAL_OPEN_ONLY) {
fsp->oplock_type = NO_OPLOCK;
fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
}
- if (delay_it) {
- BOOL ret;
- char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
+ if (!delay_it) {
+ return False;
+ }
- DEBUG(10, ("Sending break request to PID %s\n",
- procid_str_static(&exclusive->pid)));
- exclusive->op_mid = get_current_mid();
+ /*
+ * Send a break message to the oplock holder and delay the open for
+ * our client.
+ */
- /* Create the message. */
- share_mode_entry_to_message(msg, exclusive);
+ DEBUG(10, ("Sending break request to PID %s\n",
+ procid_str_static(&exclusive->pid)));
+ exclusive->op_mid = get_current_mid();
- /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We don't
- want this set in the share mode struct pointed to by lck. */
+ /* Create the message. */
+ share_mode_entry_to_message(msg, exclusive);
- if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
- SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
- }
+ /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
+ don't want this set in the share mode struct pointed to by lck. */
- become_root();
- ret = message_send_pid(exclusive->pid, MSG_SMB_BREAK_REQUEST,
- msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
- unbecome_root();
- if (!ret) {
- DEBUG(3, ("Could not send oplock break message\n"));
- }
+ if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
+ SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
+ }
+
+ ret = message_send_pid(exclusive->pid, MSG_SMB_BREAK_REQUEST,
+ msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
+ if (!ret) {
+ DEBUG(3, ("Could not send oplock break message\n"));
}
- return delay_it;
+ return True;
}
static BOOL request_timed_out(struct timeval request_time,
srv_defer_sign_response(mid);
}
-/****************************************************************************
- Set a kernel flock on a file for NFS interoperability.
- This requires a patch to Linux.
-****************************************************************************/
-
-static void kernel_flock(files_struct *fsp, uint32 share_mode)
-{
-#if HAVE_KERNEL_SHARE_MODES
- int kernel_mode = 0;
- if (share_mode == FILE_SHARE_WRITE) {
- kernel_mode = LOCK_MAND|LOCK_WRITE;
- } else if (share_mode == FILE_SHARE_READ) {
- kernel_mode = LOCK_MAND|LOCK_READ;
- } else if (share_mode == FILE_SHARE_NONE) {
- kernel_mode = LOCK_MAND;
- }
- if (kernel_mode) {
- flock(fsp->fh->fd, kernel_mode);
- }
-#endif
- ;
-}
/****************************************************************************
On overwrite open ensure that the attributes match.
****************************************************************************/
static BOOL open_match_attributes(connection_struct *conn,
- const char *path,
- uint32 old_dos_attr,
- uint32 new_dos_attr,
- mode_t existing_unx_mode,
- mode_t new_unx_mode,
- mode_t *returned_unx_mode)
+ const char *path,
+ uint32 old_dos_attr,
+ uint32 new_dos_attr,
+ mode_t existing_unx_mode,
+ mode_t new_unx_mode,
+ mode_t *returned_unx_mode)
{
uint32 noarch_old_dos_attr, noarch_new_dos_attr;
****************************************************************************/
BOOL map_open_params_to_ntcreate(const char *fname, int deny_mode, int open_func,
- uint32 *paccess_mask,
- uint32 *pshare_mode,
- uint32 *pcreate_disposition,
- uint32 *pcreate_options)
+ uint32 *paccess_mask,
+ uint32 *pshare_mode,
+ uint32 *pcreate_disposition,
+ uint32 *pcreate_options)
{
uint32 access_mask;
uint32 share_mode;
****************************************************************************/
NTSTATUS open_file_ntcreate(connection_struct *conn,
- const char *fname,
- SMB_STRUCT_STAT *psbuf,
- uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
- uint32 share_access, /* share constants (FILE_SHARE_READ etc). */
- uint32 create_disposition, /* FILE_OPEN_IF etc. */
- uint32 create_options, /* options such as delete on close. */
- uint32 new_dos_attributes, /* attributes used for new file. */
- int oplock_request, /* internal Samba oplock codes. */
- /* Information (FILE_EXISTS etc.) */
- int *pinfo,
- files_struct **result)
+ const char *fname,
+ SMB_STRUCT_STAT *psbuf,
+ uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
+ uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
+ uint32 create_disposition, /* FILE_OPEN_IF etc. */
+ uint32 create_options, /* options such as delete on close. */
+ uint32 new_dos_attributes, /* attributes used for new file. */
+ int oplock_request, /* internal Samba oplock codes. */
+ /* Information (FILE_EXISTS etc.) */
+ int *pinfo,
+ files_struct **result)
{
int flags=0;
int flags2=0;
uint16 mid = get_current_mid();
struct timeval request_time = timeval_zero();
struct share_mode_lock *lck = NULL;
+ uint32 open_access_mask = access_mask;
NTSTATUS status;
+ int ret_flock;
if (conn->printer) {
/*
DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n", fname));
- return print_fsp_open(conn, fname, &fsp);
+ return print_fsp_open(conn, fname, result);
}
/* We add aARCH to this as this mode is only used if the file is
/* This is a nasty hack - must fix... JRA. */
if (access_mask == MAXIMUM_ALLOWED_ACCESS) {
- access_mask = FILE_GENERIC_ALL;
+ open_access_mask = access_mask = FILE_GENERIC_ALL;
}
/*
*/
se_map_generic(&access_mask, &file_generic_mapping);
+ open_access_mask = access_mask;
+
+ if (flags2 & O_TRUNC) {
+ open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
+ }
DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
"access_mask=0x%x\n", fname, access_mask ));
*/
if (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) {
- flags = O_RDWR;
+ /* DENY_DOS opens are always underlying read-write on the
+ file handle, no matter what the requested access mask
+ says. */
+ if ((create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
+ access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
+ flags = O_RDWR;
+ } else {
+ flags = O_WRONLY;
+ }
} else {
flags = O_RDONLY;
}
fsp->inode = psbuf->st_ino;
fsp->share_access = share_access;
fsp->fh->private_options = create_options;
- fsp->access_mask = access_mask;
+ fsp->access_mask = open_access_mask; /* We change this to the
+ * requested access_mask after
+ * the open is done. */
/* Ensure no SAMBA_PRIVATE bits can be set. */
fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
inode = psbuf->st_ino;
lck = get_share_mode_lock(NULL, dev, inode,
- conn->connectpath,
- fname);
+ conn->connectpath,
+ fname);
if (lck == NULL) {
file_free(fsp);
return NT_STATUS_SHARING_VIOLATION;
}
+ /* Use the client requested access mask here, not the one we
+ * open with. */
status = open_mode_check(conn, fname, lck,
access_mask, share_access,
create_options, &file_existed);
if (NT_STATUS_IS_OK(status)) {
- /* We might be going to allow this open. Check oplock status again. */
- /* Second pass - send break for both batch or exclusive oplocks. */
+ /* We might be going to allow this open. Check oplock
+ * status again. */
+ /* Second pass - send break for both batch or
+ * exclusive oplocks. */
if (delay_for_oplocks(lck, fsp, 2, oplock_request)) {
schedule_defer_open(lck, request_time);
TALLOC_FREE(lck);
(NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
files_struct *fsp_dup;
+
+ /* Use the client requested access mask here,
+ * not the one we open with. */
fsp_dup = fcb_or_dos_open(conn, fname, dev,
inode, access_mask,
share_access,
if (flags & O_RDWR) {
can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
+ } else if (flags & O_WRONLY) {
+ can_access_mask = FILE_WRITE_DATA;
} else {
can_access_mask = FILE_READ_DATA;
}
- if (((flags & O_RDWR) && !CAN_WRITE(conn)) ||
- !can_access_file(conn,fname,psbuf,can_access_mask)) {
+ if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
+ !can_access_file(conn,fname,psbuf,can_access_mask)) {
can_access = False;
}
unx_mode = 0777;
}
- DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
+ DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
+ "access_mask = 0x%x, open_access_mask = 0x%x\n",
(unsigned int)flags, (unsigned int)flags2,
- (unsigned int)unx_mode));
-
- /* Drop the lock before doing any real file access. Allows kernel
- oplock breaks to be processed. Handle any races after the open
- call when we re-acquire the lock. */
-
- if (lck) {
- TALLOC_FREE(lck);
- }
+ (unsigned int)unx_mode, (unsigned int)access_mask,
+ (unsigned int)open_access_mask));
/*
* open_file strips any O_TRUNC flags itself.
*/
fsp_open = open_file(fsp,conn,fname,psbuf,flags|flags2,unx_mode,
- access_mask);
+ access_mask, open_access_mask);
if (!NT_STATUS_IS_OK(fsp_open)) {
+ if (lck != NULL) {
+ TALLOC_FREE(lck);
+ }
file_free(fsp);
return fsp_open;
}
- /*
- * Deal with the race condition where two smbd's detect the
- * file doesn't exist and do the create at the same time. One
- * of them will win and set a share mode, the other (ie. this
- * one) should check if the requested share mode for this
- * create is allowed.
- */
-
- /*
- * Now the file exists and fsp is successfully opened,
- * fsp->dev and fsp->inode are valid and should replace the
- * dev=0,inode=0 from a non existent file. Spotted by
- * Nadav Danieli <nadavd@exanet.com>. JRA.
- */
-
- dev = fsp->dev;
- inode = fsp->inode;
-
- lck = get_share_mode_lock(NULL, dev, inode,
- conn->connectpath,
- fname);
+ if (!file_existed) {
- if (lck == NULL) {
- DEBUG(0, ("open_file_ntcreate: Could not get share mode lock for %s\n", fname));
- fd_close(conn, fsp);
- file_free(fsp);
- return NT_STATUS_SHARING_VIOLATION;
- }
+ /*
+ * Deal with the race condition where two smbd's detect the
+ * file doesn't exist and do the create at the same time. One
+ * of them will win and set a share mode, the other (ie. this
+ * one) should check if the requested share mode for this
+ * create is allowed.
+ */
- /*
- * The share entry is again *locked*.....
- */
+ /*
+ * Now the file exists and fsp is successfully opened,
+ * fsp->dev and fsp->inode are valid and should replace the
+ * dev=0,inode=0 from a non existent file. Spotted by
+ * Nadav Danieli <nadavd@exanet.com>. JRA.
+ */
- /* First pass - send break only on batch oplocks. */
- if (delay_for_oplocks(lck, fsp, 1, oplock_request)) {
- schedule_defer_open(lck, request_time);
- fd_close(conn, fsp);
- file_free(fsp);
- TALLOC_FREE(lck);
- return NT_STATUS_SHARING_VIOLATION;
- }
+ dev = fsp->dev;
+ inode = fsp->inode;
- status = open_mode_check(conn, fname, lck,
- access_mask, share_access,
- create_options, &file_existed);
+ lck = get_share_mode_lock(NULL, dev, inode,
+ conn->connectpath,
+ fname);
- if (NT_STATUS_IS_OK(status)) {
- /* We might be going to allow this open. Check oplock status again. */
- /* Second pass - send break for both batch or exclusive oplocks. */
- if (delay_for_oplocks(lck, fsp, 2, oplock_request)) {
- schedule_defer_open(lck, request_time);
+ if (lck == NULL) {
+ DEBUG(0, ("open_file_ntcreate: Could not get share "
+ "mode lock for %s\n", fname));
fd_close(conn, fsp);
file_free(fsp);
- TALLOC_FREE(lck);
return NT_STATUS_SHARING_VIOLATION;
}
- }
- if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
- /* DELETE_PENDING is not deferred for a second */
- fd_close(conn, fsp);
- file_free(fsp);
- TALLOC_FREE(lck);
- return status;
- }
+ status = open_mode_check(conn, fname, lck,
+ access_mask, share_access,
+ create_options, &file_existed);
- if (!NT_STATUS_IS_OK(status)) {
- struct deferred_open_record state;
+ if (!NT_STATUS_IS_OK(status)) {
+ struct deferred_open_record state;
- fd_close(conn, fsp);
- file_free(fsp);
+ fd_close(conn, fsp);
+ file_free(fsp);
- state.delayed_for_oplocks = False;
- state.dev = dev;
- state.inode = inode;
+ state.delayed_for_oplocks = False;
+ state.dev = dev;
+ state.inode = inode;
- /* Do it all over again immediately. In the second
- * round we will find that the file existed and handle
- * the DELETE_PENDING and FCB cases correctly. No need
- * to duplicate the code here. Essentially this is a
- * "goto top of this function", but don't tell
- * anybody... */
+ /* Do it all over again immediately. In the second
+ * round we will find that the file existed and handle
+ * the DELETE_PENDING and FCB cases correctly. No need
+ * to duplicate the code here. Essentially this is a
+ * "goto top of this function", but don't tell
+ * anybody... */
+
+ defer_open(lck, request_time, timeval_zero(),
+ &state);
+ TALLOC_FREE(lck);
+ return status;
+ }
+
+ /*
+ * We exit this block with the share entry *locked*.....
+ */
- defer_open(lck, request_time, timeval_zero(),
- &state);
- TALLOC_FREE(lck);
- return status;
}
+ SMB_ASSERT(lck != NULL);
+
/* note that we ignore failure for the following. It is
basically a hack for NFS, and NFS will never set one of
these only read them. Nobody but Samba can ever set a deny
mode and we have already checked our more authoritative
locking database for permission to set this deny mode. If
- the kernel refuses the operations then the kernel is wrong */
+ the kernel refuses the operations then the kernel is wrong.
+ note that GPFS supports it as well - jmcd */
+
+ ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, fsp->fh->fd, share_access);
+ if(ret_flock == -1 ){
- kernel_flock(fsp, share_access);
+ talloc_free(lck);
+ fd_close(conn, fsp);
+ file_free(fsp);
+
+ return NT_STATUS_SHARING_VIOLATION;
+ }
/*
* At this point onwards, we can guarentee that the share entry
if (file_existed) {
/* stat opens on existing files don't get oplocks. */
- if (is_stat_open(fsp->access_mask)) {
+ if (is_stat_open(open_access_mask)) {
fsp->oplock_type = NO_OPLOCK;
}
set_share_mode(lck, fsp, current_user.ut.uid, 0, fsp->oplock_type);
if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED ||
- info == FILE_WAS_SUPERSEDED) {
+ info == FILE_WAS_SUPERSEDED) {
/* Handle strange delete on close create semantics. */
if (create_options & FILE_DELETE_ON_CLOSE) {
int saved_errno = errno; /* We might get ENOSYS in the next
* call.. */
- if (SMB_VFS_FCHMOD_ACL(fsp, fsp->fh->fd, unx_mode) == -1
- && errno == ENOSYS) {
+ if (SMB_VFS_FCHMOD_ACL(fsp, fsp->fh->fd, unx_mode) == -1 &&
+ errno == ENOSYS) {
errno = saved_errno; /* Ignore ENOSYS */
}
} else {
DEBUG(5, ("open_file_ntcreate: reset "
"attributes of file %s to 0%o\n",
- fname, (unsigned int)new_unx_mode));
+ fname, (unsigned int)new_unx_mode));
ret = 0; /* Don't do the fchmod below. */
}
}
(SMB_VFS_FCHMOD(fsp, fsp->fh->fd, new_unx_mode) == -1))
DEBUG(5, ("open_file_ntcreate: failed to reset "
"attributes of file %s to 0%o\n",
- fname, (unsigned int)new_unx_mode));
+ fname, (unsigned int)new_unx_mode));
}
/* If this is a successful open, we must remove any deferred open
/* note! we must use a non-zero desired access or we don't get
a real file descriptor. Oh what a twisted web we weave. */
- status = open_file(fsp,conn,fname,psbuf,O_WRONLY,0,FILE_WRITE_DATA);
+ status = open_file(fsp,conn,fname,psbuf,O_WRONLY,0,FILE_WRITE_DATA,FILE_WRITE_DATA);
/*
* This is not a user visible file open.
****************************************************************************/
NTSTATUS open_directory(connection_struct *conn,
- const char *fname,
- SMB_STRUCT_STAT *psbuf,
- uint32 access_mask,
- uint32 share_access,
- uint32 create_disposition,
- uint32 create_options,
- int *pinfo,
- files_struct **result)
+ const char *fname,
+ SMB_STRUCT_STAT *psbuf,
+ uint32 access_mask,
+ uint32 share_access,
+ uint32 create_disposition,
+ uint32 create_options,
+ int *pinfo,
+ files_struct **result)
{
files_struct *fsp = NULL;
BOOL dir_existed = VALID_STAT(*psbuf) ? True : False;
string_set(&fsp->fsp_name,fname);
lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode,
- conn->connectpath,
- fname);
+ conn->connectpath,
+ fname);
if (lck == NULL) {
DEBUG(0, ("open_directory: Could not get share mode lock for %s\n", fname));
Open a pseudo-file (no locking checks - a 'stat' open).
****************************************************************************/
-NTSTATUS open_file_stat(connection_struct *conn, char *fname,
+NTSTATUS open_file_stat(connection_struct *conn, const char *fname,
SMB_STRUCT_STAT *psbuf, files_struct **result)
{
files_struct *fsp = NULL;