smb_common_open.c revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This module provides the common open functionality to the various
* open and create SMB interface functions.
*/
#include <smbsrv/smb_incl.h>
#include <smbsrv/smb_fsops.h>
#include <smbsrv/ntstatus.h>
#define DENY_RW(share_access) \
/*
* The default stability mode is to perform the write-through
* behaviour requested by the client.
*/
int smb_stable_mode = 0;
/*
* This macro is used to delete a newly created object
* if any error happens after creation of object.
*/
#define SMB_DEL_NEWOBJ(obj) \
if (created) { \
if (is_dir) \
else \
}
/*
* smb_set_stability
*
* Set the default stability mode. Normal (mode is zero) means perform
* the write-through behaviour requested by the client. Synchronous
* (mode is non-zero) means journal everything regardless of the write
* through behaviour requested by the client.
*/
void
smb_set_stability(int mode)
{
}
/*
* smb_access_generic_to_file
*
* Search MSDN for IoCreateFile to see following mapping.
*
* GENERIC_READ STANDARD_RIGHTS_READ, FILE_READ_DATA,
* FILE_READ_ATTRIBUTES and FILE_READ_EA
*
* GENERIC_WRITE STANDARD_RIGHTS_WRITE, FILE_WRITE_DATA,
* FILE_WRITE_ATTRIBUTES, FILE_WRITE_EA, and FILE_APPEND_DATA
*
* GENERIC_EXECUTE STANDARD_RIGHTS_EXECUTE, SYNCHRONIZE, and FILE_EXECUTE.
*/
{
if (desired_access & GENERIC_ALL)
return (FILE_ALL_ACCESS & ~SYNCHRONIZE);
if (desired_access & GENERIC_EXECUTE) {
}
if (desired_access & GENERIC_WRITE) {
}
if (desired_access & GENERIC_READ) {
}
return (access | desired_access);
}
/*
* smb_omode_to_amask
*
* This function converts open modes used by Open and Open AndX
* commands to desired access bits used by NT Create AndX command.
*/
{
switch (desired_access & SMB_DA_ACCESS_MASK) {
case SMB_DA_ACCESS_READ:
return (FILE_GENERIC_READ);
case SMB_DA_ACCESS_WRITE:
return (FILE_GENERIC_WRITE);
case SMB_DA_ACCESS_READ_WRITE:
return (FILE_GENERIC_READ | FILE_GENERIC_WRITE);
case SMB_DA_ACCESS_EXECUTE:
return (FILE_GENERIC_EXECUTE);
}
/* invalid open mode */
return ((uint32_t)SMB_INVALID_AMASK);
}
/*
* smb_denymode_to_sharemode
*
* This function converts deny modes used by Open and Open AndX
* commands to share access bits used by NT Create AndX command.
*/
{
switch (desired_access & SMB_DA_SHARE_MASK) {
if (smb_is_executable(fname))
return (FILE_SHARE_READ | FILE_SHARE_WRITE);
else {
if ((desired_access &
return (FILE_SHARE_READ);
else
return (FILE_SHARE_NONE);
}
case SMB_DA_SHARE_EXCLUSIVE:
return (FILE_SHARE_NONE);
case SMB_DA_SHARE_DENY_WRITE:
return (FILE_SHARE_READ);
case SMB_DA_SHARE_DENY_READ:
return (FILE_SHARE_WRITE);
case SMB_DA_SHARE_DENY_NONE:
return (FILE_SHARE_READ | FILE_SHARE_WRITE);
}
/* invalid deny mode */
return ((uint32_t)SMB_INVALID_SHAREMODE);
}
/*
* smb_ofun_to_crdisposition
*
* This function converts open function values used by Open and Open AndX
* commands to create disposition values used by NT Create AndX command.
*/
{
{
{ -1, FILE_CREATE },
{ FILE_OPEN, FILE_OPEN_IF },
};
if (row == 3)
return ((uint32_t)SMB_INVALID_CRDISPOSITION);
}
/*
* smb_open_share_check
*
* check file sharing rules for current open request
* against the given existing open.
*
* Returns NT_STATUS_SHARING_VIOLATION if there is any
* sharing conflict, otherwise returns NT_STATUS_SUCCESS.
*/
{
/*
* As far as I can tell share modes are not relevant to
* directories. The check for exclusive access (Deny RW)
* remains because I don't know whether or not it was here
* for a reason.
*/
return (NT_STATUS_SHARING_VIOLATION);
}
return (NT_STATUS_SUCCESS);
}
/* if it's just meta data */
return (NT_STATUS_SUCCESS);
/*
* Check requested share access against the
* open granted (desired) access
*/
return (NT_STATUS_SHARING_VIOLATION);
if (DENY_READ(share_access) &&
return (NT_STATUS_SHARING_VIOLATION);
if (DENY_WRITE(share_access) &&
return (NT_STATUS_SHARING_VIOLATION);
/* check requested desired access against the open share access */
return (NT_STATUS_SHARING_VIOLATION);
return (NT_STATUS_SHARING_VIOLATION);
return (NT_STATUS_SHARING_VIOLATION);
return (NT_STATUS_SUCCESS);
}
/*
* smb_file_share_check
*
* check file sharing rules for current open request
* against all existing opens for a file.
*
* Returns NT_STATUS_SHARING_VIOLATION if there is any
* sharing conflict, otherwise returns NT_STATUS_SUCCESS.
*/
{
return (NT_STATUS_SUCCESS);
/* if it's just meta data */
return (NT_STATUS_SUCCESS);
while (open) {
if (status == NT_STATUS_SHARING_VIOLATION) {
return (status);
}
}
return (NT_STATUS_SUCCESS);
}
/*
* smb_amask_to_amode
* mode flags.
*/
int
smb_amask_to_amode(unsigned long amask)
{
if ((amask & FILE_READ_DATA) &&
return (O_RDWR);
return (O_WRONLY);
return (O_RDONLY);
}
/*
* smb_open_subr
*
* Notes on write-through behaviour. It looks like pre-LM0.12 versions
* of the protocol specify the write-through mode when a file is opened,
* (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose,
* SmbWriteAndUnlock) don't need to contain a write-through flag.
*
* With LM0.12, the open calls (SmbCreateAndX, SmbNtTransactCreate)
* don't indicate which write-through mode to use. Instead the write
* calls (SmbWriteAndX, SmbWriteRaw) specify the mode on a per call
* basis.
*
* We don't care which open call was used to get us here, we just need
* to ensure that the write-through mode flag is copied from the open
* parameters to the node. We test the omode write-through flag in all
* write functions.
*
* This function will return NT status codes but it also raises errors,
* in which case it won't return to the caller. Be careful how you
* handle things in here.
*/
{
int created = 0;
int rc;
int pathlen;
int max_requested = 0;
unsigned int granted_oplock;
int is_dir;
int is_stream;
int lookup_flags = SMB_FOLLOW_LINKS;
if (is_dir) {
/*
* The file being created or opened is a directory file.
* With this flag, the Disposition parameter must be set to
* one of FILE_CREATE, FILE_OPEN, or FILE_OPEN_IF
*/
/* invalid open mode */
/* NOTREACHED */
}
}
max_requested = 1;
}
/* NOTREACHED */
}
/* This must be NULL at this point */
case STYPE_DISKTREE:
break;
case STYPE_IPC:
/*
* No further processing for IPC, we need to either
* raise an exception or return success here.
*/
/* NOTREACHED */
} else {
return (NT_STATUS_SUCCESS);
}
break;
default:
/* NOTREACHED */
break;
}
/* NOTREACHED */
}
/*
* Some clients pass null file names; NT interprets this as "\".
*/
if (pathlen == 0) {
pathlen = 1;
}
/* NOTREACHED */
}
/* NOTREACHED */
}
/*
* If the access mask has only DELETE set (ignore
* FILE_READ_ATTRIBUTES), then assume that this
* is a request to delete the link (if a link)
* and do not follow links. Otherwise, follow
* the link to the target.
*/
if (rc == 0) {
rc = 0;
} else {
/* NOTREACHED */
}
/*
* Reject this request if the target is a directory
* and the client has specified that it must not be
* a directory (required by Lotus Notes).
*/
/* NOTREACHED */
}
/*
* Directories cannot be opened
* with the above commands
*/
/* NOTREACHED */
}
/* NOTREACHED */
}
/*
* No more open should be accepted when "Delete on close"
* flag is set.
*/
/* NOTREACHED */
}
/*
* Specified file already exists so the operation should fail.
*/
/* NOTREACHED */
}
/*
* Windows seems to check read-only access before file
* sharing check.
*/
if (NODE_IS_READONLY(node)) {
/* Files data only */
FILE_APPEND_DATA)) {
/* NOTREACHED */
}
}
}
if (status == NT_STATUS_SHARING_VIOLATION) {
return (status);
}
op->desired_access);
if (status != NT_STATUS_SUCCESS) {
if (status == NT_STATUS_PRIVILEGE_NOT_HELD) {
} else {
}
}
/*
* Break the oplock before share checks. If another client
* has the file open, this will force a flush or close,
* which may affect the outcome of any share checking.
*/
if (OPLOCKS_IN_FORCE(node)) {
if (status != NT_STATUS_SUCCESS) {
/* NOTREACHED */
}
}
switch (op->create_disposition) {
case FILE_SUPERSEDE:
case FILE_OVERWRITE_IF:
case FILE_OVERWRITE:
/* NOTREACHED */
}
/* NOTREACHED */
}
}
/*
* If file is being replaced,
* we should remove existing streams
*/
if (SMB_IS_STREAM(node) == 0)
node);
break;
default:
/*
* FILE_OPEN or FILE_OPEN_IF.
*/
break;
}
} else {
/* Last component was not found. */
/*
* The requested file not found so the operation should
* fail with these two dispositions
*/
if (is_stream)
else
/* NOTREACHED */
}
/*
* lock the parent dir node in case another create
* request to the same parent directory comes in.
*/
if (is_dir == 0) {
if (rc != 0) {
/* NOTREACHED */
}
if (rc != 0) {
/* NOTREACHED */
}
}
} else {
if (rc != 0) {
/* NOTREACHED */
}
}
created = 1;
}
if (node == 0) {
}
/* not allowed to do this */
if (created)
/* NOTREACHED */
}
if (max_requested) {
}
/*
* smb_ofile_open() will copy node to of->node. Hence
* the hold on node (i.e. op->fqi.last_snode) will be "transferred"
* to the "of" structure.
*/
&err);
if (created)
/* NOTREACHED */
}
/*
* Propagate the write-through mode from the open params
* to the node: see the notes in the function header.
*
* IR #102318 Mirroring may force synchronous
* writes regardless of what we specify here.
*/
/* We don't oplock directories */
} else {
if (status != NT_STATUS_SUCCESS) {
(void) smb_ofile_close(of, 0);
if (created)
/* NOTREACHED */
}
}
if (created) {
/*
* Clients may set the DOS readonly bit on create but they
* expect subsequent write operations on the open fid to
* succeed. Thus the DOS readonly bit is not set until the
* file is closed. The NODE_CREATED_READONLY flag will
* inhibit other attempts to open the file with write access
* and act as the indicator to set the DOS readonly bit on
* close.
*/
}
} else {
/*
* If we reach here, it means that file already exists
* and if create disposition is one of: FILE_SUPERSEDE,
* FILE_OVERWRITE_IF, or FILE_OVERWRITE it
* means that client wants to overwrite (or truncate)
* the existing file. So we should overwrite the dos
* attributes of destination file with the dos attributes
* of source file.
*/
switch (op->create_disposition) {
case FILE_SUPERSEDE:
case FILE_OVERWRITE_IF:
case FILE_OVERWRITE:
}
}
/*
* Set up the file type in open_param for the response
*/
if (created) {
}
return (NT_STATUS_SUCCESS);
}
/*
* smb_validate_object_name
*
* Very basic file name validation. Directory validation is handed off
* to smb_validate_dirname. For filenames, we check for names of the
* form "AAAn:". Names that contain three characters, a single digit
* and a colon (:) are reserved as DOS device names, i.e. "COM1:".
*
* Returns NT status codes.
*/
{
char *filename;
if (path == 0)
return (0);
if (ftype)
return (smb_validate_dirname(path));
/*
* Basename with backslashes.
*/
++filename;
else
return (NT_STATUS_OBJECT_NAME_INVALID);
}
return (0);
}
/*
* smb_preset_delete_on_close
*
* Set the DeleteOnClose flag on the smb file. When the file is closed,
* the flag will be transferred to the smb node, which will commit the
* delete operation and inhibit subsequent open requests.
*
* When DeleteOnClose is set on an smb_node, the common open code will
* reject subsequent open requests for the file. Observation of Windows
* 2000 indicates that subsequent opens should be allowed (assuming
* there would be no sharing violation) until the file is closed using
* the fid on which the DeleteOnClose was requested.
*/
void
{
}