/*
* Copyright (c) 2000-2001 Boris Popov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Boris Popov.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: smbfs_smb.c,v 1.73.38.1 2005/05/27 02:35:28 lindak Exp $
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <netsmb/smb_osdep.h>
#include <netsmb/smb_conn.h>
#include <netsmb/smb_subr.h>
#include <smbfs/smbfs_node.h>
#include <smbfs/smbfs_subr.h>
/*
* Jan 1 1980 as 64 bit NT time.
* (tenths of microseconds since 1601)
*/
/*
* Local functions.
* Not static, to aid debugging.
*/
/*
* Todo: locking over-the-wire
*/
#ifdef APPLE
static int
{
int error;
/* Shared lock for n_fid use below. */
/* After reconnect, n_fid is invalid */
return (ESTALE);
if (op == SMB_LOCK_SHARED)
/* XXX: if (SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_FILES)? */
if (largelock)
if (error)
return (error);
mb_put_uint16le(mbp, 0);
if (!largelock) {
} else {
}
/*
* Don't want to risk missing a successful
* unlock send or lock response, or we could
* lose track of an outstanding lock.
*/
if (op == SMB_LOCK_RELEASE)
else
return (error);
}
int
{
/*
* TODO: use LOCK_BYTE_RANGE here.
*/
return (EINVAL);
/*
* XXX: compute largelock via:
* (SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_FILES)?
*/
}
#endif /* APPLE */
/*
* Helper for smbfs_getattr
* Something like nfs_getattr_otw
*/
int
{
int error;
/*
* This lock is necessary for FID-based calls.
* Lock may be writer (via open) or reader.
*/
/*
* Extended attribute directory or file.
*/
return (error);
}
return (error);
/* fallback */
return (error);
}
/*
* Common function for QueryFileInfo, QueryPathInfo.
*/
int
{
/*
* Shared lock for n_fid use below.
* See smbfs_smb_getfattr()
*/
/*
* If we have a valid open FID, use it.
*/
else
top:
if (error)
return (error);
if (!infolevel) {
else
}
if (cmd == SMB_TRANS2_QUERY_FILE_INFORMATION)
if (cmd == SMB_TRANS2_QUERY_PATH_INFORMATION) {
mb_put_uint32le(mbp, 0);
/* mb_put_uint8(mbp, SMB_DT_ASCII); specs are wrong */
if (error) {
return (error);
}
}
if (error) {
/* Invalid info level? Try fallback. */
goto top;
}
return (error);
}
switch (infolevel) {
case SMB_QFILEINFO_STANDARD:
timesok = 1;
break;
case SMB_QFILEINFO_ALL_INFO:
timesok = 0;
/* creation time */
if (llongint)
timesok++;
/* last access time */
if (llongint)
timesok++;
/* last write time */
if (llongint)
timesok++;
/* last change time */
if (llongint)
timesok++;
/* attributes */
/*
* 4-Byte alignment - discard
* Specs don't talk about this.
*/
/* allocation size */
/* File size */
break;
default:
}
/*
* if all times are zero (observed with FAT on NT4SP6)
* then fall back to older info level
*/
if (!timesok) {
if (infolevel == SMB_QFILEINFO_ALL_INFO) {
goto top;
}
}
return (error);
}
/*
* Support functions for _qstreaminfo
* Moved to smbfs_xattr.c
*/
int
{
int error;
if (error)
return (error);
if (error)
goto out;
if (error)
goto out;
/*
* Get the FS type name.
*/
} else {
}
/*
* If fs_name starts with FAT, we can't set dates before 1980
*/
}
out:
return (0);
}
int
{
int error;
else
return (error);
}
int
{
uint64_t s, t, f;
int error;
if (error)
return (error);
if (error)
goto out;
if (error)
goto out;
s = bsize;
s *= bpu;
t = units;
f = funits;
/*
* Don't allow over-large blocksizes as they determine
* Finder List-view size granularities. On the other
* hand, we mustn't let the block count overflow the
* 31 bits available.
*/
while (s > 16 * 1024) {
if (t > LONG_MAX)
break;
s /= 2;
t *= 2;
f *= 2;
}
while (t > LONG_MAX) {
t /= 2;
f /= 2;
s *= 2;
}
out:
return (0);
}
int
{
uint64_t s, t, f;
int error;
scrp);
if (error)
return (error);
if (error)
goto out;
if (error)
goto out;
s = bsize;
s *= bpu;
t = units;
f = funits;
/*
* Don't allow over-large blocksizes as they determine
* Finder List-view size granularities. On the other
* hand, we mustn't let the block count overflow the
* 31 bits available.
*/
while (s > 16 * 1024) {
if (t > LONG_MAX)
break;
s /= 2;
t *= 2;
f *= 2;
}
while (t > LONG_MAX) {
t /= 2;
f /= 2;
s *= 2;
}
out:
return (0);
}
int
{
int error;
if (error)
return (error);
else
t2p->t2_maxdcount = 0;
return (error);
}
/*ARGSUSED*/
int
{
/* Shared lock for n_fid use below. */
/* After reconnect, n_fid is invalid */
return (ESTALE);
return (ENOTSUP);
if (error)
return (error);
if (tdnp) {
&fid);
if (error)
goto exit;
}
if (error)
goto exit;
t2p->t2_maxdcount = 0;
exit:
if (fid) {
if (cerror)
SMBVDEBUG("error %d closing %s\n",
}
return (error);
}
int
{
int error;
/* Shared lock for n_fid use below. */
return (0);
return (0); /* not open */
/* After reconnect, n_fid is invalid */
return (ESTALE);
if (error)
return (error);
if (!error) {
}
return (error);
}
int
{
int error;
/*
* This call knows about 64-bit offsets.
*/
if (!error) {
return (0);
}
}
/*
* OK, so fallback to SMB_COM_WRITE, but note:
* it only supports 32-bit file offsets.
*/
if (newsize > UINT32_MAX)
return (EFBIG);
if (error)
return (error);
mb_put_uint16le(mbp, 0);
mb_put_uint16le(mbp, 0);
mb_put_uint16le(mbp, 0);
return (error);
}
/*
* Old method for getting file attributes.
*/
int
{
int error;
if (error)
return (error);
if (error)
goto out;
if (error)
goto out;
if (error)
goto out;
if (wc != 10) {
goto out;
}
/*
* Be careful using the time returned here, as
* with FAT on NT4SP6, at least, the time returned is low
* 32 bits of 100s of nanoseconds (since 1601) so it rolls
* over about every seven minutes!
*/
out:
return (error);
}
/*
* Set DOS file attributes. mtime should be NULL for dialects above lm10
*/
int
{
long time;
if (error)
return (error);
if (mtime) {
} else
time = 0;
if (error)
goto out;
}
mb_put_uint8(mbp, 0);
out:
return (error);
}
int
{
int error;
attr |= SMB_FA_HIDDEN;
}
return (error);
}
int
{
int error;
attr &= ~SMB_FA_HIDDEN;
}
return (error);
}
/*
* Set file attributes (optionally: DOS attr, atime, mtime)
* either by open FID or by path name (FID == -1).
*/
int
int fid,
{
int error;
/*
* Normally can use the trans2 call.
*/
return (error);
}
/*
* Fall-back for older protocols.
*/
return (error);
}
return (error);
}
/*
* Set file atime and mtime. Isn't supported by core dialect.
*/
int
{
if (error)
return (error);
if (atime)
else
if (mtime)
else
return (error);
}
/*
* Set DOS file attributes, either via open FID or by path name.
* Looks like this call can be used only if CAP_NT_SMBS bit is on.
*
* When setting via path (fid == -1):
* *BASIC_INFO works with Samba, but Win2K servers say it is an
* invalid information level on a SET_PATH_INFO. Note Win2K does
* support *BASIC_INFO on a SET_FILE_INFO, and they support the
* equivalent *BASIC_INFORMATION on SET_PATH_INFO. Go figure.
*/
int
int fid, /* if fid == -1, set by path */
{
int error;
if (fid == -1) {
} else {
if (fid > UINT16_MAX)
return (EINVAL);
}
else
if (error)
return (error);
if (cmd == SMB_TRANS2_SET_FILE_INFORMATION)
if (cmd == SMB_TRANS2_SET_PATH_INFORMATION) {
if (error != 0)
goto out;
}
/* FAT file systems don't support dates earlier than 1980. */
if (atime) {
} else
tm = 0;
if (mtime) {
} else
tm = 0;
t2p->t2_maxdcount = 0;
out:
return (error);
}
/*
*/
int
const char *name,
int nmlen,
int xattr, /* is named stream? */
{
int err;
nmlen = 0;
if (err)
goto out;
0, /* NTCREATEX_FLAGS... */
out:
return (err);
}
static uint32_t
{
if ((mode == SMB_AM_OPENREAD) ||
(mode == SMB_AM_OPENRW)) {
rights |=
}
if ((mode == SMB_AM_OPENWRITE) ||
(mode == SMB_AM_OPENRW)) {
rights |=
}
if (mode == SMB_AM_OPENEXEC) {
rights |=
}
return (rights);
}
static int
{
return (accmode);
}
static int
const char *name,
int nmlen,
int xattr,
int accmode,
{
int error;
/*
* XXX: move to callers...
*
* Use DENYNONE to give unixy semantics of permitting
* everything not forbidden by permissions. Ie denial
* advisory locks for further control.
*/
if (error)
return (error);
if (error)
goto done;
/*
* Don't want to risk missing a successful
* open response, or we could "leak" FIDs.
*/
if (error)
goto done;
/*
* 8/2002 a DAVE server returned wc of 15 so we ignore that.
* (the actual packet length and data was correct)
*/
if (error)
goto done;
goto done;
}
/*
* Be careful using the time returned here, as
* with FAT on NT4SP6, at least, the time returned is low
* 32 bits of 100s of nanoseconds (since 1601) so it rolls
* over about every seven minutes!
*/
done:
if (error)
return (error);
if (fap)
return (0);
}
int
{
/* Shared lock for n_fid use below. */
/* Can we re-use n_fid? or must we open anew? */
return (0);
}
/* re-open an existing file. */
NULL, 0, 0, /* name nmlen xattr */
0, /* create options */
return (error);
}
NULL, 0, 0, /* name nmlen xattr */
fidp,
NULL, /* granted mode p */
NULL); /* fa p */
return (error);
}
int
{
int error = 0;
/* Shared lock for n_fid use below. */
/*
* Don't expect to find the last reference
* here in tmpclose. Hard to deal with as
* we don't have r_lkserlock exclusive.
* Will close oldfid below.
*/
}
} else {
/* Will close the passed fid. */
}
if (oldfid != SMB_FID_UNUSED)
return (error);
}
int
const char *name,
int nmlen,
int xattr,
{
/* open an existing file */
0, /* create options */
if (error != 0)
return (error);
return (0);
}
if (error != 0)
return (error);
return (0);
}
int
{
int error;
/*
* ENOTCONN isn't interesting - if the connection is closed,
* so are all our FIDs - and EIO is also not interesting,
* as it means a forced unmount was done. (was ENXIO)
* Also ETIME, which means we sent the request but gave up
* waiting before the response came back.
*
* Don't clog up the system log with warnings about these
* uninteresting failures on closes.
*/
switch (error) {
case ENOTCONN:
case ENXIO:
case EIO:
case ETIME:
error = 0;
}
return (error);
}
static int
{
long tm;
int error;
if (error)
return (error);
attr |= SMB_FA_HIDDEN;
gethrestime(&ctime);
if (error)
goto out;
/*
* Don't want to risk missing a successful
* open response, or we could "leak" FIDs.
*/
if (error)
goto out;
if (wc != 1) {
goto out;
}
out:
return (error);
}
int
const char *name,
int nmlen,
int xattr,
{
int error;
/*
* At present the only access we might need is to WRITE data,
* access needed gets more complex it should made a parameter
* and be set upstream.
*/
disp, /* != NTCREATEX_DISP_OPEN */
return (error);
}
return (error);
}
int
{
int error;
if (error)
return (error);
if (!error) {
}
return (error);
}
int
{
int error;
char sep;
if (error)
return (error);
/* freebsd bug: Let directories be renamed - Win98 requires DIR bit */
/*
* When we're not adding any component name, the
* passed sep is ignored, so just pass sep=0.
*/
if (error)
goto out;
/*
* After XATTR directories, separator is ":"
*/
if (error)
goto out;
out:
return (error);
}
int
{
int error;
if (error)
return (error);
if (error)
goto out;
if (error)
goto out;
out:
return (error);
}
static int
{
int error;
if (error)
return (error);
if (!error) {
}
return (error);
}
int
{
int error;
/*
* We ask for SA_RIGHT_FILE_READ_DATA not because we need it, but
* just to be asking for something. The rights==0 case could
* easily be broken on some old or unusual servers.
*/
if (error)
return (error);
return (0);
}
return (error);
}
int
{
int error;
if (error)
return (error);
if (!error) {
}
return (error);
}
static int
{
}
if (error)
return (error);
if (error)
return (error);
} else {
if (SMB_UNICODE_STRINGS(vcp)) {
mb_put_uint8(mbp, 0);
}
mb_put_uint8(mbp, 0);
}
error = 0;
iseof = 1;
} else if (error)
return (error);
if (error)
return (error);
if (wc != 1)
if (error)
return (error);
if (ec == 0)
return (ENOENT);
if (bc < 3)
return (EBADRPC);
bc -= 3;
if (bt != SMB_DT_VARIABLE)
return (EBADRPC);
return (EBADRPC);
return (0);
}
/*ARGSUSED*/
static int
{
if (wildcard) {
} else {
}
} else {
}
return (0);
}
static int
{
char *cp;
int error;
return (ENOENT);
gethrestime(&ts);
if (error)
return (error);
}
*cp-- = 0;
return (0);
}
static int
{
return (0);
}
/*
* TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
*/
static int
{
int error;
}
}
if (error)
return (error);
mb_put_uint32le(mbp, 0);
if (error)
return (error);
} else {
if (error)
return (error);
/* Send whatever resume key we received... */
/* ... and the resume name if we have one. */
/* resume file name */
}
/* Add trailing null - 1 byte if ASCII, 2 if Unicode */
mb_put_uint8(mbp, 0);
}
if (error)
return (error);
/*
* This is the "resume name" we just sent.
* We want the new one (if any) that may be
* found in the response we just received and
* will now begin parsing. Free the old one
* now so we'll know if we found a new one.
*/
ctx->f_rnamelen = 0;
}
goto nodata;
}
if (error != 0)
goto nodata;
/*
* The "end of search" flag from an XP server sometimes
* comes back zero when the prior find_next returned exactly
* the number of entries requested. in which case we'd try again
* but the search has in fact been closed so an EBADF results.
* our circumvention is to check here for a zero entry count.
*/
return (ENOENT);
/* Last Name Off (LNO) is the entry with the resume name. */
return (0);
/*
* Failed parsing the FindFirst or FindNext response.
* Force this directory listing closed, otherwise the
* calling process may hang in an infinite loop.
*/
return (EIO);
}
static int
{
int error;
if (error)
return (error);
/* Ditto comments at _smb_close */
return (error);
}
/*ARGSUSED*/
static int
{
return (0);
}
static int
{
char *cp;
return (ENOENT);
gethrestime(&ts);
if (error)
return (error);
}
switch (ctx->f_infolevel) {
case SMB_FIND_STANDARD:
next = 0;
fxsz = 0;
if (error)
goto nodata;
fxsz = 23;
break;
case SMB_FIND_DIRECTORY_INFO:
if (error)
goto nodata;
/*
* Skip EaSize(4 bytes), a byte of ShortNameLength,
* a reserved byte, and ShortName(8.3 means 24 bytes,
* as Leach defined it to always be Unicode)
*/
if (error)
goto nodata;
fxsz += 30;
}
break;
default:
return (EINVAL);
}
else
/* Allocated f_name in findopen */
if (error)
goto nodata;
if (next) {
/* How much data to skip? */
if (cnt < 0) {
SMBVDEBUG("out of sync\n");
goto nodata;
}
if (cnt > 0)
}
/* Don't count any trailing null in the name. */
nmlen -= 2;
} else {
nmlen--;
}
if (nmlen == 0)
goto nodata;
/*
* On a find-next we expect that the server will:
* 1) if the continue bit is set, use the server's offset,
* 2) else if the resume key is non-zero, use that offset,
* 3) else if the resume name is set, use that offset,
* 4) else use the server's idea of current offset.
*
* We always set the resume key flag. If the server returns
* a resume key then we should always send it back to them.
*/
if (ctx->f_rnameofs &&
/*
* This entry is the "resume name".
* Save it for the next request.
*/
}
}
return (0);
/*
* Something bad has happened and we ran out of data
* before we could parse all f_ecnt entries expected.
* Force this directory listing closed, otherwise the
* calling process may hang in an infinite loop.
*/
SMBVDEBUG("ran out of data\n");
return (EIO);
}
static int
{
int error = 0;
/*
* If SMBFS_RDD_FINDFIRST is still set, we were opened
* but never saw a findfirst, so we don't have any
* search handle to close.
*/
return (error);
}
int
struct smbfs_fctx **ctxpp)
{
int error;
goto out;
}
} else {
}
out:
if (error)
else
return (error);
}
int
{
int error;
/*
* Note: "limit" (maxcount) needs to fit in a short!
*/
if (limit > 0xffff)
limit = 0xffff;
for (;;) {
case ft_LM1:
break;
case ft_LM2:
break;
case ft_XA:
break;
default:
ASSERT(0);
break;
}
if (error)
return (error);
/*
* Skip "." or ".." - easy now that ctx->f_name
* has already been converted to utf-8 format.
*/
continue;
break;
}
/*
* Moved the smbfs_fname_tolocal(ctx) call into
* the ..._findnext functions above.
*/
return (0);
}
int
{
int error;
case ft_LM1:
break;
case ft_LM2:
break;
case ft_XA:
break;
}
return (error);
}
int
{
/* This is no longer called with a null dnp */
/*
* Should not get here with "" anymore.
*/
DEBUG_ENTER("smbfs_smb_lookup: name is NULL");
return (EINVAL);
}
/*
* Should not get here with "." or ".." anymore.
*/
DEBUG_ENTER("smbfs_smb_lookup: name is '.' or '..'");
return (EINVAL);
}
/*
* XXX: Should use _qpathinfo here instead.
* (if SMB_CAP_NT_SMBS)
*/
/*
* Shared lock for n_fid use (smb_flush).
*/
return (EINTR);
/*
* This hides a server bug observable in Win98:
* size changes may not show until a CLOSE or a FLUSH op
* XXX: Make this conditional on !NTSMBs
*/
if (error)
goto out;
if (error)
goto out;
if (error == 0) {
/*
* Solaris smbfattr doesn't have fa_ino,
* and we don't allow name==NULL in this
* function anymore.
*/
if (namep)
*namep = (const char *)smbfs_name_alloc(
if (nmlenp)
}
out:
return (error);
}
/*
* OTW function to Get a security descriptor (SD).
*
* Note: On success, this fills in mdp->md_top,
* which the caller should free.
*/
int
{
if (error)
return (error);
/* Parameters part */
/* Data part (none) */
/* Max. returned parameters and data. */
goto done;
/*
* if there's more data than we said we could receive, here
* is where we pick up the length of it
*/
if (error)
goto done;
/*
* get the data part.
*/
goto done;
}
/*
* The returned parameter SD_length should match
* the length of the returned data. Unfortunately,
* we have to work around server bugs here.
*/
SMBVDEBUG("len %d *reslen %d fid 0x%x\n",
}
/*
* Actual data provided is < returned SD_length.
*
* The following "if (len < *reslen)" handles a Windows bug
* observed when the underlying filesystem is FAT32. In that
* case a 32 byte security descriptor comes back (S-1-1-0, ie
* "Everyone") but the Parameter Block claims 44 is the length
* of the security descriptor. (The Data Block length
* claimed is 32. This server bug was reported against NT
* first and I've personally observed it with W2K.
*/
/*
* Actual data provided is > returned SD_length.
* (Seen on StorageTek NAS 5320, s/w ver. 4.21 M0)
* Narrow work-around for returned SD_length==0.
*/
/*
* Increase *reslen, but carefully.
*/
}
done:
}
return (error);
}
#ifdef APPLE
/*
* Wrapper for _getsd() compatible with darwin code.
*/
int
{
int error;
struct mbuf *m;
/*
* Server may give us an error indicating that we
* need a larger data buffer to receive the SD,
* and the size we'll need. Use the given size,
* but only after a sanity check.
*
* XXX: Check for specific error values here?
* XXX: also ... && len <= MAX_RAW_SD_SIZE
*/
goto again;
if (error)
return (error);
return (error);
}
#endif /* APPLE */
/*
* OTW function to Set a security descriptor (SD).
* Caller data are carried in an mbchain_t.
*
* Note: This normally consumes mbp->mb_top, and clears
* that pointer when it does.
*/
{
int error;
if (error)
return (error);
/* Parameters part */
/* Data part */
/* No returned parameters or data. */
ntp->nt_maxpcount = 0;
ntp->nt_maxdcount = 0;
return (error);
}
#ifdef APPLE
/*
* This function builds the SD given the various parts.
*/
int
{
/*
* Build the SD as its own mbuf chain and pass it to
* smbfs_smb_setsec_m()
*/
/*
* A note about flags ("SECURITY_DESCRIPTOR_CONTROL" in MSDN)
* We set here only those bits we can be sure must be set. The rest
* are up to the caller. In particular, the caller may intentionally
* set an acl PRESENT bit while giving us a null pointer for the
* acl - that sets a null acl, giving access to everyone. Note also
* that the AUTO_INHERITED bits should probably always be set unless
* the server is NT.
*/
if (owner) {
}
if (group) {
}
if (sacl) {
flags |= SD_SACL_PRESENT;
}
if (dacl) {
flags |= SD_DACL_PRESENT;
}
if (owner)
if (group)
if (sacl)
if (dacl)
/*
* Just pass the mbuf to _setsec_m
* It will clear mb_top if consumed.
*/
return (error);
}
#endif /* APPLE */