1c398230bSWarner Losh /*- 2681a5bbeSBoris Popov * Copyright (c) 2000-2001 Boris Popov 3681a5bbeSBoris Popov * All rights reserved. 4681a5bbeSBoris Popov * 5681a5bbeSBoris Popov * Redistribution and use in source and binary forms, with or without 6681a5bbeSBoris Popov * modification, are permitted provided that the following conditions 7681a5bbeSBoris Popov * are met: 8681a5bbeSBoris Popov * 1. Redistributions of source code must retain the above copyright 9681a5bbeSBoris Popov * notice, this list of conditions and the following disclaimer. 10681a5bbeSBoris Popov * 2. Redistributions in binary form must reproduce the above copyright 11681a5bbeSBoris Popov * notice, this list of conditions and the following disclaimer in the 12681a5bbeSBoris Popov * documentation and/or other materials provided with the distribution. 13681a5bbeSBoris Popov * 3. All advertising materials mentioning features or use of this software 14681a5bbeSBoris Popov * must display the following acknowledgement: 15681a5bbeSBoris Popov * This product includes software developed by Boris Popov. 16681a5bbeSBoris Popov * 4. Neither the name of the author nor the names of any co-contributors 17681a5bbeSBoris Popov * may be used to endorse or promote products derived from this software 18681a5bbeSBoris Popov * without specific prior written permission. 19681a5bbeSBoris Popov * 20681a5bbeSBoris Popov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21681a5bbeSBoris Popov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22681a5bbeSBoris Popov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23681a5bbeSBoris Popov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24681a5bbeSBoris Popov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25681a5bbeSBoris Popov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26681a5bbeSBoris Popov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27681a5bbeSBoris Popov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28681a5bbeSBoris Popov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29681a5bbeSBoris Popov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30681a5bbeSBoris Popov * SUCH DAMAGE. 31681a5bbeSBoris Popov */ 32ab0de15bSDavid E. O'Brien 33681a5bbeSBoris Popov /* 34681a5bbeSBoris Popov * various SMB requests. Most of the routines merely packs data into mbufs. 35681a5bbeSBoris Popov */ 36ab0de15bSDavid E. O'Brien 37ab0de15bSDavid E. O'Brien #include <sys/cdefs.h> 38ab0de15bSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39ab0de15bSDavid E. O'Brien 40681a5bbeSBoris Popov #include <sys/param.h> 41681a5bbeSBoris Popov #include <sys/systm.h> 42681a5bbeSBoris Popov #include <sys/kernel.h> 43681a5bbeSBoris Popov #include <sys/malloc.h> 44681a5bbeSBoris Popov #include <sys/proc.h> 45681a5bbeSBoris Popov #include <sys/lock.h> 46681a5bbeSBoris Popov #include <sys/sysctl.h> 47681a5bbeSBoris Popov #include <sys/socket.h> 48681a5bbeSBoris Popov #include <sys/uio.h> 49681a5bbeSBoris Popov 50681a5bbeSBoris Popov #include <sys/iconv.h> 51681a5bbeSBoris Popov 52681a5bbeSBoris Popov #include <netsmb/smb.h> 53681a5bbeSBoris Popov #include <netsmb/smb_subr.h> 54681a5bbeSBoris Popov #include <netsmb/smb_rq.h> 55681a5bbeSBoris Popov #include <netsmb/smb_conn.h> 56681a5bbeSBoris Popov #include <netsmb/smb_tran.h> 57681a5bbeSBoris Popov 58190b2c4fSTim J. Robbins #include "opt_netsmb.h" 59190b2c4fSTim J. Robbins 60681a5bbeSBoris Popov struct smb_dialect { 61681a5bbeSBoris Popov int d_id; 62681a5bbeSBoris Popov const char * d_name; 63681a5bbeSBoris Popov }; 64681a5bbeSBoris Popov 65681a5bbeSBoris Popov static struct smb_dialect smb_dialects[] = { 66681a5bbeSBoris Popov {SMB_DIALECT_CORE, "PC NETWORK PROGRAM 1.0"}, 67681a5bbeSBoris Popov {SMB_DIALECT_COREPLUS, "MICROSOFT NETWORKS 1.03"}, 68681a5bbeSBoris Popov {SMB_DIALECT_LANMAN1_0, "MICROSOFT NETWORKS 3.0"}, 69681a5bbeSBoris Popov {SMB_DIALECT_LANMAN1_0, "LANMAN1.0"}, 70681a5bbeSBoris Popov {SMB_DIALECT_LANMAN2_0, "LM1.2X002"}, 71681a5bbeSBoris Popov {SMB_DIALECT_LANMAN2_0, "Samba"}, 72681a5bbeSBoris Popov {SMB_DIALECT_NTLM0_12, "NT LANMAN 1.0"}, 73681a5bbeSBoris Popov {SMB_DIALECT_NTLM0_12, "NT LM 0.12"}, 74681a5bbeSBoris Popov {-1, NULL} 75681a5bbeSBoris Popov }; 76681a5bbeSBoris Popov 77681a5bbeSBoris Popov #define SMB_DIALECT_MAX (sizeof(smb_dialects) / sizeof(struct smb_dialect) - 2) 78681a5bbeSBoris Popov 79e2a70cffSBoris Popov static u_int32_t 80e2a70cffSBoris Popov smb_vc_maxread(struct smb_vc *vcp) 81e2a70cffSBoris Popov { 82e2a70cffSBoris Popov /* 83e2a70cffSBoris Popov * Specs say up to 64k data bytes, but Windows traffic 84e2a70cffSBoris Popov * uses 60k... no doubt for some good reason. 85190b2c4fSTim J. Robbins * 86190b2c4fSTim J. Robbins * Don't exceed the server's buffer size if signatures 87190b2c4fSTim J. Robbins * are enabled otherwise Windows 2003 chokes. Allow space 88190b2c4fSTim J. Robbins * for the SMB header & a little bit extra. 89e2a70cffSBoris Popov */ 90190b2c4fSTim J. Robbins if ((vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_READX) && 91190b2c4fSTim J. Robbins (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0) 92e2a70cffSBoris Popov return (60*1024); 93e2a70cffSBoris Popov else 94190b2c4fSTim J. Robbins return (vcp->vc_sopt.sv_maxtx - SMB_HDRLEN - 64); 95e2a70cffSBoris Popov } 96e2a70cffSBoris Popov 97e2a70cffSBoris Popov static u_int32_t 98e2a70cffSBoris Popov smb_vc_maxwrite(struct smb_vc *vcp) 99e2a70cffSBoris Popov { 100e2a70cffSBoris Popov /* 101190b2c4fSTim J. Robbins * See comment above. 102e2a70cffSBoris Popov */ 103190b2c4fSTim J. Robbins if ((vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX) && 104190b2c4fSTim J. Robbins (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0) 105e2a70cffSBoris Popov return (60*1024); 106e2a70cffSBoris Popov else 107190b2c4fSTim J. Robbins return (vcp->vc_sopt.sv_maxtx - SMB_HDRLEN - 64); 108e2a70cffSBoris Popov } 109e2a70cffSBoris Popov 110681a5bbeSBoris Popov static int 111681a5bbeSBoris Popov smb_smb_nomux(struct smb_vc *vcp, struct smb_cred *scred, const char *name) 112681a5bbeSBoris Popov { 113fce6fbfaSBoris Popov if (scred->scr_td->td_proc == vcp->vc_iod->iod_p) 114681a5bbeSBoris Popov return 0; 115681a5bbeSBoris Popov SMBERROR("wrong function called(%s)\n", name); 116681a5bbeSBoris Popov return EINVAL; 117681a5bbeSBoris Popov } 118681a5bbeSBoris Popov 119681a5bbeSBoris Popov int 120681a5bbeSBoris Popov smb_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred) 121681a5bbeSBoris Popov { 122681a5bbeSBoris Popov struct smb_dialect *dp; 123681a5bbeSBoris Popov struct smb_sopt *sp = NULL; 124681a5bbeSBoris Popov struct smb_rq *rqp; 125681a5bbeSBoris Popov struct mbchain *mbp; 126681a5bbeSBoris Popov struct mdchain *mdp; 127681a5bbeSBoris Popov u_int8_t wc, stime[8], sblen; 128681a5bbeSBoris Popov u_int16_t dindex, tw, tw1, swlen, bc; 129681a5bbeSBoris Popov int error, maxqsz; 130681a5bbeSBoris Popov 1316e551fb6SDavid E. O'Brien if (smb_smb_nomux(vcp, scred, __func__) != 0) 132681a5bbeSBoris Popov return EINVAL; 133681a5bbeSBoris Popov vcp->vc_hflags = 0; 134681a5bbeSBoris Popov vcp->vc_hflags2 = 0; 135681a5bbeSBoris Popov vcp->obj.co_flags &= ~(SMBV_ENCRYPT); 136681a5bbeSBoris Popov sp = &vcp->vc_sopt; 137681a5bbeSBoris Popov bzero(sp, sizeof(struct smb_sopt)); 138681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_NEGOTIATE, scred, &rqp); 139681a5bbeSBoris Popov if (error) 140681a5bbeSBoris Popov return error; 141681a5bbeSBoris Popov smb_rq_getrequest(rqp, &mbp); 142681a5bbeSBoris Popov smb_rq_wstart(rqp); 143681a5bbeSBoris Popov smb_rq_wend(rqp); 144681a5bbeSBoris Popov smb_rq_bstart(rqp); 145681a5bbeSBoris Popov for(dp = smb_dialects; dp->d_id != -1; dp++) { 146681a5bbeSBoris Popov mb_put_uint8(mbp, SMB_DT_DIALECT); 147681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, dp->d_name, SMB_CS_NONE); 148681a5bbeSBoris Popov } 149681a5bbeSBoris Popov smb_rq_bend(rqp); 150681a5bbeSBoris Popov error = smb_rq_simple(rqp); 151681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 152681a5bbeSBoris Popov if (error) 153681a5bbeSBoris Popov goto bad; 154681a5bbeSBoris Popov smb_rq_getreply(rqp, &mdp); 155681a5bbeSBoris Popov do { 156681a5bbeSBoris Popov error = md_get_uint8(mdp, &wc); 157681a5bbeSBoris Popov if (error) 158681a5bbeSBoris Popov break; 159681a5bbeSBoris Popov error = md_get_uint16le(mdp, &dindex); 160681a5bbeSBoris Popov if (error) 161681a5bbeSBoris Popov break; 162681a5bbeSBoris Popov if (dindex > 7) { 163681a5bbeSBoris Popov SMBERROR("Don't know how to talk with server %s (%d)\n", "xxx", dindex); 164681a5bbeSBoris Popov error = EBADRPC; 165681a5bbeSBoris Popov break; 166681a5bbeSBoris Popov } 167681a5bbeSBoris Popov dp = smb_dialects + dindex; 168681a5bbeSBoris Popov sp->sv_proto = dp->d_id; 169681a5bbeSBoris Popov SMBSDEBUG("Dialect %s (%d, %d)\n", dp->d_name, dindex, wc); 170681a5bbeSBoris Popov error = EBADRPC; 171681a5bbeSBoris Popov if (dp->d_id >= SMB_DIALECT_NTLM0_12) { 172681a5bbeSBoris Popov if (wc != 17) 173681a5bbeSBoris Popov break; 174681a5bbeSBoris Popov md_get_uint8(mdp, &sp->sv_sm); 175681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxmux); 176681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxvcs); 177681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_maxtx); 178681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_maxraw); 179681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_skey); 180681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_caps); 181681a5bbeSBoris Popov md_get_mem(mdp, stime, 8, MB_MSYSTEM); 182681a5bbeSBoris Popov md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz); 183681a5bbeSBoris Popov md_get_uint8(mdp, &sblen); 184681a5bbeSBoris Popov if (sblen && (sp->sv_sm & SMB_SM_ENCRYPT)) { 185681a5bbeSBoris Popov if (sblen != SMB_MAXCHALLENGELEN) { 186681a5bbeSBoris Popov SMBERROR("Unexpected length of security blob (%d)\n", sblen); 187681a5bbeSBoris Popov break; 188681a5bbeSBoris Popov } 189681a5bbeSBoris Popov error = md_get_uint16(mdp, &bc); 190681a5bbeSBoris Popov if (error) 191681a5bbeSBoris Popov break; 192681a5bbeSBoris Popov if (sp->sv_caps & SMB_CAP_EXT_SECURITY) 193681a5bbeSBoris Popov md_get_mem(mdp, NULL, 16, MB_MSYSTEM); 194681a5bbeSBoris Popov error = md_get_mem(mdp, vcp->vc_ch, sblen, MB_MSYSTEM); 195681a5bbeSBoris Popov if (error) 196681a5bbeSBoris Popov break; 197681a5bbeSBoris Popov vcp->vc_chlen = sblen; 198681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_ENCRYPT; 199681a5bbeSBoris Popov } 200190b2c4fSTim J. Robbins #ifdef NETSMBCRYPTO 201190b2c4fSTim J. Robbins if (sp->sv_sm & SMB_SM_SIGS_REQUIRE) 202190b2c4fSTim J. Robbins vcp->vc_hflags2 |= SMB_FLAGS2_SECURITY_SIGNATURE; 203190b2c4fSTim J. Robbins #endif 204681a5bbeSBoris Popov vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES; 205681a5bbeSBoris Popov if (dp->d_id == SMB_DIALECT_NTLM0_12 && 206681a5bbeSBoris Popov sp->sv_maxtx < 4096 && 207681a5bbeSBoris Popov (sp->sv_caps & SMB_CAP_NT_SMBS) == 0) { 208681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_WIN95; 209681a5bbeSBoris Popov SMBSDEBUG("Win95 detected\n"); 210681a5bbeSBoris Popov } 211681a5bbeSBoris Popov } else if (dp->d_id > SMB_DIALECT_CORE) { 212681a5bbeSBoris Popov md_get_uint16le(mdp, &tw); 213681a5bbeSBoris Popov sp->sv_sm = tw; 214681a5bbeSBoris Popov md_get_uint16le(mdp, &tw); 215681a5bbeSBoris Popov sp->sv_maxtx = tw; 216681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxmux); 217681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxvcs); 218681a5bbeSBoris Popov md_get_uint16le(mdp, &tw); /* rawmode */ 219681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_skey); 220681a5bbeSBoris Popov if (wc == 13) { /* >= LANMAN1 */ 221681a5bbeSBoris Popov md_get_uint16(mdp, &tw); /* time */ 222681a5bbeSBoris Popov md_get_uint16(mdp, &tw1); /* date */ 223681a5bbeSBoris Popov md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz); 224681a5bbeSBoris Popov md_get_uint16le(mdp, &swlen); 225681a5bbeSBoris Popov if (swlen > SMB_MAXCHALLENGELEN) 226681a5bbeSBoris Popov break; 227681a5bbeSBoris Popov md_get_uint16(mdp, NULL); /* mbz */ 228681a5bbeSBoris Popov if (md_get_uint16(mdp, &bc) != 0) 229681a5bbeSBoris Popov break; 230681a5bbeSBoris Popov if (bc < swlen) 231681a5bbeSBoris Popov break; 232681a5bbeSBoris Popov if (swlen && (sp->sv_sm & SMB_SM_ENCRYPT)) { 233681a5bbeSBoris Popov error = md_get_mem(mdp, vcp->vc_ch, swlen, MB_MSYSTEM); 234681a5bbeSBoris Popov if (error) 235681a5bbeSBoris Popov break; 236681a5bbeSBoris Popov vcp->vc_chlen = swlen; 237681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_ENCRYPT; 238681a5bbeSBoris Popov } 239681a5bbeSBoris Popov } 240681a5bbeSBoris Popov vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES; 241681a5bbeSBoris Popov } else { /* an old CORE protocol */ 242681a5bbeSBoris Popov sp->sv_maxmux = 1; 243681a5bbeSBoris Popov } 244681a5bbeSBoris Popov error = 0; 245681a5bbeSBoris Popov } while (0); 246681a5bbeSBoris Popov if (error == 0) { 247681a5bbeSBoris Popov vcp->vc_maxvcs = sp->sv_maxvcs; 248681a5bbeSBoris Popov if (vcp->vc_maxvcs <= 1) { 249681a5bbeSBoris Popov if (vcp->vc_maxvcs == 0) 250681a5bbeSBoris Popov vcp->vc_maxvcs = 1; 251681a5bbeSBoris Popov } 252681a5bbeSBoris Popov if (sp->sv_maxtx <= 0 || sp->sv_maxtx > 0xffff) 253681a5bbeSBoris Popov sp->sv_maxtx = 1024; 254e2a70cffSBoris Popov else 255e2a70cffSBoris Popov sp->sv_maxtx = min(sp->sv_maxtx, 256e2a70cffSBoris Popov 63*1024 + SMB_HDRLEN + 16); 257e2a70cffSBoris Popov SMB_TRAN_GETPARAM(vcp, SMBTP_RCVSZ, &maxqsz); 258e2a70cffSBoris Popov vcp->vc_rxmax = min(smb_vc_maxread(vcp), maxqsz - 1024); 259681a5bbeSBoris Popov SMB_TRAN_GETPARAM(vcp, SMBTP_SNDSZ, &maxqsz); 260e2a70cffSBoris Popov vcp->vc_wxmax = min(smb_vc_maxwrite(vcp), maxqsz - 1024); 261681a5bbeSBoris Popov vcp->vc_txmax = min(sp->sv_maxtx, maxqsz); 262681a5bbeSBoris Popov SMBSDEBUG("TZ = %d\n", sp->sv_tz); 263681a5bbeSBoris Popov SMBSDEBUG("CAPS = %x\n", sp->sv_caps); 264681a5bbeSBoris Popov SMBSDEBUG("MAXMUX = %d\n", sp->sv_maxmux); 265681a5bbeSBoris Popov SMBSDEBUG("MAXVCS = %d\n", sp->sv_maxvcs); 266681a5bbeSBoris Popov SMBSDEBUG("MAXRAW = %d\n", sp->sv_maxraw); 267681a5bbeSBoris Popov SMBSDEBUG("MAXTX = %d\n", sp->sv_maxtx); 268681a5bbeSBoris Popov } 269681a5bbeSBoris Popov bad: 270681a5bbeSBoris Popov smb_rq_done(rqp); 271681a5bbeSBoris Popov return error; 272681a5bbeSBoris Popov } 273681a5bbeSBoris Popov 274681a5bbeSBoris Popov int 275681a5bbeSBoris Popov smb_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred) 276681a5bbeSBoris Popov { 277681a5bbeSBoris Popov struct smb_rq *rqp; 278681a5bbeSBoris Popov struct mbchain *mbp; 279681a5bbeSBoris Popov /* u_int8_t wc; 280681a5bbeSBoris Popov u_int16_t tw, tw1;*/ 281681a5bbeSBoris Popov smb_uniptr unipp, ntencpass = NULL; 282681a5bbeSBoris Popov char *pp, *up, *pbuf, *encpass; 28370f9b9d9SBoris Popov int error, plen, uniplen, ulen, upper; 28470f9b9d9SBoris Popov 28570f9b9d9SBoris Popov upper = 0; 28670f9b9d9SBoris Popov 28770f9b9d9SBoris Popov again: 288681a5bbeSBoris Popov 289681a5bbeSBoris Popov vcp->vc_smbuid = SMB_UID_UNKNOWN; 290681a5bbeSBoris Popov 2916e551fb6SDavid E. O'Brien if (smb_smb_nomux(vcp, scred, __func__) != 0) 292681a5bbeSBoris Popov return EINVAL; 293681a5bbeSBoris Popov 294681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_SESSION_SETUP_ANDX, scred, &rqp); 295681a5bbeSBoris Popov if (error) 296681a5bbeSBoris Popov return error; 297a163d034SWarner Losh pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK); 298a163d034SWarner Losh encpass = malloc(24, M_SMBTEMP, M_WAITOK); 299681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_USER) { 30070f9b9d9SBoris Popov /* 30170f9b9d9SBoris Popov * We try w/o uppercasing first so Samba mixed case 30270f9b9d9SBoris Popov * passwords work. If that fails we come back and try 30370f9b9d9SBoris Popov * uppercasing to satisfy OS/2 and Windows for Workgroups. 30470f9b9d9SBoris Popov */ 30570f9b9d9SBoris Popov if (upper++) { 30670f9b9d9SBoris Popov iconv_convstr(vcp->vc_toupper, pbuf, 30770f9b9d9SBoris Popov smb_vc_getpass(vcp)/*, SMB_MAXPASSWORDLEN*/); 30870f9b9d9SBoris Popov } else { 30970f9b9d9SBoris Popov strncpy(pbuf, smb_vc_getpass(vcp), SMB_MAXPASSWORDLEN); 31070f9b9d9SBoris Popov pbuf[SMB_MAXPASSWORDLEN] = '\0'; 31170f9b9d9SBoris Popov } 31270f9b9d9SBoris Popov if (!SMB_UNICODE_STRINGS(vcp)) 31370f9b9d9SBoris Popov iconv_convstr(vcp->vc_toserver, pbuf, pbuf/*, 31470f9b9d9SBoris Popov SMB_MAXPASSWORDLEN*/); 31570f9b9d9SBoris Popov 316681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) { 317681a5bbeSBoris Popov uniplen = plen = 24; 318681a5bbeSBoris Popov smb_encrypt(pbuf, vcp->vc_ch, encpass); 319a163d034SWarner Losh ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK); 32070f9b9d9SBoris Popov if (SMB_UNICODE_STRINGS(vcp)) { 32170f9b9d9SBoris Popov strncpy(pbuf, smb_vc_getpass(vcp), 32270f9b9d9SBoris Popov SMB_MAXPASSWORDLEN); 32370f9b9d9SBoris Popov pbuf[SMB_MAXPASSWORDLEN] = '\0'; 32470f9b9d9SBoris Popov } else 32570f9b9d9SBoris Popov iconv_convstr(vcp->vc_toserver, pbuf, 32670f9b9d9SBoris Popov smb_vc_getpass(vcp)/*, 32770f9b9d9SBoris Popov SMB_MAXPASSWORDLEN*/); 328681a5bbeSBoris Popov smb_ntencrypt(pbuf, vcp->vc_ch, (u_char*)ntencpass); 329681a5bbeSBoris Popov pp = encpass; 330681a5bbeSBoris Popov unipp = ntencpass; 331681a5bbeSBoris Popov } else { 332681a5bbeSBoris Popov plen = strlen(pbuf) + 1; 333681a5bbeSBoris Popov pp = pbuf; 334681a5bbeSBoris Popov uniplen = plen * 2; 335a163d034SWarner Losh ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK); 336681a5bbeSBoris Popov smb_strtouni(ntencpass, smb_vc_getpass(vcp)); 337681a5bbeSBoris Popov plen--; 338daa35dedSBoris Popov 339daa35dedSBoris Popov /* 340daa35dedSBoris Popov * The uniplen is zeroed because Samba cannot deal 341daa35dedSBoris Popov * with this 2nd cleartext password. This Samba 342daa35dedSBoris Popov * "bug" is actually a workaround for problems in 343daa35dedSBoris Popov * Microsoft clients. 344daa35dedSBoris Popov */ 345681a5bbeSBoris Popov uniplen = 0/*-= 2*/; 346681a5bbeSBoris Popov unipp = ntencpass; 347681a5bbeSBoris Popov } 348681a5bbeSBoris Popov } else { 349681a5bbeSBoris Popov /* 350681a5bbeSBoris Popov * In the share security mode password will be used 351681a5bbeSBoris Popov * only in the tree authentication 352681a5bbeSBoris Popov */ 353681a5bbeSBoris Popov pp = ""; 354681a5bbeSBoris Popov plen = 1; 355681a5bbeSBoris Popov unipp = &smb_unieol; 356cf37bfb0SMax Khon uniplen = 0 /* sizeof(smb_unieol) */; 357681a5bbeSBoris Popov } 358681a5bbeSBoris Popov smb_rq_wstart(rqp); 359681a5bbeSBoris Popov mbp = &rqp->sr_rq; 360681a5bbeSBoris Popov up = vcp->vc_username; 361681a5bbeSBoris Popov ulen = strlen(up) + 1; 36270f9b9d9SBoris Popov /* 36370f9b9d9SBoris Popov * If userid is null we are attempting anonymous browse login 36470f9b9d9SBoris Popov * so passwords must be zero length. 36570f9b9d9SBoris Popov */ 36670f9b9d9SBoris Popov if (ulen == 1) 36770f9b9d9SBoris Popov plen = uniplen = 0; 368681a5bbeSBoris Popov mb_put_uint8(mbp, 0xff); 369681a5bbeSBoris Popov mb_put_uint8(mbp, 0); 370681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); 371681a5bbeSBoris Popov mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxtx); 372681a5bbeSBoris Popov mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxmux); 373681a5bbeSBoris Popov mb_put_uint16le(mbp, vcp->vc_number); 374681a5bbeSBoris Popov mb_put_uint32le(mbp, vcp->vc_sopt.sv_skey); 375681a5bbeSBoris Popov mb_put_uint16le(mbp, plen); 376681a5bbeSBoris Popov if (SMB_DIALECT(vcp) < SMB_DIALECT_NTLM0_12) { 377681a5bbeSBoris Popov mb_put_uint32le(mbp, 0); 378681a5bbeSBoris Popov smb_rq_wend(rqp); 379681a5bbeSBoris Popov smb_rq_bstart(rqp); 380681a5bbeSBoris Popov mb_put_mem(mbp, pp, plen, MB_MSYSTEM); 381681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, up, SMB_CS_NONE); 382681a5bbeSBoris Popov } else { 383681a5bbeSBoris Popov mb_put_uint16le(mbp, uniplen); 384681a5bbeSBoris Popov mb_put_uint32le(mbp, 0); /* reserved */ 385daa35dedSBoris Popov mb_put_uint32le(mbp, vcp->obj.co_flags & SMBV_UNICODE ? 386daa35dedSBoris Popov SMB_CAP_UNICODE : 0); 387681a5bbeSBoris Popov smb_rq_wend(rqp); 388681a5bbeSBoris Popov smb_rq_bstart(rqp); 389681a5bbeSBoris Popov mb_put_mem(mbp, pp, plen, MB_MSYSTEM); 390681a5bbeSBoris Popov mb_put_mem(mbp, (caddr_t)unipp, uniplen, MB_MSYSTEM); 391681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, up, SMB_CS_NONE); /* AccountName */ 392681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, vcp->vc_domain, SMB_CS_NONE); /* PrimaryDomain */ 393681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, "FreeBSD", SMB_CS_NONE); /* Client's OS */ 394681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, "NETSMB", SMB_CS_NONE); /* Client name */ 395681a5bbeSBoris Popov } 396681a5bbeSBoris Popov smb_rq_bend(rqp); 397681a5bbeSBoris Popov if (ntencpass) 398681a5bbeSBoris Popov free(ntencpass, M_SMBTEMP); 399190b2c4fSTim J. Robbins if (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) 400190b2c4fSTim J. Robbins smb_calcmackey(vcp); 401681a5bbeSBoris Popov error = smb_rq_simple(rqp); 402681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 403681a5bbeSBoris Popov if (error) { 404681a5bbeSBoris Popov if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnoaccess) 405681a5bbeSBoris Popov error = EAUTH; 406681a5bbeSBoris Popov goto bad; 407681a5bbeSBoris Popov } 408681a5bbeSBoris Popov vcp->vc_smbuid = rqp->sr_rpuid; 409681a5bbeSBoris Popov bad: 410681a5bbeSBoris Popov free(encpass, M_SMBTEMP); 411681a5bbeSBoris Popov free(pbuf, M_SMBTEMP); 412681a5bbeSBoris Popov smb_rq_done(rqp); 41370f9b9d9SBoris Popov if (error && upper == 1 && vcp->vc_sopt.sv_sm & SMB_SM_USER) 41470f9b9d9SBoris Popov goto again; 415681a5bbeSBoris Popov return error; 416681a5bbeSBoris Popov } 417681a5bbeSBoris Popov 418681a5bbeSBoris Popov int 419681a5bbeSBoris Popov smb_smb_ssnclose(struct smb_vc *vcp, struct smb_cred *scred) 420681a5bbeSBoris Popov { 421681a5bbeSBoris Popov struct smb_rq *rqp; 422681a5bbeSBoris Popov struct mbchain *mbp; 423681a5bbeSBoris Popov int error; 424681a5bbeSBoris Popov 425681a5bbeSBoris Popov if (vcp->vc_smbuid == SMB_UID_UNKNOWN) 426681a5bbeSBoris Popov return 0; 427681a5bbeSBoris Popov 4286e551fb6SDavid E. O'Brien if (smb_smb_nomux(vcp, scred, __func__) != 0) 429681a5bbeSBoris Popov return EINVAL; 430681a5bbeSBoris Popov 431681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_LOGOFF_ANDX, scred, &rqp); 432681a5bbeSBoris Popov if (error) 433681a5bbeSBoris Popov return error; 434681a5bbeSBoris Popov mbp = &rqp->sr_rq; 435681a5bbeSBoris Popov smb_rq_wstart(rqp); 436681a5bbeSBoris Popov mb_put_uint8(mbp, 0xff); 437681a5bbeSBoris Popov mb_put_uint8(mbp, 0); 438681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); 439681a5bbeSBoris Popov smb_rq_wend(rqp); 440681a5bbeSBoris Popov smb_rq_bstart(rqp); 441681a5bbeSBoris Popov smb_rq_bend(rqp); 442681a5bbeSBoris Popov error = smb_rq_simple(rqp); 443681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 444681a5bbeSBoris Popov smb_rq_done(rqp); 445681a5bbeSBoris Popov return error; 446681a5bbeSBoris Popov } 447681a5bbeSBoris Popov 448681a5bbeSBoris Popov static char smb_any_share[] = "?????"; 449681a5bbeSBoris Popov 450681a5bbeSBoris Popov static char * 451681a5bbeSBoris Popov smb_share_typename(int stype) 452681a5bbeSBoris Popov { 453681a5bbeSBoris Popov char *pp; 454681a5bbeSBoris Popov 455681a5bbeSBoris Popov switch (stype) { 456681a5bbeSBoris Popov case SMB_ST_DISK: 457681a5bbeSBoris Popov pp = "A:"; 458681a5bbeSBoris Popov break; 459681a5bbeSBoris Popov case SMB_ST_PRINTER: 460681a5bbeSBoris Popov pp = smb_any_share; /* can't use LPT: here... */ 461681a5bbeSBoris Popov break; 462681a5bbeSBoris Popov case SMB_ST_PIPE: 463681a5bbeSBoris Popov pp = "IPC"; 464681a5bbeSBoris Popov break; 465681a5bbeSBoris Popov case SMB_ST_COMM: 466681a5bbeSBoris Popov pp = "COMM"; 467681a5bbeSBoris Popov break; 468681a5bbeSBoris Popov case SMB_ST_ANY: 469681a5bbeSBoris Popov default: 470681a5bbeSBoris Popov pp = smb_any_share; 471681a5bbeSBoris Popov break; 472681a5bbeSBoris Popov } 473681a5bbeSBoris Popov return pp; 474681a5bbeSBoris Popov } 475681a5bbeSBoris Popov 476681a5bbeSBoris Popov int 477681a5bbeSBoris Popov smb_smb_treeconnect(struct smb_share *ssp, struct smb_cred *scred) 478681a5bbeSBoris Popov { 479681a5bbeSBoris Popov struct smb_vc *vcp; 480681a5bbeSBoris Popov struct smb_rq rq, *rqp = &rq; 481681a5bbeSBoris Popov struct mbchain *mbp; 482681a5bbeSBoris Popov char *pp, *pbuf, *encpass; 48370f9b9d9SBoris Popov int error, plen, caseopt, upper; 48470f9b9d9SBoris Popov 48570f9b9d9SBoris Popov upper = 0; 48670f9b9d9SBoris Popov 48770f9b9d9SBoris Popov again: 48870f9b9d9SBoris Popov 48970f9b9d9SBoris Popov #if 0 49070f9b9d9SBoris Popov /* Disable Unicode for SMB_COM_TREE_CONNECT_ANDX requests */ 49170f9b9d9SBoris Popov if (SSTOVC(ssp)->vc_hflags2 & SMB_FLAGS2_UNICODE) { 49270f9b9d9SBoris Popov vcp = SSTOVC(ssp); 49370f9b9d9SBoris Popov if (vcp->vc_toserver) { 49470f9b9d9SBoris Popov iconv_close(vcp->vc_toserver); 49570f9b9d9SBoris Popov /* Use NULL until UTF-8 -> ASCII works */ 49670f9b9d9SBoris Popov vcp->vc_toserver = NULL; 49770f9b9d9SBoris Popov } 49870f9b9d9SBoris Popov if (vcp->vc_tolocal) { 49970f9b9d9SBoris Popov iconv_close(vcp->vc_tolocal); 50070f9b9d9SBoris Popov /* Use NULL until ASCII -> UTF-8 works*/ 50170f9b9d9SBoris Popov vcp->vc_tolocal = NULL; 50270f9b9d9SBoris Popov } 50370f9b9d9SBoris Popov vcp->vc_hflags2 &= ~SMB_FLAGS2_UNICODE; 50470f9b9d9SBoris Popov } 50570f9b9d9SBoris Popov #endif 506681a5bbeSBoris Popov 507681a5bbeSBoris Popov ssp->ss_tid = SMB_TID_UNKNOWN; 508681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_CONNECT_ANDX, scred, &rqp); 509681a5bbeSBoris Popov if (error) 510681a5bbeSBoris Popov return error; 511681a5bbeSBoris Popov vcp = rqp->sr_vc; 512681a5bbeSBoris Popov caseopt = SMB_CS_NONE; 513681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_USER) { 514681a5bbeSBoris Popov plen = 1; 515681a5bbeSBoris Popov pp = ""; 516681a5bbeSBoris Popov pbuf = NULL; 517681a5bbeSBoris Popov encpass = NULL; 518681a5bbeSBoris Popov } else { 519a163d034SWarner Losh pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK); 520a163d034SWarner Losh encpass = malloc(24, M_SMBTEMP, M_WAITOK); 52170f9b9d9SBoris Popov /* 52270f9b9d9SBoris Popov * We try w/o uppercasing first so Samba mixed case 52370f9b9d9SBoris Popov * passwords work. If that fails we come back and try 52470f9b9d9SBoris Popov * uppercasing to satisfy OS/2 and Windows for Workgroups. 52570f9b9d9SBoris Popov */ 52670f9b9d9SBoris Popov if (upper++) { 52770f9b9d9SBoris Popov iconv_convstr(vcp->vc_toupper, pbuf, 52870f9b9d9SBoris Popov smb_share_getpass(ssp)/*, 52970f9b9d9SBoris Popov SMB_MAXPASSWORDLEN*/); 53070f9b9d9SBoris Popov } else { 53170f9b9d9SBoris Popov strncpy(pbuf, smb_share_getpass(ssp), 53270f9b9d9SBoris Popov SMB_MAXPASSWORDLEN); 53370f9b9d9SBoris Popov pbuf[SMB_MAXPASSWORDLEN] = '\0'; 53470f9b9d9SBoris Popov } 535681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) { 536681a5bbeSBoris Popov plen = 24; 537681a5bbeSBoris Popov smb_encrypt(pbuf, vcp->vc_ch, encpass); 538681a5bbeSBoris Popov pp = encpass; 539681a5bbeSBoris Popov } else { 540681a5bbeSBoris Popov plen = strlen(pbuf) + 1; 541681a5bbeSBoris Popov pp = pbuf; 542681a5bbeSBoris Popov } 543681a5bbeSBoris Popov } 544681a5bbeSBoris Popov mbp = &rqp->sr_rq; 545681a5bbeSBoris Popov smb_rq_wstart(rqp); 546681a5bbeSBoris Popov mb_put_uint8(mbp, 0xff); 547681a5bbeSBoris Popov mb_put_uint8(mbp, 0); 548681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); 549681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); /* Flags */ 550681a5bbeSBoris Popov mb_put_uint16le(mbp, plen); 551681a5bbeSBoris Popov smb_rq_wend(rqp); 552681a5bbeSBoris Popov smb_rq_bstart(rqp); 553681a5bbeSBoris Popov mb_put_mem(mbp, pp, plen, MB_MSYSTEM); 554681a5bbeSBoris Popov smb_put_dmem(mbp, vcp, "\\\\", 2, caseopt); 555681a5bbeSBoris Popov pp = vcp->vc_srvname; 556681a5bbeSBoris Popov smb_put_dmem(mbp, vcp, pp, strlen(pp), caseopt); 557681a5bbeSBoris Popov smb_put_dmem(mbp, vcp, "\\", 1, caseopt); 558681a5bbeSBoris Popov pp = ssp->ss_name; 559681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, pp, caseopt); 560681a5bbeSBoris Popov pp = smb_share_typename(ssp->ss_type); 561681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, pp, caseopt); 562681a5bbeSBoris Popov smb_rq_bend(rqp); 563681a5bbeSBoris Popov error = smb_rq_simple(rqp); 564681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 565681a5bbeSBoris Popov if (error) 566681a5bbeSBoris Popov goto bad; 567681a5bbeSBoris Popov ssp->ss_tid = rqp->sr_rptid; 568681a5bbeSBoris Popov ssp->ss_vcgenid = vcp->vc_genid; 569681a5bbeSBoris Popov ssp->ss_flags |= SMBS_CONNECTED; 570681a5bbeSBoris Popov bad: 571681a5bbeSBoris Popov if (encpass) 572681a5bbeSBoris Popov free(encpass, M_SMBTEMP); 573681a5bbeSBoris Popov if (pbuf) 574681a5bbeSBoris Popov free(pbuf, M_SMBTEMP); 575681a5bbeSBoris Popov smb_rq_done(rqp); 57670f9b9d9SBoris Popov if (error && upper == 1) 57770f9b9d9SBoris Popov goto again; 578681a5bbeSBoris Popov return error; 579681a5bbeSBoris Popov } 580681a5bbeSBoris Popov 581681a5bbeSBoris Popov int 582681a5bbeSBoris Popov smb_smb_treedisconnect(struct smb_share *ssp, struct smb_cred *scred) 583681a5bbeSBoris Popov { 584681a5bbeSBoris Popov struct smb_rq *rqp; 585681a5bbeSBoris Popov struct mbchain *mbp; 586681a5bbeSBoris Popov int error; 587681a5bbeSBoris Popov 588681a5bbeSBoris Popov if (ssp->ss_tid == SMB_TID_UNKNOWN) 589681a5bbeSBoris Popov return 0; 590681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_DISCONNECT, scred, &rqp); 591681a5bbeSBoris Popov if (error) 592681a5bbeSBoris Popov return error; 593681a5bbeSBoris Popov mbp = &rqp->sr_rq; 594681a5bbeSBoris Popov smb_rq_wstart(rqp); 595681a5bbeSBoris Popov smb_rq_wend(rqp); 596681a5bbeSBoris Popov smb_rq_bstart(rqp); 597681a5bbeSBoris Popov smb_rq_bend(rqp); 598681a5bbeSBoris Popov error = smb_rq_simple(rqp); 599681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 600681a5bbeSBoris Popov smb_rq_done(rqp); 601681a5bbeSBoris Popov ssp->ss_tid = SMB_TID_UNKNOWN; 602681a5bbeSBoris Popov return error; 603681a5bbeSBoris Popov } 604681a5bbeSBoris Popov 605681a5bbeSBoris Popov static __inline int 606e2a70cffSBoris Popov smb_smb_readx(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid, 607e2a70cffSBoris Popov struct uio *uio, struct smb_cred *scred) 608e2a70cffSBoris Popov { 609e2a70cffSBoris Popov struct smb_rq *rqp; 610e2a70cffSBoris Popov struct mbchain *mbp; 611e2a70cffSBoris Popov struct mdchain *mdp; 612e2a70cffSBoris Popov u_int8_t wc; 613e2a70cffSBoris Popov int error; 614e2a70cffSBoris Popov u_int16_t residhi, residlo, off, doff; 615e2a70cffSBoris Popov u_int32_t resid; 616e2a70cffSBoris Popov 617e2a70cffSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ_ANDX, scred, &rqp); 618e2a70cffSBoris Popov if (error) 619e2a70cffSBoris Popov return error; 620e2a70cffSBoris Popov smb_rq_getrequest(rqp, &mbp); 621e2a70cffSBoris Popov smb_rq_wstart(rqp); 622e2a70cffSBoris Popov mb_put_uint8(mbp, 0xff); /* no secondary command */ 623e2a70cffSBoris Popov mb_put_uint8(mbp, 0); /* MBZ */ 624e2a70cffSBoris Popov mb_put_uint16le(mbp, 0); /* offset to secondary */ 625e2a70cffSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM); 626e2a70cffSBoris Popov mb_put_uint32le(mbp, uio->uio_offset); 627e2a70cffSBoris Popov *len = min(SSTOVC(ssp)->vc_rxmax, *len); 628e2a70cffSBoris Popov mb_put_uint16le(mbp, *len); /* MaxCount */ 629e2a70cffSBoris Popov mb_put_uint16le(mbp, *len); /* MinCount (only indicates blocking) */ 630e2a70cffSBoris Popov mb_put_uint32le(mbp, (unsigned)*len >> 16); /* MaxCountHigh */ 631e2a70cffSBoris Popov mb_put_uint16le(mbp, *len); /* Remaining ("obsolete") */ 632e2a70cffSBoris Popov mb_put_uint32le(mbp, uio->uio_offset >> 32); /* OffsetHigh */ 633e2a70cffSBoris Popov smb_rq_wend(rqp); 634e2a70cffSBoris Popov smb_rq_bstart(rqp); 635e2a70cffSBoris Popov smb_rq_bend(rqp); 636e2a70cffSBoris Popov do { 637e2a70cffSBoris Popov error = smb_rq_simple(rqp); 638e2a70cffSBoris Popov if (error) 639e2a70cffSBoris Popov break; 640e2a70cffSBoris Popov smb_rq_getreply(rqp, &mdp); 641e2a70cffSBoris Popov off = SMB_HDRLEN; 642e2a70cffSBoris Popov md_get_uint8(mdp, &wc); 643e2a70cffSBoris Popov off++; 644e2a70cffSBoris Popov if (wc != 12) { 645e2a70cffSBoris Popov error = EBADRPC; 646e2a70cffSBoris Popov break; 647e2a70cffSBoris Popov } 648e2a70cffSBoris Popov md_get_uint8(mdp, NULL); 649e2a70cffSBoris Popov off++; 650e2a70cffSBoris Popov md_get_uint8(mdp, NULL); 651e2a70cffSBoris Popov off++; 652e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); 653e2a70cffSBoris Popov off += 2; 654e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); 655e2a70cffSBoris Popov off += 2; 656e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); /* data compaction mode */ 657e2a70cffSBoris Popov off += 2; 658e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); 659e2a70cffSBoris Popov off += 2; 660e2a70cffSBoris Popov md_get_uint16le(mdp, &residlo); 661e2a70cffSBoris Popov off += 2; 662e2a70cffSBoris Popov md_get_uint16le(mdp, &doff); /* data offset */ 663e2a70cffSBoris Popov off += 2; 664e2a70cffSBoris Popov md_get_uint16le(mdp, &residhi); 665e2a70cffSBoris Popov off += 2; 666e2a70cffSBoris Popov resid = (residhi << 16) | residlo; 667e2a70cffSBoris Popov md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM); 668e2a70cffSBoris Popov off += 4*2; 669e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); /* ByteCount */ 670e2a70cffSBoris Popov off += 2; 671e2a70cffSBoris Popov if (doff > off) /* pad byte(s)? */ 672e2a70cffSBoris Popov md_get_mem(mdp, NULL, doff - off, MB_MSYSTEM); 673e2a70cffSBoris Popov if (resid == 0) { 674e2a70cffSBoris Popov *rresid = resid; 675e2a70cffSBoris Popov break; 676e2a70cffSBoris Popov } 677e2a70cffSBoris Popov error = md_get_uio(mdp, uio, resid); 678e2a70cffSBoris Popov if (error) 679e2a70cffSBoris Popov break; 680e2a70cffSBoris Popov *rresid = resid; 681e2a70cffSBoris Popov } while(0); 682e2a70cffSBoris Popov smb_rq_done(rqp); 683e2a70cffSBoris Popov return (error); 684e2a70cffSBoris Popov } 685e2a70cffSBoris Popov 686e2a70cffSBoris Popov static __inline int 687e2a70cffSBoris Popov smb_smb_writex(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid, 688e2a70cffSBoris Popov struct uio *uio, struct smb_cred *scred) 689e2a70cffSBoris Popov { 690e2a70cffSBoris Popov struct smb_rq *rqp; 691e2a70cffSBoris Popov struct mbchain *mbp; 692e2a70cffSBoris Popov struct mdchain *mdp; 693e2a70cffSBoris Popov int error; 694e2a70cffSBoris Popov u_int8_t wc; 695e2a70cffSBoris Popov u_int16_t resid; 696e2a70cffSBoris Popov 697e2a70cffSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE_ANDX, scred, &rqp); 698e2a70cffSBoris Popov if (error) 699e2a70cffSBoris Popov return (error); 700e2a70cffSBoris Popov smb_rq_getrequest(rqp, &mbp); 701e2a70cffSBoris Popov smb_rq_wstart(rqp); 702e2a70cffSBoris Popov mb_put_uint8(mbp, 0xff); /* no secondary command */ 703e2a70cffSBoris Popov mb_put_uint8(mbp, 0); /* MBZ */ 704e2a70cffSBoris Popov mb_put_uint16le(mbp, 0); /* offset to secondary */ 705e2a70cffSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM); 706e2a70cffSBoris Popov mb_put_uint32le(mbp, uio->uio_offset); 707e2a70cffSBoris Popov mb_put_uint32le(mbp, 0); /* MBZ (timeout) */ 708e2a70cffSBoris Popov mb_put_uint16le(mbp, 0); /* !write-thru */ 709e2a70cffSBoris Popov mb_put_uint16le(mbp, 0); 710e2a70cffSBoris Popov *len = min(SSTOVC(ssp)->vc_wxmax, *len); 711e2a70cffSBoris Popov mb_put_uint16le(mbp, (unsigned)*len >> 16); 712e2a70cffSBoris Popov mb_put_uint16le(mbp, *len); 713e2a70cffSBoris Popov mb_put_uint16le(mbp, 64); /* data offset from header start */ 714e2a70cffSBoris Popov mb_put_uint32le(mbp, uio->uio_offset >> 32); /* OffsetHigh */ 715e2a70cffSBoris Popov smb_rq_wend(rqp); 716e2a70cffSBoris Popov smb_rq_bstart(rqp); 717e2a70cffSBoris Popov do { 718e2a70cffSBoris Popov mb_put_uint8(mbp, 0xee); /* mimic xp pad byte! */ 719e2a70cffSBoris Popov error = mb_put_uio(mbp, uio, *len); 720e2a70cffSBoris Popov if (error) 721e2a70cffSBoris Popov break; 722e2a70cffSBoris Popov smb_rq_bend(rqp); 723e2a70cffSBoris Popov error = smb_rq_simple(rqp); 724e2a70cffSBoris Popov if (error) 725e2a70cffSBoris Popov break; 726e2a70cffSBoris Popov smb_rq_getreply(rqp, &mdp); 727e2a70cffSBoris Popov md_get_uint8(mdp, &wc); 728e2a70cffSBoris Popov if (wc != 6) { 729e2a70cffSBoris Popov error = EBADRPC; 730e2a70cffSBoris Popov break; 731e2a70cffSBoris Popov } 732e2a70cffSBoris Popov md_get_uint8(mdp, NULL); 733e2a70cffSBoris Popov md_get_uint8(mdp, NULL); 734e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); 735e2a70cffSBoris Popov md_get_uint16le(mdp, &resid); 736e2a70cffSBoris Popov *rresid = resid; 737e2a70cffSBoris Popov } while(0); 738e2a70cffSBoris Popov 739e2a70cffSBoris Popov smb_rq_done(rqp); 740e2a70cffSBoris Popov return (error); 741e2a70cffSBoris Popov } 742e2a70cffSBoris Popov 743e2a70cffSBoris Popov static __inline int 744681a5bbeSBoris Popov smb_smb_read(struct smb_share *ssp, u_int16_t fid, 745681a5bbeSBoris Popov int *len, int *rresid, struct uio *uio, struct smb_cred *scred) 746681a5bbeSBoris Popov { 747681a5bbeSBoris Popov struct smb_rq *rqp; 748681a5bbeSBoris Popov struct mbchain *mbp; 749681a5bbeSBoris Popov struct mdchain *mdp; 750681a5bbeSBoris Popov u_int16_t resid, bc; 751681a5bbeSBoris Popov u_int8_t wc; 752681a5bbeSBoris Popov int error, rlen, blksz; 753681a5bbeSBoris Popov 754e2a70cffSBoris Popov if (SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_READX) 755e2a70cffSBoris Popov return (smb_smb_readx(ssp, fid, len, rresid, uio, scred)); 756e2a70cffSBoris Popov 757681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ, scred, &rqp); 758681a5bbeSBoris Popov if (error) 759681a5bbeSBoris Popov return error; 760681a5bbeSBoris Popov 761681a5bbeSBoris Popov blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16; 762681a5bbeSBoris Popov rlen = *len = min(blksz, *len); 763681a5bbeSBoris Popov 764681a5bbeSBoris Popov smb_rq_getrequest(rqp, &mbp); 765681a5bbeSBoris Popov smb_rq_wstart(rqp); 766681a5bbeSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM); 767681a5bbeSBoris Popov mb_put_uint16le(mbp, rlen); 768681a5bbeSBoris Popov mb_put_uint32le(mbp, uio->uio_offset); 769681a5bbeSBoris Popov mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff)); 770681a5bbeSBoris Popov smb_rq_wend(rqp); 771681a5bbeSBoris Popov smb_rq_bstart(rqp); 772681a5bbeSBoris Popov smb_rq_bend(rqp); 773681a5bbeSBoris Popov do { 774681a5bbeSBoris Popov error = smb_rq_simple(rqp); 775681a5bbeSBoris Popov if (error) 776681a5bbeSBoris Popov break; 777681a5bbeSBoris Popov smb_rq_getreply(rqp, &mdp); 778681a5bbeSBoris Popov md_get_uint8(mdp, &wc); 779681a5bbeSBoris Popov if (wc != 5) { 780681a5bbeSBoris Popov error = EBADRPC; 781681a5bbeSBoris Popov break; 782681a5bbeSBoris Popov } 783681a5bbeSBoris Popov md_get_uint16le(mdp, &resid); 784681a5bbeSBoris Popov md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM); 785681a5bbeSBoris Popov md_get_uint16le(mdp, &bc); 786681a5bbeSBoris Popov md_get_uint8(mdp, NULL); /* ignore buffer type */ 787681a5bbeSBoris Popov md_get_uint16le(mdp, &resid); 788681a5bbeSBoris Popov if (resid == 0) { 789681a5bbeSBoris Popov *rresid = resid; 790681a5bbeSBoris Popov break; 791681a5bbeSBoris Popov } 792681a5bbeSBoris Popov error = md_get_uio(mdp, uio, resid); 793681a5bbeSBoris Popov if (error) 794681a5bbeSBoris Popov break; 795681a5bbeSBoris Popov *rresid = resid; 796681a5bbeSBoris Popov } while(0); 797681a5bbeSBoris Popov smb_rq_done(rqp); 798681a5bbeSBoris Popov return error; 799681a5bbeSBoris Popov } 800681a5bbeSBoris Popov 801681a5bbeSBoris Popov int 802681a5bbeSBoris Popov smb_read(struct smb_share *ssp, u_int16_t fid, struct uio *uio, 803681a5bbeSBoris Popov struct smb_cred *scred) 804681a5bbeSBoris Popov { 805681a5bbeSBoris Popov int tsize, len, resid; 806681a5bbeSBoris Popov int error = 0; 807681a5bbeSBoris Popov 808681a5bbeSBoris Popov tsize = uio->uio_resid; 809681a5bbeSBoris Popov while (tsize > 0) { 810681a5bbeSBoris Popov len = tsize; 811681a5bbeSBoris Popov error = smb_smb_read(ssp, fid, &len, &resid, uio, scred); 812681a5bbeSBoris Popov if (error) 813681a5bbeSBoris Popov break; 814681a5bbeSBoris Popov tsize -= resid; 815681a5bbeSBoris Popov if (resid < len) 816681a5bbeSBoris Popov break; 817681a5bbeSBoris Popov } 818681a5bbeSBoris Popov return error; 819681a5bbeSBoris Popov } 820681a5bbeSBoris Popov 821681a5bbeSBoris Popov static __inline int 822681a5bbeSBoris Popov smb_smb_write(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid, 823681a5bbeSBoris Popov struct uio *uio, struct smb_cred *scred) 824681a5bbeSBoris Popov { 825681a5bbeSBoris Popov struct smb_rq *rqp; 826681a5bbeSBoris Popov struct mbchain *mbp; 827681a5bbeSBoris Popov struct mdchain *mdp; 828681a5bbeSBoris Popov u_int16_t resid; 829681a5bbeSBoris Popov u_int8_t wc; 830681a5bbeSBoris Popov int error, blksz; 831681a5bbeSBoris Popov 832e2a70cffSBoris Popov if (*len && SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX) 833e2a70cffSBoris Popov return (smb_smb_writex(ssp, fid, len, rresid, uio, scred)); 834e2a70cffSBoris Popov 835681a5bbeSBoris Popov blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16; 836681a5bbeSBoris Popov if (blksz > 0xffff) 837681a5bbeSBoris Popov blksz = 0xffff; 838681a5bbeSBoris Popov 839681a5bbeSBoris Popov resid = *len = min(blksz, *len); 840681a5bbeSBoris Popov 841681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE, scred, &rqp); 842681a5bbeSBoris Popov if (error) 843681a5bbeSBoris Popov return error; 844681a5bbeSBoris Popov smb_rq_getrequest(rqp, &mbp); 845681a5bbeSBoris Popov smb_rq_wstart(rqp); 846681a5bbeSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM); 847681a5bbeSBoris Popov mb_put_uint16le(mbp, resid); 848681a5bbeSBoris Popov mb_put_uint32le(mbp, uio->uio_offset); 849681a5bbeSBoris Popov mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff)); 850681a5bbeSBoris Popov smb_rq_wend(rqp); 851681a5bbeSBoris Popov smb_rq_bstart(rqp); 852681a5bbeSBoris Popov mb_put_uint8(mbp, SMB_DT_DATA); 853681a5bbeSBoris Popov mb_put_uint16le(mbp, resid); 854681a5bbeSBoris Popov do { 855681a5bbeSBoris Popov error = mb_put_uio(mbp, uio, resid); 856681a5bbeSBoris Popov if (error) 857681a5bbeSBoris Popov break; 858681a5bbeSBoris Popov smb_rq_bend(rqp); 859681a5bbeSBoris Popov error = smb_rq_simple(rqp); 860681a5bbeSBoris Popov if (error) 861681a5bbeSBoris Popov break; 862681a5bbeSBoris Popov smb_rq_getreply(rqp, &mdp); 863681a5bbeSBoris Popov md_get_uint8(mdp, &wc); 864681a5bbeSBoris Popov if (wc != 1) { 865681a5bbeSBoris Popov error = EBADRPC; 866681a5bbeSBoris Popov break; 867681a5bbeSBoris Popov } 868681a5bbeSBoris Popov md_get_uint16le(mdp, &resid); 869681a5bbeSBoris Popov *rresid = resid; 870681a5bbeSBoris Popov } while(0); 871681a5bbeSBoris Popov smb_rq_done(rqp); 872681a5bbeSBoris Popov return error; 873681a5bbeSBoris Popov } 874681a5bbeSBoris Popov 875681a5bbeSBoris Popov int 876681a5bbeSBoris Popov smb_write(struct smb_share *ssp, u_int16_t fid, struct uio *uio, 877681a5bbeSBoris Popov struct smb_cred *scred) 878681a5bbeSBoris Popov { 879681a5bbeSBoris Popov int error = 0, len, tsize, resid; 880681a5bbeSBoris Popov struct uio olduio; 881681a5bbeSBoris Popov 882681a5bbeSBoris Popov tsize = uio->uio_resid; 883681a5bbeSBoris Popov olduio = *uio; 884681a5bbeSBoris Popov while (tsize > 0) { 885681a5bbeSBoris Popov len = tsize; 886681a5bbeSBoris Popov error = smb_smb_write(ssp, fid, &len, &resid, uio, scred); 887681a5bbeSBoris Popov if (error) 888681a5bbeSBoris Popov break; 889681a5bbeSBoris Popov if (resid < len) { 890681a5bbeSBoris Popov error = EIO; 891681a5bbeSBoris Popov break; 892681a5bbeSBoris Popov } 893681a5bbeSBoris Popov tsize -= resid; 894681a5bbeSBoris Popov } 895681a5bbeSBoris Popov if (error) { 8966cd9842fSBoris Popov /* 8976cd9842fSBoris Popov * Errors can happen on the copyin, the rpc, etc. So they 8986cd9842fSBoris Popov * imply resid is unreliable. The only safe thing is 8996cd9842fSBoris Popov * to pretend zero bytes made it. We needn't restore the 9006cd9842fSBoris Popov * iovs because callers don't depend on them in error 9016cd9842fSBoris Popov * paths - uio_resid and uio_offset are what matter. 9026cd9842fSBoris Popov */ 903681a5bbeSBoris Popov *uio = olduio; 904681a5bbeSBoris Popov } 905681a5bbeSBoris Popov return error; 906681a5bbeSBoris Popov } 907681a5bbeSBoris Popov 908681a5bbeSBoris Popov int 909681a5bbeSBoris Popov smb_smb_echo(struct smb_vc *vcp, struct smb_cred *scred) 910681a5bbeSBoris Popov { 911681a5bbeSBoris Popov struct smb_rq *rqp; 912681a5bbeSBoris Popov struct mbchain *mbp; 913681a5bbeSBoris Popov int error; 914681a5bbeSBoris Popov 915681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_ECHO, scred, &rqp); 916681a5bbeSBoris Popov if (error) 917681a5bbeSBoris Popov return error; 918681a5bbeSBoris Popov mbp = &rqp->sr_rq; 919681a5bbeSBoris Popov smb_rq_wstart(rqp); 920681a5bbeSBoris Popov mb_put_uint16le(mbp, 1); 921681a5bbeSBoris Popov smb_rq_wend(rqp); 922681a5bbeSBoris Popov smb_rq_bstart(rqp); 923681a5bbeSBoris Popov mb_put_uint32le(mbp, 0); 924681a5bbeSBoris Popov smb_rq_bend(rqp); 925681a5bbeSBoris Popov error = smb_rq_simple(rqp); 926681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 927681a5bbeSBoris Popov smb_rq_done(rqp); 928681a5bbeSBoris Popov return error; 929681a5bbeSBoris Popov } 930