dfs.c revision 148c5f43199ca0b43fc8e3b643aab11cd66ea327
/*
* 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
*/
/*
*/
#include <strings.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <dlfcn.h>
#include <pthread.h>
#include <syslog.h>
#include <sys/fs_reparse.h>
#include <smbsrv/libmlsvc.h>
#include <smbsrv/smb_share.h>
#include <dfs.h>
/*
* default timeout (TTL) values (in second) for root and link
*/
#define DFS_ROOT_TIMEOUT 300
#define DFS_LINK_TIMEOUT 1800
/*
* DFS link data format in reparse point
*
* ver:state:prop:timeout:guid:ntarget:cmntlen:comment
* [[:tserver:tshare:tstate:pclass:prank]...]
*/
#define DFS_LINK_V1 1
#define DFS_ROOT_XATTR "SUNWdfs.rootinfo"
#define DFS_INFO_ALL 0
static void *dfs_intr_hdl = NULL;
static struct {
int (*dfsops_remote_count)(uint32_t *);
} dfs_intr_ops;
/*
* Namespace cache
*
* Caches links' UNC and filesystem path where the key is the UNC path.
*/
static smb_cache_t dfs_nscache;
static char dfs_nbname[NETBIOS_NAME_SZ];
/*
* Lock for accessing root information (extended attribute)
*/
static rwlock_t dfs_root_rwl;
/*
* Namespace functions
*/
static void *dfs_namespace_cache(void *);
/*
* Root functions
*/
static int dfs_root_add(const char *, dfs_info_t *);
static uint32_t dfs_root_remove(const char *);
static int dfs_root_xopen(const char *, int);
static void dfs_root_xclose(int);
/*
* Link functions
*/
/*
* Target functions
*/
static void dfs_target_init(dfs_target_t *, const char *, const char *,
uint32_t);
const char *);
/*
* Cache functions
*/
static void dfs_cache_populate(const char *, const char *);
static int dfs_cache_cmp(const void *, const void *);
/*
* Utility functions
*/
static boolean_t dfs_path_isdir(const char *);
/*
* DFS module initializationr:
*
* - creates the namespace cache
* - gets system's NetBIOS name
*/
void
dfs_init(void)
{
sizeof (dfs_nscnode_t));
return;
return;
if ((dfs_intr_ops.dfsops_remote_count =
dfs_intr_hdl = NULL;
}
}
/*
* DFS module cleanup:
*
* - destroys the namespace cache
*/
void
dfs_fini(void)
{
}
/*
* file system operations need to be performed. These operations
* should take place on behalf of the connected user (typically
* Administrator) and to do so we need to have an infrastructure
* in place so that smbd can act as a client and sends request to
* the kernel. Right now, we lack this infrastructure, so we make
* a compromise by temporarily enabling some privileges for smbd
*/
void
{
}
/*
* ========================
* Namespace API (public)
* ========================
*/
/*
* Launches a thread to cache the specified namespace
*/
void
dfs_namespace_load(const char *name)
{
char *rootshr;
int rc;
name);
return;
}
(void) pthread_attr_init(&tattr);
(void) pthread_attr_destroy(&tattr);
if (rc != 0)
}
/*
* Flushes the cache when a DFS root share is removed
*/
void /*ARGSUSED*/
dfs_namespace_unload(const char *name)
{
}
/*
* Returns the file system path for the given share if it
* is a DFS root share.
* If 'path' is NULL, this function only indicates whether
* or not the given share represents a DFS namespace
*/
{
return (ERROR_NOT_FOUND);
return (ERROR_NOT_FOUND);
return (ERROR_SUCCESS);
}
/*
* Returns the number of DFS root shares i.e. the number
* of standalone namespaces.
*/
dfs_namespace_count(void)
{
int rc;
/*
* If this call fails, let's assume there's at least one root
* namespace already configured. The interposer library cannot
* confirm or deny the presence of a namespace, so let's take
* the safe approach and assume one exists.
*/
nroot = 1;
"assuming one namespace exists", rc);
}
nroot++;
}
return (nroot);
}
/*
* Creates a DFS root with the given name and comment.
*
* This function does not create the root share, it
* should already exist.
*/
{
dfs_target_t t;
if (*rootshr == '\\') {
/* Windows has a special case here! */
return (ERROR_BAD_PATHNAME);
}
return (NERR_NetNameNotFound);
/* Share is already a DFS root */
return (ERROR_FILE_EXISTS);
}
if (cmnt)
info.i_propflags = 0;
return (status);
if (status == ERROR_SUCCESS)
return (status);
}
/*
* Removes the namespace and all the links in it.
*/
dfs_namespace_remove(const char *name)
{
return (ERROR_NOT_FOUND);
return (ERROR_NOT_FOUND);
return (status);
if (status != ERROR_SUCCESS)
continue;
if (status != ERROR_SUCCESS)
}
/* TODO: remove empty dirs */
return (ERROR_SUCCESS);
}
/*
* ==================
* Root API (public)
* ==================
*/
/*
* Retrieves the information of the root specified by its path.
*
* Info level (1) only needs the UNC path which is not stored,
* it is constructed so the function will return without
* accessing the backend storage.
*/
{
int xfd;
if (infolvl == 1)
return (ERROR_SUCCESS);
(void) rw_rdlock(&dfs_root_rwl);
}
(void) rw_unlock(&dfs_root_rwl);
return (status);
}
/*
* Sets the provided information for the specified root or root target.
* Root is specified by 'rootdir' and the target is specified by
* (t_server, t_share) pair. Only information items needed for given
* information level (infolvl) is valid in the passed DFS info structure
* 'info'.
*/
{
int xfd;
(void) rw_wrlock(&dfs_root_rwl);
(void) rw_unlock(&dfs_root_rwl);
return (ERROR_INTERNAL_ERROR);
}
if (status != ERROR_SUCCESS) {
(void) rw_unlock(&dfs_root_rwl);
return (status);
}
if (status == ERROR_SUCCESS)
(void) rw_unlock(&dfs_root_rwl);
return (status);
}
/*
* ==================
* Link API (public)
* ==================
*/
/*
* Gets the status of the given path as a link
*/
{
return (ERROR_INTERNAL_ERROR);
switch (*stat) {
case SMB_REPARSE_NOTFOUND:
break;
case SMB_REPARSE_NOTREPARSE:
*stat = DFS_STAT_NOTLINK;
break;
case SMB_REPARSE_ISREPARSE:
*stat = DFS_STAT_ISDFS;
break;
default:
*stat = DFS_STAT_UNKNOWN;
break;
}
return (ERROR_SUCCESS);
}
/*
* Creates a new DFS link or adds a new target to an existing link
*/
{
dfs_target_t *t;
int ntargets;
return (status);
switch (stat) {
case DFS_STAT_NOTFOUND:
case DFS_STAT_ISREPARSE:
/* Create a new DFS link */
if (status != ERROR_SUCCESS)
return (status);
break;
case DFS_STAT_ISDFS:
/* Add a target to an existing link */
if (flags & DFS_ADD_VOLUME)
return (ERROR_FILE_EXISTS);
if (status != ERROR_SUCCESS)
return (status);
break;
case DFS_STAT_NOTLINK:
/* specified path points to a non-reparse object */
return (ERROR_FILE_EXISTS);
default:
return (ERROR_INTERNAL_ERROR);
}
/* checks to see if the target already exists */
return (ERROR_FILE_EXISTS);
}
/* add the new target */
if (t == NULL) {
return (ERROR_NOT_ENOUGH_MEMORY);
}
info.i_ntargets++;
return (status);
}
/*
* Removes a link or a link target from a DFS namespace. A link can be
* removed regardless of the number of targets associated with it.
*
* 'server' and 'share' parameters specify a target, so if they are NULL
* it means the link should be removed, otherwise the specified target
* is removed if found.
*/
{
return (status);
if (stat != DFS_STAT_ISDFS)
return (ERROR_NOT_FOUND);
/* remove the link */
return (ERROR_INTERNAL_ERROR);
return (ERROR_SUCCESS);
}
/* remove the specified target in the link */
if (status != ERROR_SUCCESS)
return (status);
/* checks to see if the target exists */
if (idx != -1) {
info.i_ntargets--;
} else {
return (ERROR_FILE_NOT_FOUND);
}
if (info.i_ntargets == 0) {
/* if last target, then remove the link */
} else {
}
return (status);
}
/*
* Sets the provided information for the specified link or link target.
* Link is specified by 'path' and the target is specified by
* (t_server, t_share) pair. Only information items needed for given
* information level (infolvl) is valid in the passed DFS info structure
* 'info'.
*/
{
if (status != ERROR_SUCCESS)
return (status);
if (status == ERROR_SUCCESS)
return (status);
}
/*
* Gets the DFS link info.
*
* If path is NULL, it just does some initialization.
*
* Info level (1) only needs the UNC path which is not
* stored, it is constructed so the function will return
* without accessing the backend storage.
*/
{
char *link_data;
int rc;
info->i_propflags = 0;
return (ERROR_SUCCESS);
}
if (infolvl == 1)
return (ERROR_SUCCESS);
if (rc != 0)
return (ERROR_INTERNAL_ERROR);
return (status);
}
/*
* ===================
* Cache API (public)
* ===================
*/
/*
* Adds an entry with given DFS name (root sharename) and relative path
* to the namespace cache.
*/
{
char uncpath[DFS_PATH_MAX];
char fspath[DFS_PATH_MAX];
return (ERROR_NOT_FOUND);
if (type == DFS_OBJECT_ROOT) {
dfs_nbname, name);
}
/* add link entry */
/* relpath may contain '/' */
}
/*
* Removes the namespace cache entry for the given link
* in the namespace ('name') with 'relpath'
*/
void
{
/* relpath may contain '/' */
}
/*
* Get the DFS data for the specified cache entry
*/
{
else
if (status == ERROR_SUCCESS)
return (status);
}
/*
* Returns the number of cache entries i.e. the number of
* root(s) and link(s)
*/
dfs_cache_num(void)
{
return (smb_cache_num(&dfs_nscache));
}
void
{
}
{
}
/*
* ==================
* Misc API (public)
* ==================
*/
/*
* This is the function that is called by smbd door server to
* fullfil a GetReferrals request from smbsrv kernel module
*
* 'reftype' specifies the requested referral type. If it is
* DFS_REFERRAL_ROOT then dfs_path should point to a namespace
* root. If it is DFS_REFERRAL_LINK then dfs_path should CONTAIN
* a link, in which case this function will find the link and
* returns its target information.
*/
{
char linkpath[DFS_PATH_MAX];
if (status != ERROR_SUCCESS)
return (status);
switch (reftype) {
case DFS_REFERRAL_ROOT:
break;
}
break;
case DFS_REFERRAL_LINK:
break;
}
linkpath, DFS_PATH_MAX)) {
break;
}
break;
default:
break;
}
return (status);
}
/*
* Takes a DFS path in UNC format (dfs_path) and parse it into a dfs_path_t
* structure.
*
* dfs_path_free() MUST be called to free the allocated memory in this
* function.
*
* Returns:
*
* ERROR_INVALID_PARAMETER path is not a valid UNC or not valid for the
* specified object type
* ERROR_NOT_ENOUGH_MEMORY not enough memory to peform the parse
* ERROR_NOT_FOUND namespace specified does not exist
*/
{
char rootdir[DFS_PATH_MAX];
int rc;
switch (rc) {
case EINVAL:
return (ERROR_INVALID_PARAMETER);
case ENOMEM:
return (ERROR_NOT_ENOUGH_MEMORY);
default:
break;
}
!= ERROR_SUCCESS) {
return (ERROR_NOT_FOUND);
}
if (path_type == DFS_OBJECT_ANY)
else
case DFS_OBJECT_LINK:
else
break;
case DFS_OBJECT_ROOT:
else
break;
default:
}
if (status != ERROR_SUCCESS)
return (status);
}
/*
* Frees the allocated memory for p_unc field of the passed path
*/
void
{
}
/*
* Free the allocated memory for targets in the given info
* structure
*/
void
{
if (info)
}
/*
* Trace the given DFS info structure
*/
void
{
dfs_target_t *t;
int i;
return;
return;
t->t_priority.p_rank);
}
}
/*
* Search the path specified by 'relpath' to see if it contains
* a DFS link starting from the last component. If a link is found
* the full path is returned in 'linkpath'
*/
static boolean_t
{
char rootdir[DFS_PATH_MAX];
char *p;
return (B_FALSE);
for (;;) {
return (B_FALSE);
if (stat == DFS_STAT_ISDFS)
return (B_TRUE);
return (B_FALSE);
*p = '\0';
}
/*NOTREACHED*/
return (B_FALSE);
}
/*
* Caches the specified namespace
*/
static void *
dfs_namespace_cache(void *arg)
{
char uncpath[DFS_PATH_MAX];
return (NULL);
}
return (NULL);
}
static int
{
int xfd;
(void) rw_wrlock(&dfs_root_rwl);
}
(void) rw_unlock(&dfs_root_rwl);
return (status);
}
/*
* Deletes the specified root information
*/
static uint32_t
dfs_root_remove(const char *rootdir)
{
int attrdirfd;
int err = 0;
(void) rw_wrlock(&dfs_root_rwl);
}
} else {
}
(void) rw_unlock(&dfs_root_rwl);
if (err != 0) {
return (ERROR_INTERNAL_ERROR);
}
return (ERROR_SUCCESS);
}
/*
* Opens DFS root directory's extended attribute with the given mode.
*/
static int
{
int dfd;
int xfd = -1;
int err = 0;
if (xfd == -1)
} else {
}
if (err != 0) {
}
return (xfd);
}
/*
* Closes given extended attribute file descriptor
*/
static void
dfs_root_xclose(int xfd)
{
}
/*
* Writes the given DFS data in the DFS root directory's
* extended attribute specified with xfd file descriptor.
*/
static uint32_t
{
return (status);
}
/*
* Reads DFS root information from its directory extended attribute
* and parse it into given dfs_info_t structure
*/
static uint32_t
{
char *buf;
return (ERROR_INTERNAL_ERROR);
return (ERROR_NOT_ENOUGH_MEMORY);
else
return (status);
}
/*
* Encodes (packs) DFS information in 'info' into a flat
* buffer in a name-value format. This function allocates a
* buffer with appropriate size to contain all the information
* so the caller MUST free the allocated memory by calling free().
*/
static uint32_t
{
dfs_target_t *t;
int rc;
return (ERROR_NOT_ENOUGH_MEMORY);
if (rc == 0)
}
/*
* Decodes (unpack) provided buffer which contains a list of name-value
* pairs into given dfs_info_t structure
*/
static uint32_t
{
int rc;
return (ERROR_INTERNAL_ERROR);
if (rc != 0) {
return (ERROR_INTERNAL_ERROR);
}
switch (infolvl) {
case DFS_INFO_ALL:
case 3:
case 4:
case 6:
case 9:
/* need target information */
break;
default:
return (ERROR_SUCCESS);
}
return (ERROR_NOT_ENOUGH_MEMORY);
}
if (rc != 0) {
return (ERROR_INTERNAL_ERROR);
}
return (ERROR_SUCCESS);
}
/*
* Determines if the passed state is valid for a DFS root
*
* This is based on test results against Win2003 and in some cases
* does not match [MS-DFSNM] spec.
*/
static uint32_t
{
switch (state) {
case DFS_VOLUME_STATE_OK:
return (ERROR_SUCCESS);
return (ERROR_INVALID_PARAMETER);
case DFS_VOLUME_STATE_OFFLINE:
case DFS_VOLUME_STATE_ONLINE:
case DFS_VOLUME_STATE_STANDBY:
return (ERROR_NOT_SUPPORTED);
default:
break;
}
return (ERROR_INVALID_PARAMETER);
}
/*
* Decodes the link info from given string buffer (buf) into
* dfs_info_t structure.
*/
static uint32_t
{
char *lfield[DFS_LINK_HDR_NFIELDS];
dfs_target_t *t;
int i, j;
/*
* Header format
* ver:state:prop:timeout:guid:ntarget:cmntlen:comment:
*/
for (i = 0; i < DFS_LINK_HDR_NFIELDS; i++) {
return (ERROR_INVALID_DATA);
}
i = 0;
if (linkver != DFS_LINK_V1)
return (ERROR_INVALID_DATA);
else if (cmntlen != 0)
switch (infolvl) {
case DFS_INFO_ALL:
case 3:
case 4:
case 6:
case 9:
/* need target information */
break;
default:
return (ERROR_SUCCESS);
}
return (ERROR_NOT_ENOUGH_MEMORY);
/*
* Format for each target
* server:share:state:class:rank
*/
for (j = 0; j < DFS_LINK_TRGT_NFIELDS; j++) {
return (ERROR_INVALID_DATA);
}
}
}
return (ERROR_SUCCESS);
}
/*
* Encodes given link information (info)
*/
static uint32_t
{
char linkdata[MAXREPARSELEN];
dfs_target_t *t;
int i, sz;
/*
* Header format
* ver:state:prop:timeout:guid:ntarget:cmntlen:comment
*/
return (ERROR_INTERNAL_ERROR);
}
/*
* Format for each target
* :server:share:state:class:rank
*/
return (ERROR_INVALID_NAME);
return (ERROR_INTERNAL_ERROR);
}
}
return (ERROR_SUCCESS);
}
/*
* Stores given information for the specified link
*/
static uint32_t
{
char linkdata[MAXREPARSELEN];
int rc;
if (status == ERROR_SUCCESS) {
if (rc != 0)
}
return (status);
}
/*
* Determines if the passed state is valid for a link
*/
static boolean_t
{
return (state == DFS_VOLUME_STATE_OK ||
}
/*
* Initializes the given target structure (t) with provided information.
*/
static void
{
t->t_priority.p_rank = 0;
}
/*
* Lookup the specified target (server, share) in the given
* target list (targets). If there is a match its index is
* returned, otherwise -1 will be returned.
*/
static int
{
dfs_target_t *t;
int i;
return (i);
}
return (-1);
}
/*
*/
static boolean_t
{
return (state == DFS_STORAGE_STATE_ONLINE ||
}
/*
* Cache compare function, the key is UNC path
*/
static int
{
int rc;
if (rc < 0)
return (-1);
if (rc > 0)
return (1);
return (0);
}
/*
* Adds an entry with given UNC and filesystem path and the specified
*/
static uint32_t
{
return (ERROR_NOT_ENOUGH_MEMORY);
}
return (status);
}
/*
* starting from DFS root directory, scans the tree for DFS links
* and adds them to the cache.
*/
static void
{
char fspath[DFS_PATH_MAX];
char uncpath[DFS_PATH_MAX];
char *fname;
int nentries, i;
struct dirent **entry_list;
if (nentries == -1)
return;
for (i = 0; i < nentries; i++) {
free(entry_list[i]);
continue;
}
fname);
if (dfs_path_isdir(fspath)) {
if (stat == DFS_STAT_ISDFS)
}
free(entry_list[i]);
}
for (; i < nentries; i++)
free(entry_list[i]);
}
/*
* Determines whether the given path is a directory.
*/
static boolean_t
dfs_path_isdir(const char *path)
{
return (B_FALSE);
}
/*
* and whether it is the object's state or its target's state
*/
static uint32_t
{
if (type == DFS_OBJECT_ROOT) {
if (!target)
return (dfs_root_isvalidstate(state));
if (!dfs_target_isvalidstate(state))
else if (state == DFS_STORAGE_STATE_OFFLINE)
} else {
if (!target) {
if (!dfs_link_isvalidstate(state))
} else {
if (!dfs_target_isvalidstate(state))
}
}
return (status);
}
/*
* Based on the specified information level (infolvl) copy parts of the
* information provided through newinfo into the existing information
* (info) for the given object.
*/
static uint32_t
{
int target_idx;
if (target_idx == -1)
return (ERROR_FILE_NOT_FOUND);
}
switch (infolvl) {
case 100:
break;
case 101:
if (status != ERROR_SUCCESS)
return (status);
if (!target_op) {
/*
* states specified by this mask should not be stored
*/
if (state & DFS_VOLUME_STATES_SRV_OPS)
return (ERROR_SUCCESS);
} else {
}
break;
case 102:
break;
case 103:
break;
case 104:
break;
case 105:
break;
default:
}
return (status);
}