/*
* Copyright (c) 2000, 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
* documentation and/or other materials provided with the distribution.
* 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.
*/
/*
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <stdlib.h>
#include <sysexits.h>
#include <libintl.h>
#include <syslog.h>
#include <smb/smb.h>
#include "smbfs_lib.h"
#include "smbfs_private.h"
#define MIN_REPLY_SIZE 4096
static uint32_t smbfs_map_doserr(uint8_t, uint16_t);
/*
* Create and initialize a request structure, for either an
* "internal" request (one that does not use the driver) or
* a regular "driver" request, that uses driver ioctls.
*
* The two kinds are built a little differently:
* Driver requests are composed starting with the
* first word of the "variable word vector" section.
* The driver prepends the SMB header and word count.
* The driver also needs an output buffer to receive
* the response, filled in via copyout in the ioctl.
*
* Internal requests are composed entirely in this library.
* Space for the SMB header is reserved here, and later
* filled in by smbfs_rq_internal before the send/receive.
*/
int
smbfs_rq_init(struct smb_ctx *ctx, uchar_t cmd, struct smb_rq **rqpp)
{
struct smb_rq *rqp;
if ((rqp = malloc(sizeof (*rqp))) == NULL)
return (ENOMEM);
bzero(rqp, sizeof (*rqp));
rqp->rq_cmd = cmd;
rqp->rq_ctx = ctx;
/*
* Setup the request buffer.
* Do the reply buffer later.
*/
if (smbfs_mb_init(&rqp->rq_rq)) {
smbfs_rq_done(rqp);
return (ENOMEM);
}
/* Space for the SMB header (filled in later) */
smbfs_mb_put_mem(&rqp->rq_rq, NULL, SMB_HEADER_LEN, MB_MSYSTEM);
/*
* Copy the ctx flags here, so the caller can
* update the req flags before the OTW call.
*/
rqp->rq_hflags = ctx->ct_hflags;
rqp->rq_hflags2 = ctx->ct_hflags2;
*rqpp = rqp;
return (0);
}
void
smbfs_rq_done(struct smb_rq *rqp)
{
smbfs_mb_done(&rqp->rq_rp);
smbfs_mb_done(&rqp->rq_rq);
free(rqp);
}
/*
* Reserve space for the word count, which is filled in later by
* smbfs_rq_wend(). Also initialize the counter that it uses
* to figure out what value to fill in.
*
* Note that the word count happens to be 8-bits,
* which can lead to confusion.
*/
void
smbfs_rq_wstart(struct smb_rq *rqp)
{
struct mbdata *mbp = &rqp->rq_rq;
(void) smbfs_mb_fit(mbp, 1, &rqp->rq_wcntp);
rqp->rq_wcbase = mbp->mb_count;
}
/*
* Fill in the word count, in the space reserved by
* smbfs_rq_wstart().
*/
void
smbfs_rq_wend(struct smb_rq *rqp)
{
struct mbdata *mbp = &rqp->rq_rq;
int wcnt;
if (rqp->rq_wcntp == NULL) {
DPRINT("no wcount ptr\n");
return;
}
wcnt = mbp->mb_count - rqp->rq_wcbase;
if (wcnt > 0x1ff)
DPRINT("word count too large (%d)\n", wcnt);
if (wcnt & 1)
DPRINT("odd word count\n");
wcnt >>= 1;
/*
* Fill in the word count (8-bits).
* Also store it in the rq, in case
* we're using the ioctl path.
*/
*rqp->rq_wcntp = (char)wcnt;
}
/*
* Reserve space for the byte count, which is filled in later by
* smbfs_rq_bend(). Also initialize the counter that it uses
* to figure out what value to fill in.
*
* Note that the byte count happens to be 16-bits,
* which can lead to confusion.
*/
void
smbfs_rq_bstart(struct smb_rq *rqp)
{
struct mbdata *mbp = &rqp->rq_rq;
(void) smbfs_mb_fit(mbp, 2, &rqp->rq_bcntp);
rqp->rq_bcbase = mbp->mb_count;
}
/*
* Fill in the byte count, in the space reserved by
* smbfs_rq_bstart().
*/
void
smbfs_rq_bend(struct smb_rq *rqp)
{
struct mbdata *mbp = &rqp->rq_rq;
int bcnt;
if (rqp->rq_bcntp == NULL) {
DPRINT("no bcount ptr\n");
return;
}
bcnt = mbp->mb_count - rqp->rq_bcbase;
if (bcnt > 0xffff)
DPRINT("byte count too large (%d)\n", bcnt);
/*
* Fill in the byte count (16-bits).
* Also store it in the rq, in case
* we're using the ioctl path.
*
* The pointer is char * type due to
* typical off-by-one alignment.
*/
rqp->rq_bcntp[0] = bcnt & 0xFF;
rqp->rq_bcntp[1] = (bcnt >> 8);
}
int
smbfs_rq_t2_request(int dev_fd, int setupcount, uint16_t *setup,
const char *name,
int tparamcnt, void *tparam,
int tdatacnt, void *tdata,
int *rparamcnt, void *rparam,
int *rdatacnt, void *rdata,
int *buffer_oflow)
{
smbioc_t2rq_t *krq;
int i;
if (setupcount < 0 || setupcount >= SMBIOC_T2RQ_MAXSETUP) {
syslog(LOG_ERR, "smbfs_rq_t2_request: setup=%d: %s",
setupcount, strerror(EINVAL));
return (EINVAL);
}
if ((krq = malloc(sizeof (smbioc_t2rq_t))) == NULL) {
syslog(LOG_ERR, "smbfs_rq_t2_request: %s", strerror(errno));
return (ENOMEM);
}
bzero(krq, sizeof (*krq));
for (i = 0; i < setupcount; i++)
krq->ioc_setup[i] = setup[i];
krq->ioc_setupcnt = setupcount;
(void) strcpy(krq->ioc_name, name);
krq->ioc_tparamcnt = tparamcnt;
krq->ioc_tparam = tparam;
krq->ioc_tdatacnt = tdatacnt;
krq->ioc_tdata = tdata;
krq->ioc_rparamcnt = *rparamcnt;
krq->ioc_rdatacnt = *rdatacnt;
krq->ioc_rparam = rparam;
krq->ioc_rdata = rdata;
if (ioctl(dev_fd, SMBIOC_T2RQ, krq) == -1) {
free(krq);
syslog(LOG_ERR, "smbfs_rq_t2_request: SMBIOC_T2RQ failed: %s",
strerror(errno));
return (errno);
}
*rparamcnt = krq->ioc_rparamcnt;
*rdatacnt = krq->ioc_rdatacnt;
*buffer_oflow = (krq->ioc_rpflags2 & SMB_FLAGS2_NT_STATUS) &&
(krq->ioc_error == NT_STATUS_BUFFER_OVERFLOW);
free(krq);
return (0);
}
/*
* Do an over-the-wire call without using the nsmb driver.
* This is all "internal" to this library, and used only
* for connection setup (negotiate protocol, etc.)
*/
int
smbfs_rq_internal(struct smb_ctx *ctx, struct smb_rq *rqp)
{
static const uint8_t ffsmb[SMB_MAGIC_LEN] = SMB_MAGIC_SIG;
struct smb_iods *is = &ctx->ct_iods;
uint32_t sigbuf[2];
struct mbdata mbtmp, *mbp;
int err, save_mlen;
uint8_t ctmp;
rqp->rq_uid = is->is_smbuid;
rqp->rq_tid = SMB_TID_RESERVED;
rqp->rq_mid = is->is_next_mid++;
/*
* Fill in the NBT and SMB headers
* Using mbtmp so we can rewind without
* affecting the passed request mbdata.
*/
bcopy(&rqp->rq_rq, &mbtmp, sizeof (mbtmp));
mbp = &mbtmp;
mbp->mb_cur = mbp->mb_top;
mbp->mb_pos = mbp->mb_cur->m_data;
mbp->mb_count = 0;
/* Have to save and restore m_len */
save_mlen = mbp->mb_cur->m_len;
mbp->mb_cur->m_len = 0;
/*
* rewind done; fill it in
*/
smbfs_mb_put_mem(mbp, ffsmb, SMB_MAGIC_LEN, MB_MSYSTEM);
smbfs_mb_put_uint8(mbp, rqp->rq_cmd);
smbfs_mb_put_uint32le(mbp, 0); /* status */
smbfs_mb_put_uint8(mbp, rqp->rq_hflags);
smbfs_mb_put_uint16le(mbp, rqp->rq_hflags2);
/* pid_hi(2), signature(8), reserved(2) */
smbfs_mb_put_mem(mbp, NULL, 12, MB_MZERO);
smbfs_mb_put_uint16le(mbp, rqp->rq_tid);
smbfs_mb_put_uint16le(mbp, 0); /* pid_lo */
smbfs_mb_put_uint16le(mbp, rqp->rq_uid);
smbfs_mb_put_uint16le(mbp, rqp->rq_mid);
/* Restore original m_len */
mbp->mb_cur->m_len = save_mlen;
/*
* Sign the message, if flags2 indicates.
*/
if (rqp->rq_hflags2 & SMB_FLAGS2_SMB_SECURITY_SIGNATURE)
smbfs_rq_sign(rqp);
/*
* Send it, wait for the reply.
*/
if ((err = smbfs_nb_ssn_send(ctx, &rqp->rq_rq)) != 0)
return (err);
if ((err = smbfs_nb_ssn_recv(ctx, &rqp->rq_rp)) != 0)
return (err);
/*
* Should have an SMB header, at least.
*/
mbp = &rqp->rq_rp;
if (mbp->mb_cur->m_len < SMB_HEADER_LEN) {
DPRINT("len < 32");
return (EBADRPC);
}
/*
* If the request was signed, validate the
* signature on the response.
*/
if (rqp->rq_hflags2 & SMB_FLAGS2_SMB_SECURITY_SIGNATURE) {
err = smbfs_rq_verify(rqp);
if (err) {
DPRINT("bad signature");
return (err);
}
}
/*
* Decode the SMB header.
*/
smbfs_md_get_mem(mbp, (char *)sigbuf, 4, MB_MSYSTEM);
if (0 != bcmp(sigbuf, ffsmb, 4)) {
DPRINT("not SMB");
return (EBADRPC);
}
smbfs_md_get_uint8(mbp, &ctmp); /* SMB cmd */
smbfs_md_get_uint32le(mbp, &rqp->rq_status);
smbfs_md_get_uint8(mbp, &rqp->rq_hflags);
smbfs_md_get_uint16le(mbp, &rqp->rq_hflags2);
/* pid_hi(2), signature(8), reserved(2) */
smbfs_md_get_mem(mbp, NULL, 12, MB_MSYSTEM);
smbfs_md_get_uint16le(mbp, &rqp->rq_tid);
smbfs_md_get_uint16le(mbp, NULL); /* pid_lo */
smbfs_md_get_uint16le(mbp, &rqp->rq_uid);
smbfs_md_get_uint16le(mbp, &rqp->rq_mid);
/*
* Figure out the status return.
* Caller looks at rq_status.
*/
if ((rqp->rq_hflags2 & SMB_FLAGS2_NT_STATUS) == 0) {
uint16_t serr;
uint8_t class;
class = rqp->rq_status & 0xff;
serr = rqp->rq_status >> 16;
rqp->rq_status = smbfs_map_doserr(class, serr);
}
return (0);
}
/*
* Map old DOS errors (etc.) to NT status codes.
* We probably don't need this anymore, since
* the oldest server we talk to is NT. But if
* later find we do need this, add support here
* for the DOS errors we care about.
*/
static uint32_t
smbfs_map_doserr(uint8_t class, uint16_t serr)
{
if (class == 0 && serr == 0)
return (0);
DPRINT("class 0x%x serr 0x%x", (int)class, (int)serr);
return (NT_STATUS_UNSUCCESSFUL);
}