/*
* 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
* or http://www.opensolaris.org/os/licensing.
* 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 (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
*/
#include <errno.h>
#include <unistd.h>
#include <strings.h>
#include <syslog.h>
#include <sys/fs_reparse.h>
#include <smbsrv/libsmb.h>
#include "dfs.h"
#include "smb_reparse.h"
static int smb_reparse_init(const char *, nvlist_t **);
static void smb_reparse_free(nvlist_t *);
static int smb_reparse_set(const char *, nvlist_t *);
static int smb_reparse_deref(char *, char *, dfs_info_t *);
/*
* Checks the status of the object specified by 'path'
*
* Returns 0 and fills 'stat' with the proper status on
* success, otherwise returns an error code.
*/
int
smb_reparse_stat(const char *path, uint32_t *stat)
{
struct stat statbuf;
char symbuf[MAXREPARSELEN];
int rptaglen;
if (lstat(path, &statbuf) != 0) {
if (errno == ENOENT) {
*stat = SMB_REPARSE_NOTFOUND;
return (0);
}
return (errno);
}
if ((statbuf.st_mode & S_IFMT) != S_IFLNK) {
*stat = SMB_REPARSE_NOTREPARSE;
return (0);
}
bzero(symbuf, MAXREPARSELEN);
if (readlink(path, symbuf, MAXREPARSELEN) == -1)
return (errno);
rptaglen = strlen(FS_REPARSE_TAG_STR);
if (strncmp(symbuf, FS_REPARSE_TAG_STR, rptaglen) != 0)
*stat = SMB_REPARSE_NOTREPARSE;
else
*stat = SMB_REPARSE_ISREPARSE;
return (0);
}
/*
* If the reparse point specified by the path already exists
* it is updated by given service type and its data. Update means
* that if such service type does not already exist, it is added
* otherwise it is overwritten by given data.
*
* If the reparse point does not exist, one is created with given
* service type and its data.
*/
int
smb_reparse_svcadd(const char *path, const char *svctype, dfs_info_t *info)
{
nvlist_t *nvl;
int rc;
if ((rc = smb_reparse_init(path, &nvl)) != 0)
return (rc);
if ((rc = reparse_add(nvl, svctype, (const char *)info)) != 0) {
smb_reparse_free(nvl);
return (rc);
}
rc = smb_reparse_set(path, nvl);
smb_reparse_free(nvl);
return (rc);
}
/*
* Removes the entry for the given service type from the
* specified reparse point. If there is no service entry
* left, the reparse point object will be deleted.
*/
int
smb_reparse_svcdel(const char *path, const char *svctype)
{
nvlist_t *nvl;
int rc;
if ((rc = smb_reparse_init(path, &nvl)) != 0)
return (rc);
if ((rc = reparse_remove(nvl, svctype)) != 0) {
smb_reparse_free(nvl);
return (rc);
}
if (nvlist_next_nvpair(nvl, NULL) == NULL) {
/* list is empty remove the object */
rc = reparse_delete(path);
if ((rc != 0) && (rc == ENOENT))
rc = 0;
} else {
rc = smb_reparse_set(path, nvl);
}
smb_reparse_free(nvl);
return (rc);
}
/*
* Obtains referral data of the given service type from the specified
* reparse point. The service data read from the reparse point is passed
* to the reparsed plugin library to convert the data to a dfs_info_t.
*
* If 'info' is NULL, successful return means that the reparse
* point contains a record for the given service type.
*
* If info is non-NULL, a successful return will require the caller to
* release allocated memory by calling dfs_info_free().
*/
int
smb_reparse_svcget(const char *path, const char *svctype, dfs_info_t *info)
{
nvlist_t *nvl;
nvpair_t *nvp;
char *stype, *sdata;
int rc;
if ((rc = smb_reparse_init(path, &nvl)) != 0)
return (rc);
rc = ENODATA;
nvp = nvlist_next_nvpair(nvl, NULL);
while (nvp != NULL) {
stype = nvpair_name(nvp);
if ((stype != NULL) && (strcasecmp(stype, svctype) == 0)) {
if ((rc = nvpair_value_string(nvp, &sdata)) != 0)
break;
if (info != NULL)
rc = smb_reparse_deref(stype, sdata, info);
break;
}
nvp = nvlist_next_nvpair(nvl, nvp);
}
smb_reparse_free(nvl);
return (rc);
}
int
smb_reparse_svctype(const char *path, int *svctype)
{
nvlist_t *nvl;
nvpair_t *nvp;
char *stype;
int rc;
if ((rc = smb_reparse_init(path, &nvl)) != 0)
return (rc);
rc = ENODATA;
nvp = NULL;
while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
if ((stype = nvpair_name(nvp)) == NULL)
continue;
if (strcasecmp(stype, DFS_REPARSE_SVCTYPE) == 0) {
*svctype = SMB_SVCTYPE_DFS;
rc = 0;
break;
}
if (strcasecmp(stype, DFS_FEDFS_SVCTYPE) == 0) {
*svctype = SMB_SVCTYPE_FEDFS;
rc = 0;
break;
}
}
smb_reparse_free(nvl);
return (rc);
}
/*
* Initializes the given nvpair list.
*
* This function assumes that the object specified by this path
* is a reparse point, so it does not do any verification.
*
* If specified reparse point does not exist the function
* returns successfully with an empty nvpair list.
*
* If the object exists and readlink is successful then nvpair
* list is polulated with the reparse service information, otherwise
* an error code is returned.
*/
static int
smb_reparse_init(const char *path, nvlist_t **nvl)
{
char rp_data[MAXREPARSELEN];
int rc;
if ((*nvl = reparse_init()) == NULL)
return (ENOMEM);
bzero(rp_data, MAXREPARSELEN);
if ((rc = readlink(path, rp_data, MAXREPARSELEN)) == -1) {
if (errno == ENOENT)
return (0);
reparse_free(*nvl);
return (errno);
}
if ((rc = reparse_parse(rp_data, *nvl)) != 0) {
reparse_free(*nvl);
return (rc);
}
return (0);
}
/*
* Frees given nvlist
*/
static void
smb_reparse_free(nvlist_t *nvl)
{
reparse_free(nvl);
}
/*
* Create a reparse point with given services in the passed
* nvlist. If the reparse point already exists, it will be
* deleted and a new one with the given data is created.
*/
static int
smb_reparse_set(const char *path, nvlist_t *nvl)
{
char *rp_data;
int rc;
if ((rc = reparse_unparse(nvl, &rp_data)) != 0)
return (rc);
rc = reparse_delete(path);
if ((rc != 0) && (rc != ENOENT)) {
free(rp_data);
return (rc);
}
rc = reparse_create(path, rp_data);
free(rp_data);
return (rc);
}
/*
* This routine takes reparse point data (sdata) of the specified
* type (stype) and calls the reparse library (reparse_deref()) to
* convert it to a dfs referral of type dfs_info_t.
*
* reparse_deref() will call the appropriate reparse plugin library
* to do the conversion. The data returned from reparse_deref()
* is an xdr encoded dfs_info_t.
* reparse_deref() returns EOVERFLOW if the supplied buffer is not
* large enough to contain the xdr encoded referral and sets
* bufsize to the required buffer length.
*/
static int
smb_reparse_deref(char *stype, char *sdata, dfs_info_t *info)
{
XDR xdr;
dfs_referral_response_t rsp;
char *buf;
size_t bufsize;
int rc;
bufsize = DFS_REF_RSP_BUF_SZ;
if ((buf = malloc(bufsize)) == NULL)
return (ENOMEM);
rc = reparse_deref(stype, sdata, buf, &bufsize);
if (rc == EOVERFLOW) {
free(buf);
if ((buf = malloc(bufsize)) == NULL)
return (ENOMEM);
rc = reparse_deref(stype, sdata, buf, &bufsize);
}
if (rc != 0) {
free(buf);
return (rc);
}
xdrmem_create(&xdr, buf, bufsize, XDR_DECODE);
bzero(&rsp, sizeof (dfs_referral_response_t));
if (dfs_referral_response_xdr(&xdr, &rsp)) {
rc = dfs_info_copy(&rsp.rp_referrals, info);
xdr_free(dfs_referral_response_xdr,
(char *)&rsp);
} else {
rc = EINVAL;
}
XDR_DESTROY(&xdr);
free(buf);
return (rc);
}