2009-10-19 03:20:31 +04:00
|
|
|
/* $NetBSD: smb_smb.c,v 1.32 2009/10/18 23:20:31 tron Exp $ */
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
/*
|
2002-01-04 05:39:37 +03:00
|
|
|
* Copyright (c) 2000-2001 Boris Popov
|
2000-12-07 06:48:09 +03:00
|
|
|
* 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.
|
2002-01-04 05:39:37 +03:00
|
|
|
*
|
2003-03-24 18:07:52 +03:00
|
|
|
* FreeBSD: src/sys/netsmb/smb_smb.c,v 1.10 2003/02/19 05:47:38 imp Exp
|
2000-12-07 06:48:09 +03:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* various SMB requests. Most of the routines merely packs data into mbufs.
|
|
|
|
*/
|
2003-02-25 12:12:11 +03:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2009-10-19 03:20:31 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: smb_smb.c,v 1.32 2009/10/18 23:20:31 tron Exp $");
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
#include <netsmb/iconv.h>
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
#include <netsmb/smb.h>
|
2002-01-04 05:39:37 +03:00
|
|
|
#include <netsmb/smb_subr.h>
|
2000-12-07 06:48:09 +03:00
|
|
|
#include <netsmb/smb_rq.h>
|
|
|
|
#include <netsmb/smb_conn.h>
|
|
|
|
#include <netsmb/smb_tran.h>
|
|
|
|
|
|
|
|
struct smb_dialect {
|
2003-02-24 22:32:16 +03:00
|
|
|
int d_id;
|
|
|
|
const char *d_name;
|
2000-12-07 06:48:09 +03:00
|
|
|
};
|
|
|
|
|
2003-02-24 22:32:16 +03:00
|
|
|
static const struct smb_dialect smb_dialects[] = {
|
2000-12-07 06:48:09 +03:00
|
|
|
{SMB_DIALECT_CORE, "PC NETWORK PROGRAM 1.0"},
|
|
|
|
{SMB_DIALECT_COREPLUS, "MICROSOFT NETWORKS 1.03"},
|
|
|
|
{SMB_DIALECT_LANMAN1_0, "MICROSOFT NETWORKS 3.0"},
|
|
|
|
{SMB_DIALECT_LANMAN1_0, "LANMAN1.0"},
|
|
|
|
{SMB_DIALECT_LANMAN2_0, "LM1.2X002"},
|
|
|
|
{SMB_DIALECT_LANMAN2_0, "Samba"},
|
|
|
|
{SMB_DIALECT_NTLM0_12, "NT LANMAN 1.0"},
|
|
|
|
{SMB_DIALECT_NTLM0_12, "NT LM 0.12"},
|
|
|
|
{-1, NULL}
|
|
|
|
};
|
|
|
|
|
2003-03-24 17:24:14 +03:00
|
|
|
static u_int32_t
|
|
|
|
smb_vc_maxread(struct smb_vc *vcp)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Specs say up to 64k data bytes, but Windows traffic
|
|
|
|
* uses 60k... no doubt for some good reason.
|
|
|
|
*/
|
2003-04-05 17:13:50 +04:00
|
|
|
if (SMB_CAPS(vcp) & SMB_CAP_LARGE_READX)
|
2003-03-24 17:24:14 +03:00
|
|
|
return (60*1024);
|
|
|
|
else
|
|
|
|
return (vcp->vc_sopt.sv_maxtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static u_int32_t
|
|
|
|
smb_vc_maxwrite(struct smb_vc *vcp)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Specs say up to 64k data bytes, but Windows traffic
|
|
|
|
* uses 60k... probably for some good reason.
|
|
|
|
*/
|
2003-04-05 17:13:50 +04:00
|
|
|
if (SMB_CAPS(vcp) & SMB_CAP_LARGE_WRITEX)
|
2003-03-24 17:24:14 +03:00
|
|
|
return (60*1024);
|
|
|
|
else
|
|
|
|
return (vcp->vc_sopt.sv_maxtx);
|
|
|
|
}
|
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
int
|
|
|
|
smb_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred)
|
|
|
|
{
|
2003-02-24 22:32:16 +03:00
|
|
|
const struct smb_dialect *dp;
|
2000-12-07 06:48:09 +03:00
|
|
|
struct smb_sopt *sp = NULL;
|
2002-01-04 05:39:37 +03:00
|
|
|
struct smb_rq *rqp;
|
|
|
|
struct mbchain *mbp;
|
|
|
|
struct mdchain *mdp;
|
2000-12-07 06:48:09 +03:00
|
|
|
u_int8_t wc, stime[8], sblen;
|
2003-03-24 12:17:52 +03:00
|
|
|
u_int16_t dindex, tw, swlen, bc;
|
2000-12-07 06:48:09 +03:00
|
|
|
int error, maxqsz;
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
KASSERT(scred->scr_l == vcp->vc_iod->iod_l);
|
2003-04-01 12:35:42 +04:00
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
vcp->vc_hflags = 0;
|
|
|
|
vcp->vc_hflags2 = 0;
|
2002-01-04 05:39:37 +03:00
|
|
|
vcp->obj.co_flags &= ~(SMBV_ENCRYPT);
|
2000-12-07 06:48:09 +03:00
|
|
|
sp = &vcp->vc_sopt;
|
2009-03-18 19:00:08 +03:00
|
|
|
memset(sp, 0, sizeof(struct smb_sopt));
|
2002-01-04 05:39:37 +03:00
|
|
|
error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_NEGOTIATE, scred, &rqp);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
smb_rq_getrequest(rqp, &mbp);
|
|
|
|
smb_rq_wstart(rqp);
|
|
|
|
smb_rq_wend(rqp);
|
|
|
|
smb_rq_bstart(rqp);
|
|
|
|
for(dp = smb_dialects; dp->d_id != -1; dp++) {
|
2002-01-04 05:39:37 +03:00
|
|
|
mb_put_uint8(mbp, SMB_DT_DIALECT);
|
|
|
|
smb_put_dstring(mbp, vcp, dp->d_name, SMB_CS_NONE);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
smb_rq_bend(rqp);
|
|
|
|
error = smb_rq_simple(rqp);
|
2008-06-24 14:37:19 +04:00
|
|
|
SMBSDEBUG(("%d\n", error));
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
goto bad;
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_rq_getreply(rqp, &mdp);
|
2000-12-07 06:48:09 +03:00
|
|
|
do {
|
2002-01-04 05:39:37 +03:00
|
|
|
error = md_get_uint8(mdp, &wc);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
break;
|
2002-01-04 05:39:37 +03:00
|
|
|
error = md_get_uint16le(mdp, &dindex);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
if (dindex > 7) {
|
2008-06-24 14:37:19 +04:00
|
|
|
SMBERROR(("Don't know how to talk with server %s (%d)\n", "xxx", dindex));
|
2000-12-07 06:48:09 +03:00
|
|
|
error = EBADRPC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dp = smb_dialects + dindex;
|
|
|
|
sp->sv_proto = dp->d_id;
|
2008-06-24 14:37:19 +04:00
|
|
|
SMBSDEBUG(("Dialect %s (%d, %d)\n", dp->d_name, dindex, wc));
|
2000-12-07 06:48:09 +03:00
|
|
|
error = EBADRPC;
|
|
|
|
if (dp->d_id >= SMB_DIALECT_NTLM0_12) {
|
2003-03-24 12:17:52 +03:00
|
|
|
u_int8_t tb;
|
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
if (wc != 17)
|
|
|
|
break;
|
2003-03-24 12:17:52 +03:00
|
|
|
md_get_uint8(mdp, &tb);
|
|
|
|
sp->sv_sm = tb;
|
2002-01-04 05:39:37 +03:00
|
|
|
md_get_uint16le(mdp, &sp->sv_maxmux);
|
|
|
|
md_get_uint16le(mdp, &sp->sv_maxvcs);
|
|
|
|
md_get_uint32le(mdp, &sp->sv_maxtx);
|
|
|
|
md_get_uint32le(mdp, &sp->sv_maxraw);
|
|
|
|
md_get_uint32le(mdp, &sp->sv_skey);
|
|
|
|
md_get_uint32le(mdp, &sp->sv_caps);
|
|
|
|
md_get_mem(mdp, stime, 8, MB_MSYSTEM);
|
2003-03-24 12:17:52 +03:00
|
|
|
md_get_uint16le(mdp, &tw);
|
|
|
|
sp->sv_tz = tw;
|
2002-01-04 05:39:37 +03:00
|
|
|
md_get_uint8(mdp, &sblen);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (sblen && (sp->sv_sm & SMB_SM_ENCRYPT)) {
|
|
|
|
if (sblen != SMB_MAXCHALLENGELEN) {
|
2008-06-24 14:37:19 +04:00
|
|
|
SMBERROR(("Unexpected length of security blob (%d)\n", sblen));
|
2000-12-07 06:48:09 +03:00
|
|
|
break;
|
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
error = md_get_uint16(mdp, &bc);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
if (sp->sv_caps & SMB_CAP_EXT_SECURITY)
|
2002-01-04 05:39:37 +03:00
|
|
|
md_get_mem(mdp, NULL, 16, MB_MSYSTEM);
|
|
|
|
error = md_get_mem(mdp, vcp->vc_ch, sblen, MB_MSYSTEM);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
vcp->vc_chlen = sblen;
|
2002-01-04 05:39:37 +03:00
|
|
|
vcp->obj.co_flags |= SMBV_ENCRYPT;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES;
|
|
|
|
if (dp->d_id == SMB_DIALECT_NTLM0_12 &&
|
|
|
|
sp->sv_maxtx < 4096 &&
|
|
|
|
(sp->sv_caps & SMB_CAP_NT_SMBS) == 0) {
|
2002-01-04 05:39:37 +03:00
|
|
|
vcp->obj.co_flags |= SMBV_WIN95;
|
2008-06-24 14:37:19 +04:00
|
|
|
SMBSDEBUG(("Win95 detected\n"));
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
} else if (dp->d_id > SMB_DIALECT_CORE) {
|
2003-03-24 12:17:52 +03:00
|
|
|
md_get_uint16le(mdp, &sp->sv_sm);
|
2002-01-04 05:39:37 +03:00
|
|
|
md_get_uint16le(mdp, &tw);
|
2000-12-07 06:48:09 +03:00
|
|
|
sp->sv_maxtx = tw;
|
2002-01-04 05:39:37 +03:00
|
|
|
md_get_uint16le(mdp, &sp->sv_maxmux);
|
|
|
|
md_get_uint16le(mdp, &sp->sv_maxvcs);
|
2003-03-24 12:17:52 +03:00
|
|
|
md_get_uint16(mdp, NULL); /* rawmode */
|
2002-01-04 05:39:37 +03:00
|
|
|
md_get_uint32le(mdp, &sp->sv_skey);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (wc == 13) { /* >= LANMAN1 */
|
2003-03-24 12:17:52 +03:00
|
|
|
md_get_uint16(mdp, NULL); /* time */
|
|
|
|
md_get_uint16(mdp, NULL); /* date */
|
|
|
|
md_get_uint16le(mdp, &tw);
|
|
|
|
sp->sv_tz = tw;
|
2002-01-04 05:39:37 +03:00
|
|
|
md_get_uint16le(mdp, &swlen);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (swlen > SMB_MAXCHALLENGELEN)
|
|
|
|
break;
|
2002-01-04 05:39:37 +03:00
|
|
|
md_get_uint16(mdp, NULL); /* mbz */
|
|
|
|
if (md_get_uint16(mdp, &bc) != 0)
|
2000-12-07 06:48:09 +03:00
|
|
|
break;
|
|
|
|
if (bc < swlen)
|
|
|
|
break;
|
|
|
|
if (swlen && (sp->sv_sm & SMB_SM_ENCRYPT)) {
|
2002-01-04 05:39:37 +03:00
|
|
|
error = md_get_mem(mdp, vcp->vc_ch, swlen, MB_MSYSTEM);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
vcp->vc_chlen = swlen;
|
2002-01-04 05:39:37 +03:00
|
|
|
vcp->obj.co_flags |= SMBV_ENCRYPT;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES;
|
|
|
|
} else { /* an old CORE protocol */
|
|
|
|
sp->sv_maxmux = 1;
|
|
|
|
}
|
|
|
|
error = 0;
|
|
|
|
} while (0);
|
|
|
|
if (error == 0) {
|
2002-01-04 05:39:37 +03:00
|
|
|
vcp->vc_maxvcs = sp->sv_maxvcs;
|
|
|
|
if (vcp->vc_maxvcs <= 1) {
|
|
|
|
if (vcp->vc_maxvcs == 0)
|
|
|
|
vcp->vc_maxvcs = 1;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
if (sp->sv_maxtx <= 0 || sp->sv_maxtx > 0xffff)
|
|
|
|
sp->sv_maxtx = 1024;
|
2003-03-24 17:24:14 +03:00
|
|
|
else
|
|
|
|
sp->sv_maxtx = min(sp->sv_maxtx,
|
|
|
|
63*1024 + SMB_HDRLEN + 16);
|
|
|
|
SMB_TRAN_GETPARAM(vcp, SMBTP_RCVSZ, &maxqsz);
|
|
|
|
vcp->vc_rxmax = min(smb_vc_maxread(vcp), maxqsz - 1024);
|
2002-01-04 05:39:37 +03:00
|
|
|
SMB_TRAN_GETPARAM(vcp, SMBTP_SNDSZ, &maxqsz);
|
2003-03-24 17:24:14 +03:00
|
|
|
vcp->vc_wxmax = min(smb_vc_maxwrite(vcp), maxqsz - 1024);
|
2000-12-07 06:48:09 +03:00
|
|
|
vcp->vc_txmax = min(sp->sv_maxtx, maxqsz);
|
2008-06-24 14:37:19 +04:00
|
|
|
SMBSDEBUG(("TZ = %d\n", sp->sv_tz));
|
|
|
|
SMBSDEBUG(("CAPS = %x\n", sp->sv_caps));
|
|
|
|
SMBSDEBUG(("MAXMUX = %d\n", sp->sv_maxmux));
|
|
|
|
SMBSDEBUG(("MAXVCS = %d\n", sp->sv_maxvcs));
|
|
|
|
SMBSDEBUG(("MAXRAW = %d\n", sp->sv_maxraw));
|
|
|
|
SMBSDEBUG(("MAXTX = %d\n", sp->sv_maxtx));
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
bad:
|
|
|
|
smb_rq_done(rqp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
smb_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred)
|
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct smb_rq *rqp;
|
|
|
|
struct mbchain *mbp;
|
2003-02-24 22:32:16 +03:00
|
|
|
const smb_unichar *unipp;
|
|
|
|
smb_uniptr ntencpass = NULL;
|
2005-05-30 01:26:27 +04:00
|
|
|
char *up, *pbuf, *encpass;
|
|
|
|
const char *pp;
|
2003-03-24 18:03:58 +03:00
|
|
|
int error, plen, uniplen, ulen, upper;
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
KASSERT(scred->scr_l == vcp->vc_iod->iod_l);
|
2003-04-01 12:35:42 +04:00
|
|
|
|
2003-03-24 18:03:58 +03:00
|
|
|
upper = 0;
|
|
|
|
|
|
|
|
again:
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
vcp->vc_smbuid = SMB_UID_UNKNOWN;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_SESSION_SETUP_ANDX, scred, &rqp);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
2002-01-04 05:39:37 +03:00
|
|
|
pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK);
|
|
|
|
encpass = malloc(24, M_SMBTEMP, M_WAITOK);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (vcp->vc_sopt.sv_sm & SMB_SM_USER) {
|
2003-03-24 18:03:58 +03:00
|
|
|
/*
|
|
|
|
* We try w/o uppercasing first so Samba mixed case
|
|
|
|
* passwords work. If that fails we come back and try
|
|
|
|
* uppercasing to satisfy OS/2 and Windows for Workgroups.
|
|
|
|
*/
|
|
|
|
if (upper) {
|
|
|
|
iconv_convstr(vcp->vc_toupper, pbuf,
|
2004-04-21 22:16:14 +04:00
|
|
|
smb_vc_getpass(vcp), SMB_MAXPASSWORDLEN + 1);
|
2003-03-24 18:03:58 +03:00
|
|
|
} else {
|
2004-04-21 22:16:14 +04:00
|
|
|
strlcpy(pbuf, smb_vc_getpass(vcp),
|
|
|
|
SMB_MAXPASSWORDLEN + 1);
|
2003-03-24 18:03:58 +03:00
|
|
|
}
|
|
|
|
if (!SMB_UNICODE_STRINGS(vcp))
|
2004-04-21 22:16:14 +04:00
|
|
|
iconv_convstr(vcp->vc_toserver, pbuf, pbuf,
|
|
|
|
SMB_MAXPASSWORDLEN + 1);
|
2003-03-24 18:03:58 +03:00
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) {
|
|
|
|
uniplen = plen = 24;
|
|
|
|
smb_encrypt(pbuf, vcp->vc_ch, encpass);
|
|
|
|
ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK);
|
2003-03-24 18:03:58 +03:00
|
|
|
if (SMB_UNICODE_STRINGS(vcp)) {
|
2004-04-21 22:16:14 +04:00
|
|
|
strlcpy(pbuf, smb_vc_getpass(vcp),
|
|
|
|
SMB_MAXPASSWORDLEN + 1);
|
2003-03-24 18:03:58 +03:00
|
|
|
} else
|
|
|
|
iconv_convstr(vcp->vc_toserver, pbuf,
|
2004-04-21 22:16:14 +04:00
|
|
|
smb_vc_getpass(vcp),
|
|
|
|
SMB_MAXPASSWORDLEN + 1);
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_ntencrypt(pbuf, vcp->vc_ch, (u_char*)ntencpass);
|
|
|
|
pp = encpass;
|
|
|
|
unipp = ntencpass;
|
|
|
|
} else {
|
|
|
|
plen = strlen(pbuf) + 1;
|
|
|
|
pp = pbuf;
|
|
|
|
uniplen = plen * 2;
|
|
|
|
ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK);
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_strtouni(ntencpass, smb_vc_getpass(vcp));
|
2000-12-07 06:48:09 +03:00
|
|
|
plen--;
|
2003-03-24 10:40:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The uniplen is zeroed because Samba cannot deal
|
|
|
|
* with this 2nd cleartext password. This Samba
|
|
|
|
* "bug" is actually a workaround for problems in
|
|
|
|
* Microsoft clients.
|
|
|
|
*/
|
2000-12-07 06:48:09 +03:00
|
|
|
uniplen = 0/*-= 2*/;
|
|
|
|
unipp = ntencpass;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* In the share security mode password will be used
|
|
|
|
* only in the tree authentication
|
|
|
|
*/
|
|
|
|
pp = "";
|
|
|
|
plen = 1;
|
|
|
|
unipp = &smb_unieol;
|
2003-03-24 18:07:52 +03:00
|
|
|
uniplen = 0;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
smb_rq_wstart(rqp);
|
|
|
|
mbp = &rqp->sr_rq;
|
2002-01-04 05:39:37 +03:00
|
|
|
up = vcp->vc_username;
|
2000-12-07 06:48:09 +03:00
|
|
|
ulen = strlen(up) + 1;
|
2003-03-24 18:03:58 +03:00
|
|
|
/*
|
|
|
|
* If userid is null we are attempting anonymous browse login
|
|
|
|
* so passwords must be zero length.
|
|
|
|
*/
|
|
|
|
if (ulen == 1)
|
|
|
|
plen = uniplen = 0;
|
2002-01-04 05:39:37 +03:00
|
|
|
mb_put_uint8(mbp, 0xff);
|
|
|
|
mb_put_uint8(mbp, 0);
|
|
|
|
mb_put_uint16le(mbp, 0);
|
|
|
|
mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxtx);
|
|
|
|
mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxmux);
|
|
|
|
mb_put_uint16le(mbp, vcp->vc_number);
|
|
|
|
mb_put_uint32le(mbp, vcp->vc_sopt.sv_skey);
|
|
|
|
mb_put_uint16le(mbp, plen);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (SMB_DIALECT(vcp) < SMB_DIALECT_NTLM0_12) {
|
2002-01-04 05:39:37 +03:00
|
|
|
mb_put_uint32le(mbp, 0);
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_rq_wend(rqp);
|
|
|
|
smb_rq_bstart(rqp);
|
|
|
|
mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_put_dstring(mbp, vcp, up, SMB_CS_NONE);
|
2000-12-07 06:48:09 +03:00
|
|
|
} else {
|
2002-01-04 05:39:37 +03:00
|
|
|
mb_put_uint16le(mbp, uniplen);
|
|
|
|
mb_put_uint32le(mbp, 0); /* reserved */
|
2003-03-24 10:40:50 +03:00
|
|
|
mb_put_uint32le(mbp, vcp->obj.co_flags & SMBV_UNICODE ?
|
|
|
|
SMB_CAP_UNICODE : 0);
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_rq_wend(rqp);
|
|
|
|
smb_rq_bstart(rqp);
|
|
|
|
mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
|
2005-05-30 01:26:27 +04:00
|
|
|
mb_put_mem(mbp, (const void *)unipp, uniplen, MB_MSYSTEM);
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_put_dstring(mbp, vcp, up, SMB_CS_NONE); /* AccountName */
|
|
|
|
smb_put_dstring(mbp, vcp, vcp->vc_domain, SMB_CS_NONE); /* PrimaryDomain */
|
2003-03-23 13:52:03 +03:00
|
|
|
smb_put_dstring(mbp, vcp, "NetBSD", SMB_CS_NONE); /* Client's OS */
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_put_dstring(mbp, vcp, "NETSMB", SMB_CS_NONE); /* Client name */
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
smb_rq_bend(rqp);
|
|
|
|
if (ntencpass)
|
|
|
|
free(ntencpass, M_SMBTEMP);
|
|
|
|
error = smb_rq_simple(rqp);
|
2008-06-24 14:37:19 +04:00
|
|
|
SMBSDEBUG(("%d\n", error));
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error) {
|
2003-03-23 19:51:52 +03:00
|
|
|
if (error == EACCES)
|
2000-12-07 06:48:09 +03:00
|
|
|
error = EAUTH;
|
|
|
|
goto bad;
|
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
vcp->vc_smbuid = rqp->sr_rpuid;
|
2000-12-07 06:48:09 +03:00
|
|
|
bad:
|
2002-01-04 05:39:37 +03:00
|
|
|
free(encpass, M_SMBTEMP);
|
|
|
|
free(pbuf, M_SMBTEMP);
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_rq_done(rqp);
|
2003-03-24 18:03:58 +03:00
|
|
|
if (error && !upper && vcp->vc_sopt.sv_sm & SMB_SM_USER) {
|
|
|
|
upper = 1;
|
|
|
|
goto again;
|
|
|
|
}
|
2000-12-07 06:48:09 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
smb_smb_ssnclose(struct smb_vc *vcp, struct smb_cred *scred)
|
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct smb_rq *rqp;
|
|
|
|
struct mbchain *mbp;
|
2000-12-07 06:48:09 +03:00
|
|
|
int error;
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
KASSERT(scred->scr_l == vcp->vc_iod->iod_l);
|
2003-04-01 12:35:42 +04:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
if (vcp->vc_smbuid == SMB_UID_UNKNOWN)
|
2000-12-07 06:48:09 +03:00
|
|
|
return 0;
|
2002-01-04 05:39:37 +03:00
|
|
|
|
|
|
|
error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_LOGOFF_ANDX, scred, &rqp);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
mbp = &rqp->sr_rq;
|
|
|
|
smb_rq_wstart(rqp);
|
2002-01-04 05:39:37 +03:00
|
|
|
mb_put_uint8(mbp, 0xff);
|
|
|
|
mb_put_uint8(mbp, 0);
|
|
|
|
mb_put_uint16le(mbp, 0);
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_rq_wend(rqp);
|
|
|
|
smb_rq_bstart(rqp);
|
|
|
|
smb_rq_bend(rqp);
|
|
|
|
error = smb_rq_simple(rqp);
|
2008-06-24 14:37:19 +04:00
|
|
|
SMBSDEBUG(("%d\n", error));
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_rq_done(rqp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2003-03-23 13:38:14 +03:00
|
|
|
static const char *
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_share_typename(int stype)
|
|
|
|
{
|
2003-03-23 13:38:14 +03:00
|
|
|
static const char smb_any_share[] = "?????";
|
|
|
|
const char *pp;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
switch (stype) {
|
2003-03-23 13:38:14 +03:00
|
|
|
case SMB_ST_DISK:
|
2000-12-07 06:48:09 +03:00
|
|
|
pp = "A:";
|
|
|
|
break;
|
2003-03-23 13:38:14 +03:00
|
|
|
case SMB_ST_PRINTER:
|
2000-12-07 06:48:09 +03:00
|
|
|
pp = smb_any_share; /* can't use LPT: here... */
|
|
|
|
break;
|
2003-03-23 13:38:14 +03:00
|
|
|
case SMB_ST_PIPE:
|
2000-12-07 06:48:09 +03:00
|
|
|
pp = "IPC";
|
|
|
|
break;
|
2003-03-23 13:38:14 +03:00
|
|
|
case SMB_ST_COMM:
|
2000-12-07 06:48:09 +03:00
|
|
|
pp = "COMM";
|
|
|
|
break;
|
2003-03-23 13:38:14 +03:00
|
|
|
case SMB_ST_ANY:
|
|
|
|
default:
|
2000-12-07 06:48:09 +03:00
|
|
|
pp = smb_any_share;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
smb_smb_treeconnect(struct smb_share *ssp, struct smb_cred *scred)
|
|
|
|
{
|
|
|
|
struct smb_vc *vcp;
|
|
|
|
struct smb_rq rq, *rqp = &rq;
|
2002-01-04 05:39:37 +03:00
|
|
|
struct mbchain *mbp;
|
2003-03-23 13:38:14 +03:00
|
|
|
const char *pp;
|
|
|
|
char *pbuf, *encpass;
|
2003-03-24 18:03:58 +03:00
|
|
|
int error, plen, caseopt, upper;
|
|
|
|
|
|
|
|
upper = 0;
|
|
|
|
|
|
|
|
again:
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Disable Unicode for SMB_COM_TREE_CONNECT_ANDX requests */
|
|
|
|
if (SSTOVC(ssp)->vc_hflags2 & SMB_FLAGS2_UNICODE) {
|
|
|
|
vcp = SSTOVC(ssp);
|
|
|
|
if (vcp->vc_toserver) {
|
|
|
|
iconv_close(vcp->vc_toserver);
|
|
|
|
/* Use NULL until UTF-8 -> ASCII works */
|
|
|
|
vcp->vc_toserver = NULL;
|
|
|
|
}
|
|
|
|
if (vcp->vc_tolocal) {
|
|
|
|
iconv_close(vcp->vc_tolocal);
|
|
|
|
/* Use NULL until ASCII -> UTF-8 works*/
|
|
|
|
vcp->vc_tolocal = NULL;
|
|
|
|
}
|
|
|
|
vcp->vc_hflags2 &= ~SMB_FLAGS2_UNICODE;
|
|
|
|
}
|
|
|
|
#endif
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
ssp->ss_tid = SMB_TID_UNKNOWN;
|
|
|
|
error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_CONNECT_ANDX, scred, &rqp);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
vcp = rqp->sr_vc;
|
|
|
|
caseopt = SMB_CS_NONE;
|
|
|
|
if (vcp->vc_sopt.sv_sm & SMB_SM_USER) {
|
|
|
|
plen = 1;
|
|
|
|
pp = "";
|
2002-01-04 05:39:37 +03:00
|
|
|
pbuf = NULL;
|
|
|
|
encpass = NULL;
|
2000-12-07 06:48:09 +03:00
|
|
|
} else {
|
2002-01-04 05:39:37 +03:00
|
|
|
pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK);
|
|
|
|
encpass = malloc(24, M_SMBTEMP, M_WAITOK);
|
2003-03-24 18:03:58 +03:00
|
|
|
/*
|
|
|
|
* We try w/o uppercasing first so Samba mixed case
|
|
|
|
* passwords work. If that fails we come back and try
|
|
|
|
* uppercasing to satisfy OS/2 and Windows for Workgroups.
|
|
|
|
*/
|
|
|
|
if (upper) {
|
|
|
|
iconv_convstr(vcp->vc_toupper, pbuf,
|
2004-04-21 22:16:14 +04:00
|
|
|
smb_share_getpass(ssp), SMB_MAXPASSWORDLEN + 1);
|
2003-03-24 18:03:58 +03:00
|
|
|
} else {
|
2004-04-21 22:16:14 +04:00
|
|
|
strlcpy(pbuf, smb_share_getpass(ssp),
|
|
|
|
SMB_MAXPASSWORDLEN + 1);
|
2003-03-24 18:03:58 +03:00
|
|
|
}
|
2000-12-07 06:48:09 +03:00
|
|
|
if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) {
|
|
|
|
plen = 24;
|
|
|
|
smb_encrypt(pbuf, vcp->vc_ch, encpass);
|
|
|
|
pp = encpass;
|
|
|
|
} else {
|
|
|
|
plen = strlen(pbuf) + 1;
|
|
|
|
pp = pbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mbp = &rqp->sr_rq;
|
|
|
|
smb_rq_wstart(rqp);
|
2002-01-04 05:39:37 +03:00
|
|
|
mb_put_uint8(mbp, 0xff);
|
|
|
|
mb_put_uint8(mbp, 0);
|
|
|
|
mb_put_uint16le(mbp, 0);
|
|
|
|
mb_put_uint16le(mbp, 0); /* Flags */
|
|
|
|
mb_put_uint16le(mbp, plen);
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_rq_wend(rqp);
|
|
|
|
smb_rq_bstart(rqp);
|
|
|
|
mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_put_dmem(mbp, vcp, "\\\\", 2, caseopt);
|
|
|
|
pp = vcp->vc_srvname;
|
|
|
|
smb_put_dmem(mbp, vcp, pp, strlen(pp), caseopt);
|
|
|
|
smb_put_dmem(mbp, vcp, "\\", 1, caseopt);
|
|
|
|
pp = ssp->ss_name;
|
|
|
|
smb_put_dstring(mbp, vcp, pp, caseopt);
|
2000-12-07 06:48:09 +03:00
|
|
|
pp = smb_share_typename(ssp->ss_type);
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_put_dstring(mbp, vcp, pp, caseopt);
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_rq_bend(rqp);
|
|
|
|
error = smb_rq_simple(rqp);
|
2008-06-24 14:37:19 +04:00
|
|
|
SMBSDEBUG(("%d\n", error));
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
goto bad;
|
2002-01-04 05:39:37 +03:00
|
|
|
ssp->ss_tid = rqp->sr_rptid;
|
|
|
|
ssp->ss_vcgenid = vcp->vc_genid;
|
|
|
|
ssp->ss_flags |= SMBS_CONNECTED;
|
2000-12-07 06:48:09 +03:00
|
|
|
bad:
|
2002-01-04 05:39:37 +03:00
|
|
|
if (encpass)
|
|
|
|
free(encpass, M_SMBTEMP);
|
|
|
|
if (pbuf)
|
|
|
|
free(pbuf, M_SMBTEMP);
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_rq_done(rqp);
|
2003-03-24 18:03:58 +03:00
|
|
|
if (error && !upper) {
|
|
|
|
upper = 1;
|
|
|
|
goto again;
|
|
|
|
}
|
2000-12-07 06:48:09 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
smb_smb_treedisconnect(struct smb_share *ssp, struct smb_cred *scred)
|
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct smb_rq *rqp;
|
2000-12-07 06:48:09 +03:00
|
|
|
int error;
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
if (ssp->ss_tid == SMB_TID_UNKNOWN)
|
2000-12-07 06:48:09 +03:00
|
|
|
return 0;
|
2002-01-04 05:39:37 +03:00
|
|
|
error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_DISCONNECT, scred, &rqp);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
smb_rq_wstart(rqp);
|
|
|
|
smb_rq_wend(rqp);
|
|
|
|
smb_rq_bstart(rqp);
|
|
|
|
smb_rq_bend(rqp);
|
|
|
|
error = smb_rq_simple(rqp);
|
2008-06-24 14:37:19 +04:00
|
|
|
SMBSDEBUG(("%d\n", error));
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_rq_done(rqp);
|
2002-01-04 05:39:37 +03:00
|
|
|
ssp->ss_tid = SMB_TID_UNKNOWN;
|
2000-12-07 06:48:09 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2005-12-24 23:45:08 +03:00
|
|
|
static inline int
|
2003-03-24 17:24:14 +03:00
|
|
|
smb_smb_readx(struct smb_share *ssp, u_int16_t fid, size_t *len, size_t *rresid,
|
|
|
|
struct uio *uio, struct smb_cred *scred)
|
|
|
|
{
|
|
|
|
struct smb_rq *rqp;
|
|
|
|
struct mbchain *mbp;
|
|
|
|
struct mdchain *mdp;
|
|
|
|
u_int8_t wc;
|
|
|
|
int error;
|
|
|
|
u_int16_t residhi, residlo, off, doff;
|
|
|
|
u_int32_t resid;
|
|
|
|
|
2009-10-19 02:53:36 +04:00
|
|
|
if (!(SMB_CAPS(SSTOVC(ssp)) & SMB_CAP_LARGE_FILES) &&
|
|
|
|
uio->uio_offset >= (1LL << 32)) {
|
|
|
|
/* Cannot read at/beyond 4G */
|
|
|
|
return (EFBIG);
|
|
|
|
}
|
|
|
|
|
2009-10-19 03:20:31 +04:00
|
|
|
if (!(SMB_CAPS(SSTOVC(ssp)) & SMB_CAP_LARGE_READX)) {
|
2009-10-19 02:53:36 +04:00
|
|
|
size_t blksz;
|
|
|
|
|
|
|
|
blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 64;
|
|
|
|
if (blksz > 0xffff)
|
|
|
|
blksz = 0xffff;
|
|
|
|
|
|
|
|
*len = min(blksz, *len);
|
|
|
|
}
|
|
|
|
|
2003-03-24 17:24:14 +03:00
|
|
|
error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ_ANDX, scred, &rqp);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
smb_rq_getrequest(rqp, &mbp);
|
|
|
|
smb_rq_wstart(rqp);
|
|
|
|
mb_put_uint8(mbp, 0xff); /* no secondary command */
|
|
|
|
mb_put_uint8(mbp, 0); /* MBZ */
|
|
|
|
mb_put_uint16le(mbp, 0); /* offset to secondary */
|
2007-03-04 08:59:00 +03:00
|
|
|
mb_put_mem(mbp, (void *)&fid, sizeof(fid), MB_MSYSTEM);
|
2003-03-24 17:24:14 +03:00
|
|
|
mb_put_uint32le(mbp, uio->uio_offset);
|
|
|
|
*len = min(SSTOVC(ssp)->vc_rxmax, *len);
|
|
|
|
mb_put_uint16le(mbp, *len); /* MaxCount */
|
|
|
|
mb_put_uint16le(mbp, *len); /* MinCount (only indicates blocking) */
|
|
|
|
mb_put_uint32le(mbp, *len >> 16); /* MaxCountHigh */
|
|
|
|
mb_put_uint16le(mbp, *len); /* Remaining ("obsolete") */
|
|
|
|
mb_put_uint32le(mbp, uio->uio_offset >> 32); /* OffsetHigh */
|
|
|
|
smb_rq_wend(rqp);
|
|
|
|
smb_rq_bstart(rqp);
|
|
|
|
smb_rq_bend(rqp);
|
|
|
|
do {
|
|
|
|
error = smb_rq_simple(rqp);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
smb_rq_getreply(rqp, &mdp);
|
|
|
|
off = SMB_HDRLEN;
|
|
|
|
md_get_uint8(mdp, &wc);
|
|
|
|
off++;
|
|
|
|
if (wc != 12) {
|
|
|
|
error = EBADRPC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
md_get_uint8(mdp, NULL);
|
|
|
|
off++;
|
|
|
|
md_get_uint8(mdp, NULL);
|
|
|
|
off++;
|
|
|
|
md_get_uint16(mdp, NULL);
|
|
|
|
off += 2;
|
|
|
|
md_get_uint16(mdp, NULL);
|
|
|
|
off += 2;
|
|
|
|
md_get_uint16(mdp, NULL); /* data compaction mode */
|
|
|
|
off += 2;
|
|
|
|
md_get_uint16(mdp, NULL);
|
|
|
|
off += 2;
|
|
|
|
md_get_uint16le(mdp, &residlo);
|
|
|
|
off += 2;
|
|
|
|
md_get_uint16le(mdp, &doff); /* data offset */
|
|
|
|
off += 2;
|
|
|
|
md_get_uint16le(mdp, &residhi);
|
|
|
|
off += 2;
|
|
|
|
resid = (residhi << 16) | residlo;
|
|
|
|
md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM);
|
|
|
|
off += 4*2;
|
|
|
|
md_get_uint16(mdp, NULL); /* ByteCount */
|
|
|
|
off += 2;
|
|
|
|
if (doff > off) /* pad byte(s)? */
|
|
|
|
md_get_mem(mdp, NULL, doff - off, MB_MSYSTEM);
|
|
|
|
if (resid == 0) {
|
|
|
|
*rresid = resid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
error = md_get_uio(mdp, uio, resid);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
*rresid = resid;
|
|
|
|
} while(0);
|
|
|
|
smb_rq_done(rqp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2005-12-24 23:45:08 +03:00
|
|
|
static inline int
|
2003-03-24 17:24:14 +03:00
|
|
|
smb_smb_writex(struct smb_share *ssp, u_int16_t fid, size_t *len, size_t *rresid,
|
|
|
|
struct uio *uio, struct smb_cred *scred)
|
|
|
|
{
|
|
|
|
struct smb_rq *rqp;
|
|
|
|
struct mbchain *mbp;
|
|
|
|
struct mdchain *mdp;
|
|
|
|
int error;
|
|
|
|
u_int8_t wc;
|
|
|
|
u_int16_t resid;
|
|
|
|
|
2009-10-19 02:53:36 +04:00
|
|
|
if (!(SMB_CAPS(SSTOVC(ssp)) & SMB_CAP_LARGE_FILES) &&
|
|
|
|
uio->uio_offset >= (1LL << 32)) {
|
|
|
|
/* Cannot write at/beyond 4G */
|
|
|
|
return (EFBIG);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SMB_CAPS(SSTOVC(ssp)) & SMB_CAP_LARGE_WRITEX) {
|
|
|
|
*len = min(SSTOVC(ssp)->vc_wxmax, *len);
|
|
|
|
} else {
|
|
|
|
size_t blksz;
|
|
|
|
|
|
|
|
blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 64;
|
|
|
|
if (blksz > 0xffff)
|
|
|
|
blksz = 0xffff;
|
|
|
|
|
|
|
|
*len = min(blksz, *len);
|
|
|
|
}
|
|
|
|
|
2003-03-24 17:24:14 +03:00
|
|
|
error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE_ANDX, scred, &rqp);
|
2009-10-19 02:53:36 +04:00
|
|
|
if (error != 0)
|
2003-03-24 17:24:14 +03:00
|
|
|
return (error);
|
|
|
|
smb_rq_getrequest(rqp, &mbp);
|
|
|
|
smb_rq_wstart(rqp);
|
|
|
|
mb_put_uint8(mbp, 0xff); /* no secondary command */
|
|
|
|
mb_put_uint8(mbp, 0); /* MBZ */
|
|
|
|
mb_put_uint16le(mbp, 0); /* offset to secondary */
|
2007-03-04 08:59:00 +03:00
|
|
|
mb_put_mem(mbp, (void *)&fid, sizeof(fid), MB_MSYSTEM);
|
2003-03-24 17:24:14 +03:00
|
|
|
mb_put_uint32le(mbp, uio->uio_offset);
|
|
|
|
mb_put_uint32le(mbp, 0); /* MBZ (timeout) */
|
|
|
|
mb_put_uint16le(mbp, 0); /* !write-thru */
|
|
|
|
mb_put_uint16le(mbp, 0);
|
|
|
|
mb_put_uint16le(mbp, *len >> 16);
|
|
|
|
mb_put_uint16le(mbp, *len);
|
|
|
|
mb_put_uint16le(mbp, 64); /* data offset from header start */
|
|
|
|
mb_put_uint32le(mbp, uio->uio_offset >> 32); /* OffsetHigh */
|
|
|
|
smb_rq_wend(rqp);
|
|
|
|
smb_rq_bstart(rqp);
|
|
|
|
do {
|
|
|
|
mb_put_uint8(mbp, 0xee); /* mimic xp pad byte! */
|
|
|
|
error = mb_put_uio(mbp, uio, *len);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
smb_rq_bend(rqp);
|
|
|
|
error = smb_rq_simple(rqp);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
smb_rq_getreply(rqp, &mdp);
|
|
|
|
md_get_uint8(mdp, &wc);
|
|
|
|
if (wc != 6) {
|
|
|
|
error = EBADRPC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
md_get_uint8(mdp, NULL);
|
|
|
|
md_get_uint8(mdp, NULL);
|
|
|
|
md_get_uint16(mdp, NULL);
|
|
|
|
md_get_uint16le(mdp, &resid);
|
|
|
|
*rresid = resid;
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
smb_rq_done(rqp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2005-12-24 23:45:08 +03:00
|
|
|
static inline int
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_smb_read(struct smb_share *ssp, u_int16_t fid,
|
2003-03-24 17:24:14 +03:00
|
|
|
size_t *len, size_t *rresid, struct uio *uio, struct smb_cred *scred)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct smb_rq *rqp;
|
|
|
|
struct mbchain *mbp;
|
|
|
|
struct mdchain *mdp;
|
2000-12-07 06:48:09 +03:00
|
|
|
u_int16_t resid, bc;
|
|
|
|
u_int8_t wc;
|
|
|
|
int error, rlen, blksz;
|
|
|
|
|
2003-03-24 17:11:32 +03:00
|
|
|
/* Cannot read at/beyond 4G */
|
|
|
|
if (uio->uio_offset >= (1LL << 32))
|
|
|
|
return (EFBIG);
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ, scred, &rqp);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16;
|
|
|
|
rlen = *len = min(blksz, *len);
|
|
|
|
|
|
|
|
smb_rq_getrequest(rqp, &mbp);
|
|
|
|
smb_rq_wstart(rqp);
|
2007-03-04 08:59:00 +03:00
|
|
|
mb_put_mem(mbp, (void *)&fid, sizeof(fid), MB_MSYSTEM);
|
2002-01-04 05:39:37 +03:00
|
|
|
mb_put_uint16le(mbp, rlen);
|
|
|
|
mb_put_uint32le(mbp, uio->uio_offset);
|
|
|
|
mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff));
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_rq_wend(rqp);
|
|
|
|
smb_rq_bstart(rqp);
|
|
|
|
smb_rq_bend(rqp);
|
|
|
|
do {
|
|
|
|
error = smb_rq_simple(rqp);
|
|
|
|
if (error)
|
|
|
|
break;
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_rq_getreply(rqp, &mdp);
|
|
|
|
md_get_uint8(mdp, &wc);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (wc != 5) {
|
|
|
|
error = EBADRPC;
|
|
|
|
break;
|
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
md_get_uint16le(mdp, &resid);
|
|
|
|
md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM);
|
|
|
|
md_get_uint16le(mdp, &bc);
|
|
|
|
md_get_uint8(mdp, NULL); /* ignore buffer type */
|
|
|
|
md_get_uint16le(mdp, &resid);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (resid == 0) {
|
|
|
|
*rresid = resid;
|
|
|
|
break;
|
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
error = md_get_uio(mdp, uio, resid);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
*rresid = resid;
|
|
|
|
} while(0);
|
|
|
|
smb_rq_done(rqp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
smb_read(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
|
|
|
|
struct smb_cred *scred)
|
|
|
|
{
|
2003-03-24 17:24:14 +03:00
|
|
|
size_t tsize, len, resid;
|
2000-12-07 06:48:09 +03:00
|
|
|
int error = 0;
|
2009-10-19 02:53:36 +04:00
|
|
|
bool rx = (SMB_CAPS(SSTOVC(ssp)) &
|
|
|
|
(SMB_CAP_LARGE_FILES|SMB_CAP_LARGE_READX)) != 0;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2006-05-11 01:53:14 +04:00
|
|
|
resid = 0; /* XXX gcc */
|
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
tsize = uio->uio_resid;
|
|
|
|
while (tsize > 0) {
|
|
|
|
len = tsize;
|
2003-03-24 17:24:14 +03:00
|
|
|
if (rx)
|
|
|
|
error = smb_smb_readx(ssp, fid, &len, &resid, uio, scred);
|
|
|
|
else
|
|
|
|
error = smb_smb_read(ssp, fid, &len, &resid, uio, scred);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
tsize -= resid;
|
|
|
|
if (resid < len)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2005-12-24 23:45:08 +03:00
|
|
|
static inline int
|
2003-03-24 17:24:14 +03:00
|
|
|
smb_smb_write(struct smb_share *ssp, u_int16_t fid, size_t *len, size_t *rresid,
|
2000-12-07 06:48:09 +03:00
|
|
|
struct uio *uio, struct smb_cred *scred)
|
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct smb_rq *rqp;
|
|
|
|
struct mbchain *mbp;
|
|
|
|
struct mdchain *mdp;
|
2000-12-07 06:48:09 +03:00
|
|
|
u_int16_t resid;
|
|
|
|
u_int8_t wc;
|
|
|
|
int error, blksz;
|
|
|
|
|
2003-03-24 17:11:32 +03:00
|
|
|
/* Cannot write at/beyond 4G */
|
|
|
|
if (uio->uio_offset >= (1LL << 32))
|
|
|
|
return (EFBIG);
|
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16;
|
|
|
|
if (blksz > 0xffff)
|
|
|
|
blksz = 0xffff;
|
|
|
|
|
|
|
|
resid = *len = min(blksz, *len);
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE, scred, &rqp);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
smb_rq_getrequest(rqp, &mbp);
|
|
|
|
smb_rq_wstart(rqp);
|
2007-03-04 08:59:00 +03:00
|
|
|
mb_put_mem(mbp, (void *)&fid, sizeof(fid), MB_MSYSTEM);
|
2002-01-04 05:39:37 +03:00
|
|
|
mb_put_uint16le(mbp, resid);
|
|
|
|
mb_put_uint32le(mbp, uio->uio_offset);
|
|
|
|
mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff));
|
2000-12-07 06:48:09 +03:00
|
|
|
smb_rq_wend(rqp);
|
|
|
|
smb_rq_bstart(rqp);
|
2002-01-04 05:39:37 +03:00
|
|
|
mb_put_uint8(mbp, SMB_DT_DATA);
|
|
|
|
mb_put_uint16le(mbp, resid);
|
2000-12-07 06:48:09 +03:00
|
|
|
do {
|
2002-01-04 05:39:37 +03:00
|
|
|
error = mb_put_uio(mbp, uio, resid);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
smb_rq_bend(rqp);
|
|
|
|
error = smb_rq_simple(rqp);
|
|
|
|
if (error)
|
|
|
|
break;
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_rq_getreply(rqp, &mdp);
|
|
|
|
md_get_uint8(mdp, &wc);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (wc != 1) {
|
|
|
|
error = EBADRPC;
|
|
|
|
break;
|
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
md_get_uint16le(mdp, &resid);
|
2000-12-07 06:48:09 +03:00
|
|
|
*rresid = resid;
|
|
|
|
} while(0);
|
|
|
|
smb_rq_done(rqp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
smb_write(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
|
|
|
|
struct smb_cred *scred)
|
|
|
|
{
|
2003-03-24 17:24:14 +03:00
|
|
|
int error = 0;
|
|
|
|
size_t len, tsize, resid;
|
2009-10-19 02:53:36 +04:00
|
|
|
bool wx = (SMB_CAPS(SSTOVC(ssp)) &
|
|
|
|
(SMB_CAP_LARGE_FILES|SMB_CAP_LARGE_WRITEX)) != 0;
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2006-05-11 01:53:14 +04:00
|
|
|
resid = 0; /* XXX gcc */
|
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
tsize = uio->uio_resid;
|
|
|
|
while (tsize > 0) {
|
|
|
|
len = tsize;
|
2003-03-24 17:24:14 +03:00
|
|
|
if (wx)
|
|
|
|
error = smb_smb_writex(ssp, fid, &len, &resid, uio, scred);
|
|
|
|
else
|
|
|
|
error = smb_smb_write(ssp, fid, &len, &resid, uio, scred);
|
2009-10-19 02:53:36 +04:00
|
|
|
if (error != 0)
|
2000-12-07 06:48:09 +03:00
|
|
|
break;
|
|
|
|
if (resid < len) {
|
|
|
|
error = EIO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tsize -= resid;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
|
2003-03-04 00:16:02 +03:00
|
|
|
#if 0
|
2002-01-04 05:39:37 +03:00
|
|
|
int
|
|
|
|
smb_smb_echo(struct smb_vc *vcp, struct smb_cred *scred)
|
|
|
|
{
|
|
|
|
struct smb_rq *rqp;
|
|
|
|
struct mbchain *mbp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_ECHO, scred, &rqp);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
mbp = &rqp->sr_rq;
|
|
|
|
smb_rq_wstart(rqp);
|
|
|
|
mb_put_uint16le(mbp, 1);
|
|
|
|
smb_rq_wend(rqp);
|
|
|
|
smb_rq_bstart(rqp);
|
|
|
|
mb_put_uint32le(mbp, 0);
|
|
|
|
smb_rq_bend(rqp);
|
|
|
|
error = smb_rq_simple(rqp);
|
2008-06-24 14:37:19 +04:00
|
|
|
SMBSDEBUG(("%d\n", error));
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_rq_done(rqp);
|
|
|
|
return error;
|
|
|
|
}
|
2003-03-04 00:16:02 +03:00
|
|
|
#endif
|