/*
* 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 2015 Nexenta Systems, Inc. All rights reserved.
*/
/*
* General Structures Layout
* -------------------------
*
* This is a simplified diagram showing the relationship between most of the
* main structures.
*
* +-------------------+
* | SMB_INFO |
* +-------------------+
* |
* |
* v
* +-------------------+ +-------------------+ +-------------------+
* | SESSION |<----->| SESSION |......| SESSION |
* +-------------------+ +-------------------+ +-------------------+
* | |
* | |
* | v
* | +-------------------+ +-------------------+ +-------------------+
* | | USER |<--->| USER |...| USER |
* | +-------------------+ +-------------------+ +-------------------+
* |
* |
* v
* +-------------------+ +-------------------+ +-------------------+
* | TREE |<----->| TREE |......| TREE |
* +-------------------+ +-------------------+ +-------------------+
* | |
* | |
* | v
* | +-------+ +-------+ +-------+
* | | OFILE |<----->| OFILE |......| OFILE |
* | +-------+ +-------+ +-------+
* |
* |
* v
* +-------+ +------+ +------+
* | ODIR |<----->| ODIR |......| ODIR |
* +-------+ +------+ +------+
*
*
* Odir State Machine
* ------------------
*
* +-------------------------+
* | SMB_ODIR_STATE_OPEN |<----------- open / creation
* +-------------------------+
* | ^
* | (first) | (last)
* | lookup | release
* v |
* +-------------------------+
* | SMB_ODIR_STATE_IN_USE |----
* +-------------------------+ | lookup / release / read
* | ^-------
* | close
* |
* v
* +-------------------------+
* | SMB_ODIR_STATE_CLOSING |----
* +-------------------------+ | close / release / read
* | ^-------
* | (last) release
* |
* v
* +-------------------------+
* | SMB_ODIR_STATE_CLOSED |----------> deletion
* +-------------------------+
*
*
* SMB_ODIR_STATE_OPEN
* - the odir exists in the list of odirs of its tree
* - lookup is valid in this state. It will place a hold on the odir
* by incrementing the reference count and the odir will transition
* to SMB_ODIR_STATE_IN_USE
*
* SMB_ODIR_STATE_IN_USE
* - the odir exists in the list of odirs of its tree.
* - lookup is valid in this state. It will place a hold on the odir
* by incrementing the reference count.
* - if the last hold is released the odir will transition
* back to SMB_ODIR_STATE_OPEN
* - if a close is received the odir will transition to
* SMB_ODIR_STATE_CLOSING.
*
* SMB_ODIR_STATE_CLOSING
* - the odir exists in the list of odirs of its tree.
* - lookup will fail in this state.
* - when the last hold is released the odir will transition
* to SMB_ODIR_STATE_CLOSED.
*
* SMB_ODIR_STATE_CLOSED
* - the odir exists in the list of odirs of its tree.
* - there are no users of the odir (refcnt == 0)
* - the odir is being removed from the tree's list and deleted.
* - lookup will fail in this state.
*
* Comments
* --------
* The state machine of the odir structures is controlled by 3 elements:
* - The list of odirs of the tree it belongs to.
* - The mutex embedded in the structure itself.
* - The reference count.
*
* There's a mutex embedded in the odir structure used to protect its fields
* and there's a lock embedded in the list of odirs of a tree. To
* increment or to decrement the reference count the mutex must be entered.
* To insert the odir into the list of odirs of the tree and to remove
* the odir from it, the lock must be entered in RW_WRITER mode.
*
* In order to avoid deadlocks, when both (mutex and lock of the odir
* list) have to be entered, the lock must be entered first.
*
*
* Odir Interface
* ---------------
* smb_odir_open(char *pathname)
* Create an odir representing the directory specified in pathname and
* add it into the tree's list of odirs.
* Returns NT status.
*
* smb_odir_openfh(smb_ofile_t *of)
* Create an odir representing the directory specified by the
* existing open handle (from a prior open of the directory).
* Returns NT status.
*
* smb_odir_openat(smb_node_t *unode)
* Create an odir representing the extended attribute directory
* associated with the file (or directory) represented by unode
* and add it into the tree's list of odirs.
* Returns NT status.
*
* smb_odir_t *odir = smb_tree_lookup_odir(..., odid)
* Find the odir corresponding to the specified odid in the tree's
* list of odirs. Place a hold on the odir.
*
* smb_odir_read(..., smb_odirent_t *odirent)
* Find the next directory entry in the odir and return it in odirent.
*
* smb_odir_read_fileinfo(..., smb_fileinfo_t *)
* Find the next directory entry in the odir. Return the details of
* the directory entry in smb_fileinfo_t. (See odir internals below)
*
* smb_odir_read_streaminfo(..., smb_streaminfo_t *)
* Find the next named stream entry in the odir. Return the details of
* the named stream in smb_streaminfo_t.
*
* smb_odir_close(smb_odir_t *odir)
* Close the odir.
* The caller of close must have a hold on the odir being closed.
* The hold should be released after closing.
*
* smb_odir_release(smb_odir_t *odir)
* Release the hold on the odir, obtained by lookup.
*
*
* Odir Internals
* --------------
* The odir object represent an open directory search. Each read operation
* provides the caller with a structure containing information pertaining
* to the next directory entry that matches the search criteria, namely
* the filename or match pattern and, in the case of smb_odir_read_fileinfo(),
* the search attributes.
*
* The odir maintains a buffer (d_buf) of directory entries read from
* the filesystem via a vop_readdir. The buffer is populated when a read
* request (smb_odir_next_odirent) finds that the buffer is empty or that
* the end of the buffer has been reached, and also when a new client request
* (find next) begins.
*
* The data in d_buf (that which is returned from the file system) can
* be in one of two formats. If the file system supports extended directory
* entries we request that the data be returned as edirent_t structures. If
* it does not the data will be returned as dirent64_t structures. For
* convenience, when the next directory entry is read from d_buf by
* smb_odir_next_odirent it is translated into an smb_odirent_t.
*
* smb_odir_read_fileinfo
* The processing required to obtain the information to populate the caller's
* smb_fileinfo_t differs depending upon whether the directory search is for a
* single specified filename or for multiple files matching a search pattern.
* Thus smb_odir_read_fileinfo uses two static functions:
* smb_odir_single_fileinfo - obtains the smb_fileinfo_t info for the single
* filename as specified in smb_odir_open request.
* smb_odir_wildcard_fileinfo - obtains the smb_fileinfo_t info for the filename
* returned from the smb_odir_next_odirent. This is called in a loop until
* an entry matching the search criteria is found or no more entries exist.
*
* If a directory entry is a VLNK, the name returned in the smb_fileinfo_t
* is the name of the directory entry but the attributes are the attribites
* of the file that is the target of the link. If the link target cannot
* be found the attributes returned are the attributes of the link itself.
*
* smb_odir_read_streaminfo
* In order for an odir to provide information about stream files it
* must be opened with smb_odir_openat(). smb_odir_read_streaminfo() can
* then be used to obtain the name and size of named stream files.
*
* Resuming a Search
* -----------------
* A directory search often consists of multiple client requests: an initial
* find_first request followed by zero or more find_next requests and a
* find_close request.
* The find_first request will open and lookup the odir, read its desired
* number of entries from the odir, then release the odir and return.
* A find_next request will lookup the odir and read its desired number of
* entries from the odir, then release the odir and return.
* At the end of the search the find_close request will close the odir.
*
* In order to be able to resume a directory search (find_next) the odir
* provides the capability for the caller to save one or more resume points
* (cookies) at the end of a request, and to specify which resume point
* (cookie) to restart from at the beginning of the next search.
* smb_odir_save_cookie(..., cookie)
* smb_odir_resume_at(smb_odir_resume_t *resume)
* A search can be resumed at a specified resume point (cookie), the resume
* point (cookie) stored at a specified index in the d_cookies array, or
* a specified filename. The latter (specified filename) is not yet supported.
*
* See smb_search, smb_find, smb_find_unique, and smb_trans2_find for details
*/
#include <smbsrv/smb_kproto.h>
#include <smbsrv/smb_fsops.h>
#include <smbsrv/smb_share.h>
#include <sys/extdirent.h>
/* static functions */
smb_fileinfo_t *);
smb_odirent_t *, smb_fileinfo_t *);
char *, smb_node_t **);
/*
* smb_odir_openpath
*
* Create an odir representing the directory specified in pathname.
*
* Returns:
* NT Status
*/
{
int rc;
if (rc != 0)
return (smb_errno2status(rc));
if (!smb_node_is_dir(dnode)) {
return (NT_STATUS_OBJECT_PATH_NOT_FOUND);
}
return (NT_STATUS_ACCESS_DENIED);
}
return (NT_STATUS_TOO_MANY_OPENED_FILES);
}
else
return (0);
}
/*
* smb_odir_openfh
*
* Create an odir representing the directory already opened on "of".
*
* Returns:
* NT status
*/
smb_odir_t **odp)
{
return (NT_STATUS_INVALID_PARAMETER);
return (NT_STATUS_ACCESS_DENIED);
return (0);
}
/*
* smb_odir_openat
*
* Create an odir representing the extended attribute directory
* associated with the file (or directory) represented by unode.
*
* Returns:
* NT status
*/
{
int rc;
return (NT_STATUS_ACCESS_DENIED);
cr = zone_kcred();
/* find the xattrdir vnode */
if (rc != 0)
return (smb_errno2status(rc));
/* lookup the xattrdir's smb_node */
if (xattr_dnode == NULL)
return (NT_STATUS_NO_MEMORY);
SMB_SEARCH_ATTRIBUTES, 0, cr);
return (0);
}
/*
* smb_odir_hold
*
* A hold will only be granted if the odir is open or in_use.
*/
{
case SMB_ODIR_STATE_OPEN:
break;
case SMB_ODIR_STATE_IN_USE:
break;
case SMB_ODIR_STATE_CLOSING:
case SMB_ODIR_STATE_CLOSED:
default:
return (B_FALSE);
}
return (B_TRUE);
}
/*
* If the odir is in SMB_ODIR_STATE_CLOSING and this release results in
* a refcnt of 0, change the state to SMB_ODIR_STATE_CLOSED and post the
* object for deletion. Object deletion is deferred to avoid modifying
* a list while an iteration may be in progress.
*/
void
{
case SMB_ODIR_STATE_OPEN:
break;
case SMB_ODIR_STATE_IN_USE:
break;
case SMB_ODIR_STATE_CLOSING:
}
break;
case SMB_ODIR_STATE_CLOSED:
default:
break;
}
}
/*
* smb_odir_close
*/
void
{
case SMB_ODIR_STATE_OPEN:
break;
case SMB_ODIR_STATE_IN_USE:
break;
case SMB_ODIR_STATE_CLOSING:
case SMB_ODIR_STATE_CLOSED:
default:
break;
}
}
/*
* smb_odir_read
*
* Find the next directory entry matching the search pattern.
* No search attribute matching is performed.
*
* Returns:
* 0 - success.
* - If a matching entry was found eof will be B_FALSE and
* odirent will be populated.
* ENOENT
* - If we've scanned to the end, eof will be B_TRUE.
* errno - other errors
*/
int
{
int rc;
case SMB_ODIR_STATE_IN_USE:
case SMB_ODIR_STATE_CLOSING:
break;
case SMB_ODIR_STATE_OPEN:
case SMB_ODIR_STATE_CLOSED:
default:
return (EBADF);
}
for (;;) {
break;
break;
}
switch (rc) {
case 0:
return (0);
case ENOENT:
/* FALLTHROUGH */
default:
return (rc);
}
}
/*
* smb_odir_read_fileinfo
*
* Find the next directory entry matching the search pattern
* and attributes: od->d_pattern and od->d_sattr.
*
* If the search pattern specifies a single filename call
* smb_odir_single_fileinfo to get the file attributes and
* populate the caller's smb_fileinfo_t.
*
* If the search pattern contains wildcards call smb_odir_next_odirent
* to get the next directory entry then. Repeat until a matching
* filename is found. Call smb_odir_wildcard_fileinfo to get the
* file attributes and populate the caller's smb_fileinfo_t.
* This is repeated until a file matching the search criteria is found.
*
* Returns:
* 0 - success.
* - If a matching entry was found eof will be B_FALSE and
* fileinfo will be populated.
* ENOENT
* - If at end of dir, eof will be B_TRUE.
* errno - other error
*/
int
{
case SMB_ODIR_STATE_IN_USE:
case SMB_ODIR_STATE_CLOSING:
break;
case SMB_ODIR_STATE_OPEN:
case SMB_ODIR_STATE_CLOSED:
default:
return (EBADF);
}
else
} else {
for (;;) {
break;
/* skip non utf8 filename */
U8_VALIDATE_ENTIRE, &errnum) < 0)
continue;
continue;
fileinfo);
if (rc == 0)
break;
}
}
switch (rc) {
case 0:
*eof = 0;
return (0);
case ENOENT:
/* FALLTHROUGH */
default:
return (rc);
}
}
/*
* smb_odir_read_streaminfo
*
* Find the next directory entry whose name begins with SMB_STREAM_PREFIX,
* and thus represents an NTFS named stream.
* No search attribute matching is performed.
* No case conflict name mangling is required for NTFS named stream names.
*
* Returns:
* 0 - success.
* - If a matching entry was found eof will be B_FALSE and
* sinfo will be populated.
* - If there are no matching entries eof will be B_TRUE.
* errno - error
*/
int
{
int rc;
kcr = zone_kcred();
case SMB_ODIR_STATE_IN_USE:
case SMB_ODIR_STATE_CLOSING:
break;
case SMB_ODIR_STATE_OPEN:
case SMB_ODIR_STATE_CLOSED:
default:
return (EBADF);
}
/* Check that odir represents an xattr directory */
return (0);
}
for (;;) {
break;
continue;
}
if (rc == 0) {
/*
* We just need the file sizes, and don't want
* EACCES failures here, so use kcred and pass
* NULL as the sr to skip sr->fid-ofile checks.
*/
}
if (rc == 0) {
break;
}
}
switch (rc) {
case 0:
return (0);
case ENOENT:
return (0);
default:
return (rc);
}
}
/*
* smb_odir_save_cookie
*
* Callers can save up to SMB_MAX_SEARCH cookies in the odir
* to be used as resume points for a 'find next' request.
*/
void
{
}
/*
* smb_odir_save_fname
*
* Save a filename / offset pair, which are basically a
* one entry cache. See smb_com_trans2_find_next2.
*/
void
{
}
/*
* smb_odir_resume_at
*
* If SMB_ODIR_FLAG_WILDCARDS is not set, and we're rewinding,
* assume we're no longer at EOF.
*
* Wildcard searching can be resumed from:
* - the cookie saved at a specified index (SMBsearch, SMBfind).
* - a specified cookie (SMB_trans2_find)
* - a specified filename (SMB_trans2_find) - NOT SUPPORTED.
* Defaults to continuing from where the last search ended.
*
* Continuation from where the last search ended (SMB_trans2_find)
* is implemented by saving the last cookie at a specific index (0)
* smb_odir_resume_at indicates a new request, so reset od->d_bufptr
* and d_eof to force a vop_readdir.
*/
void
{
return;
}
default:
case SMB_ODIR_RESUME_CONT:
/* Continue where we left off. */
break;
case SMB_ODIR_RESUME_IDX:
/*
* This is used only by the (ancient) SMB_SEARCH.
* Modern clients use trans2 FindFirst, FindNext.
*/
}
break;
case SMB_ODIR_RESUME_COOKIE:
break;
case SMB_ODIR_RESUME_FNAME:
/*
* If the name matches the last one saved,
* use the offset that was saved with it in
* the odir. Otherwise use the cookie value
* in the resume data from the client.
*/
od->d_last_cookie != 0) {
} /* else continue where we left off */
break;
}
/* Force a vop_readdir to refresh d_buf */
}
}
/* *** static functions *** */
/*
* smb_odir_create
* Allocate and populate an odir obect and add it to the tree's list.
*/
static smb_odir_t *
{
/*
* Return this to the caller as if they had done
* smb_tree_lookup_odir() to obtain the odir.
*/
/*
* grab a ref for od->d_user
* released in smb_odir_delete()
*/
if (SMB_TREE_SUPPORTS_CATIA(sr))
if (SMB_TREE_SUPPORTS_ABE(sr))
return (od);
}
/*
* Set a new pattern, attributes, and rewind.
*/
void
{
else
/* Internal smb_odir_resume_at */
}
/*
* Delete an odir.
*
* Remove the odir from the tree list before freeing resources
* associated with the odir.
*/
void
{
}
/*
* smb_odir_next_odirent
*
* Find the next directory entry in d_buf. If d_bufptr is NULL (buffer
* is empty or we've reached the end of it), read the next set of
* entries from the file system (vop_readdir).
*
* File systems which support VFSFT_EDIRENT_FLAGS will return the
* directory entries as a buffer of edirent_t structure. Others will
* return a buffer of dirent64_t structures. For simplicity translate
* the data into an smb_odirent_t structure.
*
* Some file systems can have directories larger than SMB_MAXDIRSIZE.
* If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof
* to true to stop subsequent calls to smb_vop_readdir.
*
* Returns:
* 0 - success. odirent is populated with the next directory entry
* ENOENT - no more directory entries
* errno - error
*/
static int
{
int rc;
int reclen;
int eof;
char *np;
else
if (reclen == 0) {
} else {
}
}
return (ENOENT);
if (rc != 0) {
return (rc);
}
}
else
return (ENOENT);
}
} else {
}
} else {
}
return (0);
}
/*
* smb_odir_single_fileinfo
*
* Lookup the file identified by od->d_pattern.
*
* If the looked up file is a link, we attempt to lookup the link target
* to use its attributes in place of those of the files's.
* If we fail to lookup the target of the link we use the original
* file's attributes.
* Check if the attributes match the search attributes.
*
* Returns: 0 - success
* ENOENT - no match
* errno - error
*/
static int
{
int rc;
char *name;
if (rc != 0)
return (rc);
/*
* If case sensitive, do a case insensitive smb_vop_lookup to
* check for case conflict
*/
if (rc != 0)
return (rc);
if (flags & ED_CASE_CONFLICT)
}
if (rc != 0) {
return (rc);
}
/* follow link to get target node & attr */
if (smb_node_is_symlink(fnode) &&
if (rc != 0) {
return (rc);
}
}
/* check search attributes */
return (ENOENT);
}
}
if (case_conflict)
}
else
return (0);
}
/*
* smb_odir_wildcard_fileinfo
*
* odirent contains a directory entry, obtained from a vop_readdir.
* If a case conflict is identified the filename is mangled and the
* shortname is used as 'name', in place of odirent->od_name.
*
* If the looked up file is a link, we attempt to lookup the link target
* to use its attributes in place of those of the files's.
* If we fail to lookup the target of the link we use the original
* file's attributes.
* Check if the attributes match the search attributes.
*
* Although some file systems can have directories larger than
* SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger
* than SMB_MAXDIRSIZE is returned. It is therefore safe to use the
* offset as the cookie (uint32_t).
*
* Returns: 0 - success
* ENOENT - no match, proceed to next entry
* errno - error
*/
static int
{
int rc;
char *name;
if (rc != 0)
return (rc);
/* follow link to get target node & attr */
if (smb_node_is_symlink(fnode) &&
}
/* skip system files */
if (smb_node_is_system(fnode)) {
return (ENOENT);
}
/*
* Windows directory listings return not only names, but
* also some attributes. In Unix, you need some access to
* get those attributes. Which credential should we use to
* get those? If we're doing Access Based Enumeration (ABE)
* we want this getattr to fail, which will cause the caller
* to skip this entry. If we're NOT doing ABE, we normally
* want to show all the directory entries (including their
* attributes) so we want this getattr to succeed!
*/
else
cr = zone_kcred();
if (rc != 0) {
return (rc);
}
/* check search attributes */
return (ENOENT);
}
}
if (case_conflict)
}
else
return (0);
}
/*
* smb_odir_lookup_link
*
* If the file is a symlink we lookup the object to which the
* symlink refers so that we can return its attributes.
* This can cause a problem if a symlink in a sub-directory
* points to a parent directory (some UNIX GUI's create a symlink
* in $HOME/.desktop that points to the user's home directory).
* trying to follow this recursive path and there is little
* we can do because the path is constructed on the client.
* smb_dirsymlink_enable allows an end-user to disable
* symlinks to directories. Symlinks to other object types
* should be unaffected.
*
* Returns: B_TRUE - followed link. tgt_node and tgt_attr set
* B_FALSE - link not followed
*/
static boolean_t
{
int rc;
if (rc != 0) {
return (B_FALSE);
}
return (B_FALSE);
}
return (B_TRUE);
}
/*
* smb_odir_match_name
*
* Check if the directory entry name matches the search pattern:
* - Don't match reserved dos filenames.
* - Check if odirent->od_name matches od->d_pattern.
* - If shortnames are supported, generate the shortname from
* odirent->od_name and check if it matches od->d_pattern.
*/
static boolean_t
{
if (smb_is_reserved_dos_name(name))
return (B_FALSE);
return (B_TRUE);
return (B_TRUE);
}
return (B_FALSE);
}