1681a5bbeSBoris Popov /* 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 * 32681a5bbeSBoris Popov * $FreeBSD$ 33681a5bbeSBoris Popov */ 34681a5bbeSBoris Popov /* 35681a5bbeSBoris Popov * various SMB requests. Most of the routines merely packs data into mbufs. 36681a5bbeSBoris Popov */ 37681a5bbeSBoris Popov #include <sys/param.h> 38681a5bbeSBoris Popov #include <sys/systm.h> 39681a5bbeSBoris Popov #include <sys/kernel.h> 40681a5bbeSBoris Popov #include <sys/malloc.h> 41681a5bbeSBoris Popov #include <sys/proc.h> 42681a5bbeSBoris Popov #include <sys/lock.h> 43681a5bbeSBoris Popov #include <sys/sysctl.h> 44681a5bbeSBoris Popov #include <sys/socket.h> 45681a5bbeSBoris Popov #include <sys/uio.h> 46681a5bbeSBoris Popov 47681a5bbeSBoris Popov #include <sys/iconv.h> 48681a5bbeSBoris Popov 49681a5bbeSBoris Popov #include <netsmb/smb.h> 50681a5bbeSBoris Popov #include <netsmb/smb_subr.h> 51681a5bbeSBoris Popov #include <netsmb/smb_rq.h> 52681a5bbeSBoris Popov #include <netsmb/smb_conn.h> 53681a5bbeSBoris Popov #include <netsmb/smb_tran.h> 54681a5bbeSBoris Popov 55681a5bbeSBoris Popov struct smb_dialect { 56681a5bbeSBoris Popov int d_id; 57681a5bbeSBoris Popov const char * d_name; 58681a5bbeSBoris Popov }; 59681a5bbeSBoris Popov 60681a5bbeSBoris Popov static struct smb_dialect smb_dialects[] = { 61681a5bbeSBoris Popov {SMB_DIALECT_CORE, "PC NETWORK PROGRAM 1.0"}, 62681a5bbeSBoris Popov {SMB_DIALECT_COREPLUS, "MICROSOFT NETWORKS 1.03"}, 63681a5bbeSBoris Popov {SMB_DIALECT_LANMAN1_0, "MICROSOFT NETWORKS 3.0"}, 64681a5bbeSBoris Popov {SMB_DIALECT_LANMAN1_0, "LANMAN1.0"}, 65681a5bbeSBoris Popov {SMB_DIALECT_LANMAN2_0, "LM1.2X002"}, 66681a5bbeSBoris Popov {SMB_DIALECT_LANMAN2_0, "Samba"}, 67681a5bbeSBoris Popov {SMB_DIALECT_NTLM0_12, "NT LANMAN 1.0"}, 68681a5bbeSBoris Popov {SMB_DIALECT_NTLM0_12, "NT LM 0.12"}, 69681a5bbeSBoris Popov {-1, NULL} 70681a5bbeSBoris Popov }; 71681a5bbeSBoris Popov 72681a5bbeSBoris Popov #define SMB_DIALECT_MAX (sizeof(smb_dialects) / sizeof(struct smb_dialect) - 2) 73681a5bbeSBoris Popov 74e2a70cffSBoris Popov static u_int32_t 75e2a70cffSBoris Popov smb_vc_maxread(struct smb_vc *vcp) 76e2a70cffSBoris Popov { 77e2a70cffSBoris Popov /* 78e2a70cffSBoris Popov * Specs say up to 64k data bytes, but Windows traffic 79e2a70cffSBoris Popov * uses 60k... no doubt for some good reason. 80e2a70cffSBoris Popov */ 81e2a70cffSBoris Popov if (vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_READX) 82e2a70cffSBoris Popov return (60*1024); 83e2a70cffSBoris Popov else 84e2a70cffSBoris Popov return (vcp->vc_sopt.sv_maxtx); 85e2a70cffSBoris Popov } 86e2a70cffSBoris Popov 87e2a70cffSBoris Popov static u_int32_t 88e2a70cffSBoris Popov smb_vc_maxwrite(struct smb_vc *vcp) 89e2a70cffSBoris Popov { 90e2a70cffSBoris Popov /* 91e2a70cffSBoris Popov * Specs say up to 64k data bytes, but Windows traffic 92e2a70cffSBoris Popov * uses 60k... probably for some good reason. 93e2a70cffSBoris Popov */ 94e2a70cffSBoris Popov if (vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX) 95e2a70cffSBoris Popov return (60*1024); 96e2a70cffSBoris Popov else 97e2a70cffSBoris Popov return (vcp->vc_sopt.sv_maxtx); 98e2a70cffSBoris Popov } 99e2a70cffSBoris Popov 100681a5bbeSBoris Popov static int 101681a5bbeSBoris Popov smb_smb_nomux(struct smb_vc *vcp, struct smb_cred *scred, const char *name) 102681a5bbeSBoris Popov { 103fce6fbfaSBoris Popov if (scred->scr_td->td_proc == vcp->vc_iod->iod_p) 104681a5bbeSBoris Popov return 0; 105681a5bbeSBoris Popov SMBERROR("wrong function called(%s)\n", name); 106681a5bbeSBoris Popov return EINVAL; 107681a5bbeSBoris Popov } 108681a5bbeSBoris Popov 109681a5bbeSBoris Popov int 110681a5bbeSBoris Popov smb_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred) 111681a5bbeSBoris Popov { 112681a5bbeSBoris Popov struct smb_dialect *dp; 113681a5bbeSBoris Popov struct smb_sopt *sp = NULL; 114681a5bbeSBoris Popov struct smb_rq *rqp; 115681a5bbeSBoris Popov struct mbchain *mbp; 116681a5bbeSBoris Popov struct mdchain *mdp; 117681a5bbeSBoris Popov u_int8_t wc, stime[8], sblen; 118681a5bbeSBoris Popov u_int16_t dindex, tw, tw1, swlen, bc; 119681a5bbeSBoris Popov int error, maxqsz; 120681a5bbeSBoris Popov 1216e551fb6SDavid E. O'Brien if (smb_smb_nomux(vcp, scred, __func__) != 0) 122681a5bbeSBoris Popov return EINVAL; 123681a5bbeSBoris Popov vcp->vc_hflags = 0; 124681a5bbeSBoris Popov vcp->vc_hflags2 = 0; 125681a5bbeSBoris Popov vcp->obj.co_flags &= ~(SMBV_ENCRYPT); 126681a5bbeSBoris Popov sp = &vcp->vc_sopt; 127681a5bbeSBoris Popov bzero(sp, sizeof(struct smb_sopt)); 128681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_NEGOTIATE, scred, &rqp); 129681a5bbeSBoris Popov if (error) 130681a5bbeSBoris Popov return error; 131681a5bbeSBoris Popov smb_rq_getrequest(rqp, &mbp); 132681a5bbeSBoris Popov smb_rq_wstart(rqp); 133681a5bbeSBoris Popov smb_rq_wend(rqp); 134681a5bbeSBoris Popov smb_rq_bstart(rqp); 135681a5bbeSBoris Popov for(dp = smb_dialects; dp->d_id != -1; dp++) { 136681a5bbeSBoris Popov mb_put_uint8(mbp, SMB_DT_DIALECT); 137681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, dp->d_name, SMB_CS_NONE); 138681a5bbeSBoris Popov } 139681a5bbeSBoris Popov smb_rq_bend(rqp); 140681a5bbeSBoris Popov error = smb_rq_simple(rqp); 141681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 142681a5bbeSBoris Popov if (error) 143681a5bbeSBoris Popov goto bad; 144681a5bbeSBoris Popov smb_rq_getreply(rqp, &mdp); 145681a5bbeSBoris Popov do { 146681a5bbeSBoris Popov error = md_get_uint8(mdp, &wc); 147681a5bbeSBoris Popov if (error) 148681a5bbeSBoris Popov break; 149681a5bbeSBoris Popov error = md_get_uint16le(mdp, &dindex); 150681a5bbeSBoris Popov if (error) 151681a5bbeSBoris Popov break; 152681a5bbeSBoris Popov if (dindex > 7) { 153681a5bbeSBoris Popov SMBERROR("Don't know how to talk with server %s (%d)\n", "xxx", dindex); 154681a5bbeSBoris Popov error = EBADRPC; 155681a5bbeSBoris Popov break; 156681a5bbeSBoris Popov } 157681a5bbeSBoris Popov dp = smb_dialects + dindex; 158681a5bbeSBoris Popov sp->sv_proto = dp->d_id; 159681a5bbeSBoris Popov SMBSDEBUG("Dialect %s (%d, %d)\n", dp->d_name, dindex, wc); 160681a5bbeSBoris Popov error = EBADRPC; 161681a5bbeSBoris Popov if (dp->d_id >= SMB_DIALECT_NTLM0_12) { 162681a5bbeSBoris Popov if (wc != 17) 163681a5bbeSBoris Popov break; 164681a5bbeSBoris Popov md_get_uint8(mdp, &sp->sv_sm); 165681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxmux); 166681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxvcs); 167681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_maxtx); 168681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_maxraw); 169681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_skey); 170681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_caps); 171681a5bbeSBoris Popov md_get_mem(mdp, stime, 8, MB_MSYSTEM); 172681a5bbeSBoris Popov md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz); 173681a5bbeSBoris Popov md_get_uint8(mdp, &sblen); 174681a5bbeSBoris Popov if (sblen && (sp->sv_sm & SMB_SM_ENCRYPT)) { 175681a5bbeSBoris Popov if (sblen != SMB_MAXCHALLENGELEN) { 176681a5bbeSBoris Popov SMBERROR("Unexpected length of security blob (%d)\n", sblen); 177681a5bbeSBoris Popov break; 178681a5bbeSBoris Popov } 179681a5bbeSBoris Popov error = md_get_uint16(mdp, &bc); 180681a5bbeSBoris Popov if (error) 181681a5bbeSBoris Popov break; 182681a5bbeSBoris Popov if (sp->sv_caps & SMB_CAP_EXT_SECURITY) 183681a5bbeSBoris Popov md_get_mem(mdp, NULL, 16, MB_MSYSTEM); 184681a5bbeSBoris Popov error = md_get_mem(mdp, vcp->vc_ch, sblen, MB_MSYSTEM); 185681a5bbeSBoris Popov if (error) 186681a5bbeSBoris Popov break; 187681a5bbeSBoris Popov vcp->vc_chlen = sblen; 188681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_ENCRYPT; 189681a5bbeSBoris Popov } 190681a5bbeSBoris Popov vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES; 191681a5bbeSBoris Popov if (dp->d_id == SMB_DIALECT_NTLM0_12 && 192681a5bbeSBoris Popov sp->sv_maxtx < 4096 && 193681a5bbeSBoris Popov (sp->sv_caps & SMB_CAP_NT_SMBS) == 0) { 194681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_WIN95; 195681a5bbeSBoris Popov SMBSDEBUG("Win95 detected\n"); 196681a5bbeSBoris Popov } 197681a5bbeSBoris Popov } else if (dp->d_id > SMB_DIALECT_CORE) { 198681a5bbeSBoris Popov md_get_uint16le(mdp, &tw); 199681a5bbeSBoris Popov sp->sv_sm = tw; 200681a5bbeSBoris Popov md_get_uint16le(mdp, &tw); 201681a5bbeSBoris Popov sp->sv_maxtx = tw; 202681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxmux); 203681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxvcs); 204681a5bbeSBoris Popov md_get_uint16le(mdp, &tw); /* rawmode */ 205681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_skey); 206681a5bbeSBoris Popov if (wc == 13) { /* >= LANMAN1 */ 207681a5bbeSBoris Popov md_get_uint16(mdp, &tw); /* time */ 208681a5bbeSBoris Popov md_get_uint16(mdp, &tw1); /* date */ 209681a5bbeSBoris Popov md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz); 210681a5bbeSBoris Popov md_get_uint16le(mdp, &swlen); 211681a5bbeSBoris Popov if (swlen > SMB_MAXCHALLENGELEN) 212681a5bbeSBoris Popov break; 213681a5bbeSBoris Popov md_get_uint16(mdp, NULL); /* mbz */ 214681a5bbeSBoris Popov if (md_get_uint16(mdp, &bc) != 0) 215681a5bbeSBoris Popov break; 216681a5bbeSBoris Popov if (bc < swlen) 217681a5bbeSBoris Popov break; 218681a5bbeSBoris Popov if (swlen && (sp->sv_sm & SMB_SM_ENCRYPT)) { 219681a5bbeSBoris Popov error = md_get_mem(mdp, vcp->vc_ch, swlen, MB_MSYSTEM); 220681a5bbeSBoris Popov if (error) 221681a5bbeSBoris Popov break; 222681a5bbeSBoris Popov vcp->vc_chlen = swlen; 223681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_ENCRYPT; 224681a5bbeSBoris Popov } 225681a5bbeSBoris Popov } 226681a5bbeSBoris Popov vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES; 227681a5bbeSBoris Popov } else { /* an old CORE protocol */ 228681a5bbeSBoris Popov sp->sv_maxmux = 1; 229681a5bbeSBoris Popov } 230681a5bbeSBoris Popov error = 0; 231681a5bbeSBoris Popov } while (0); 232681a5bbeSBoris Popov if (error == 0) { 233681a5bbeSBoris Popov vcp->vc_maxvcs = sp->sv_maxvcs; 234681a5bbeSBoris Popov if (vcp->vc_maxvcs <= 1) { 235681a5bbeSBoris Popov if (vcp->vc_maxvcs == 0) 236681a5bbeSBoris Popov vcp->vc_maxvcs = 1; 237681a5bbeSBoris Popov } 238681a5bbeSBoris Popov if (sp->sv_maxtx <= 0 || sp->sv_maxtx > 0xffff) 239681a5bbeSBoris Popov sp->sv_maxtx = 1024; 240e2a70cffSBoris Popov else 241e2a70cffSBoris Popov sp->sv_maxtx = min(sp->sv_maxtx, 242e2a70cffSBoris Popov 63*1024 + SMB_HDRLEN + 16); 243e2a70cffSBoris Popov SMB_TRAN_GETPARAM(vcp, SMBTP_RCVSZ, &maxqsz); 244e2a70cffSBoris Popov vcp->vc_rxmax = min(smb_vc_maxread(vcp), maxqsz - 1024); 245681a5bbeSBoris Popov SMB_TRAN_GETPARAM(vcp, SMBTP_SNDSZ, &maxqsz); 246e2a70cffSBoris Popov vcp->vc_wxmax = min(smb_vc_maxwrite(vcp), maxqsz - 1024); 247681a5bbeSBoris Popov vcp->vc_txmax = min(sp->sv_maxtx, maxqsz); 248681a5bbeSBoris Popov SMBSDEBUG("TZ = %d\n", sp->sv_tz); 249681a5bbeSBoris Popov SMBSDEBUG("CAPS = %x\n", sp->sv_caps); 250681a5bbeSBoris Popov SMBSDEBUG("MAXMUX = %d\n", sp->sv_maxmux); 251681a5bbeSBoris Popov SMBSDEBUG("MAXVCS = %d\n", sp->sv_maxvcs); 252681a5bbeSBoris Popov SMBSDEBUG("MAXRAW = %d\n", sp->sv_maxraw); 253681a5bbeSBoris Popov SMBSDEBUG("MAXTX = %d\n", sp->sv_maxtx); 254681a5bbeSBoris Popov } 255681a5bbeSBoris Popov bad: 256681a5bbeSBoris Popov smb_rq_done(rqp); 257681a5bbeSBoris Popov return error; 258681a5bbeSBoris Popov } 259681a5bbeSBoris Popov 260681a5bbeSBoris Popov int 261681a5bbeSBoris Popov smb_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred) 262681a5bbeSBoris Popov { 263681a5bbeSBoris Popov struct smb_rq *rqp; 264681a5bbeSBoris Popov struct mbchain *mbp; 265681a5bbeSBoris Popov /* u_int8_t wc; 266681a5bbeSBoris Popov u_int16_t tw, tw1;*/ 267681a5bbeSBoris Popov smb_uniptr unipp, ntencpass = NULL; 268681a5bbeSBoris Popov char *pp, *up, *pbuf, *encpass; 26970f9b9d9SBoris Popov int error, plen, uniplen, ulen, upper; 27070f9b9d9SBoris Popov 27170f9b9d9SBoris Popov upper = 0; 27270f9b9d9SBoris Popov 27370f9b9d9SBoris Popov again: 274681a5bbeSBoris Popov 275681a5bbeSBoris Popov vcp->vc_smbuid = SMB_UID_UNKNOWN; 276681a5bbeSBoris Popov 2776e551fb6SDavid E. O'Brien if (smb_smb_nomux(vcp, scred, __func__) != 0) 278681a5bbeSBoris Popov return EINVAL; 279681a5bbeSBoris Popov 280681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_SESSION_SETUP_ANDX, scred, &rqp); 281681a5bbeSBoris Popov if (error) 282681a5bbeSBoris Popov return error; 283681a5bbeSBoris Popov pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK); 284681a5bbeSBoris Popov encpass = malloc(24, M_SMBTEMP, M_WAITOK); 285681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_USER) { 28670f9b9d9SBoris Popov /* 28770f9b9d9SBoris Popov * We try w/o uppercasing first so Samba mixed case 28870f9b9d9SBoris Popov * passwords work. If that fails we come back and try 28970f9b9d9SBoris Popov * uppercasing to satisfy OS/2 and Windows for Workgroups. 29070f9b9d9SBoris Popov */ 29170f9b9d9SBoris Popov if (upper++) { 29270f9b9d9SBoris Popov iconv_convstr(vcp->vc_toupper, pbuf, 29370f9b9d9SBoris Popov smb_vc_getpass(vcp)/*, SMB_MAXPASSWORDLEN*/); 29470f9b9d9SBoris Popov } else { 29570f9b9d9SBoris Popov strncpy(pbuf, smb_vc_getpass(vcp), SMB_MAXPASSWORDLEN); 29670f9b9d9SBoris Popov pbuf[SMB_MAXPASSWORDLEN] = '\0'; 29770f9b9d9SBoris Popov } 29870f9b9d9SBoris Popov if (!SMB_UNICODE_STRINGS(vcp)) 29970f9b9d9SBoris Popov iconv_convstr(vcp->vc_toserver, pbuf, pbuf/*, 30070f9b9d9SBoris Popov SMB_MAXPASSWORDLEN*/); 30170f9b9d9SBoris Popov 302681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) { 303681a5bbeSBoris Popov uniplen = plen = 24; 304681a5bbeSBoris Popov smb_encrypt(pbuf, vcp->vc_ch, encpass); 305681a5bbeSBoris Popov ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK); 30670f9b9d9SBoris Popov if (SMB_UNICODE_STRINGS(vcp)) { 30770f9b9d9SBoris Popov strncpy(pbuf, smb_vc_getpass(vcp), 30870f9b9d9SBoris Popov SMB_MAXPASSWORDLEN); 30970f9b9d9SBoris Popov pbuf[SMB_MAXPASSWORDLEN] = '\0'; 31070f9b9d9SBoris Popov } else 31170f9b9d9SBoris Popov iconv_convstr(vcp->vc_toserver, pbuf, 31270f9b9d9SBoris Popov smb_vc_getpass(vcp)/*, 31370f9b9d9SBoris Popov SMB_MAXPASSWORDLEN*/); 314681a5bbeSBoris Popov smb_ntencrypt(pbuf, vcp->vc_ch, (u_char*)ntencpass); 315681a5bbeSBoris Popov pp = encpass; 316681a5bbeSBoris Popov unipp = ntencpass; 317681a5bbeSBoris Popov } else { 318681a5bbeSBoris Popov plen = strlen(pbuf) + 1; 319681a5bbeSBoris Popov pp = pbuf; 320681a5bbeSBoris Popov uniplen = plen * 2; 321681a5bbeSBoris Popov ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK); 322681a5bbeSBoris Popov smb_strtouni(ntencpass, smb_vc_getpass(vcp)); 323681a5bbeSBoris Popov plen--; 324daa35dedSBoris Popov 325daa35dedSBoris Popov /* 326daa35dedSBoris Popov * The uniplen is zeroed because Samba cannot deal 327daa35dedSBoris Popov * with this 2nd cleartext password. This Samba 328daa35dedSBoris Popov * "bug" is actually a workaround for problems in 329daa35dedSBoris Popov * Microsoft clients. 330daa35dedSBoris Popov */ 331681a5bbeSBoris Popov uniplen = 0/*-= 2*/; 332681a5bbeSBoris Popov unipp = ntencpass; 333681a5bbeSBoris Popov } 334681a5bbeSBoris Popov } else { 335681a5bbeSBoris Popov /* 336681a5bbeSBoris Popov * In the share security mode password will be used 337681a5bbeSBoris Popov * only in the tree authentication 338681a5bbeSBoris Popov */ 339681a5bbeSBoris Popov pp = ""; 340681a5bbeSBoris Popov plen = 1; 341681a5bbeSBoris Popov unipp = &smb_unieol; 342cf37bfb0SMax Khon uniplen = 0 /* sizeof(smb_unieol) */; 343681a5bbeSBoris Popov } 344681a5bbeSBoris Popov smb_rq_wstart(rqp); 345681a5bbeSBoris Popov mbp = &rqp->sr_rq; 346681a5bbeSBoris Popov up = vcp->vc_username; 347681a5bbeSBoris Popov ulen = strlen(up) + 1; 34870f9b9d9SBoris Popov /* 34970f9b9d9SBoris Popov * If userid is null we are attempting anonymous browse login 35070f9b9d9SBoris Popov * so passwords must be zero length. 35170f9b9d9SBoris Popov */ 35270f9b9d9SBoris Popov if (ulen == 1) 35370f9b9d9SBoris Popov plen = uniplen = 0; 354681a5bbeSBoris Popov mb_put_uint8(mbp, 0xff); 355681a5bbeSBoris Popov mb_put_uint8(mbp, 0); 356681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); 357681a5bbeSBoris Popov mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxtx); 358681a5bbeSBoris Popov mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxmux); 359681a5bbeSBoris Popov mb_put_uint16le(mbp, vcp->vc_number); 360681a5bbeSBoris Popov mb_put_uint32le(mbp, vcp->vc_sopt.sv_skey); 361681a5bbeSBoris Popov mb_put_uint16le(mbp, plen); 362681a5bbeSBoris Popov if (SMB_DIALECT(vcp) < SMB_DIALECT_NTLM0_12) { 363681a5bbeSBoris Popov mb_put_uint32le(mbp, 0); 364681a5bbeSBoris Popov smb_rq_wend(rqp); 365681a5bbeSBoris Popov smb_rq_bstart(rqp); 366681a5bbeSBoris Popov mb_put_mem(mbp, pp, plen, MB_MSYSTEM); 367681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, up, SMB_CS_NONE); 368681a5bbeSBoris Popov } else { 369681a5bbeSBoris Popov mb_put_uint16le(mbp, uniplen); 370681a5bbeSBoris Popov mb_put_uint32le(mbp, 0); /* reserved */ 371daa35dedSBoris Popov mb_put_uint32le(mbp, vcp->obj.co_flags & SMBV_UNICODE ? 372daa35dedSBoris Popov SMB_CAP_UNICODE : 0); 373681a5bbeSBoris Popov smb_rq_wend(rqp); 374681a5bbeSBoris Popov smb_rq_bstart(rqp); 375681a5bbeSBoris Popov mb_put_mem(mbp, pp, plen, MB_MSYSTEM); 376681a5bbeSBoris Popov mb_put_mem(mbp, (caddr_t)unipp, uniplen, MB_MSYSTEM); 377681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, up, SMB_CS_NONE); /* AccountName */ 378681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, vcp->vc_domain, SMB_CS_NONE); /* PrimaryDomain */ 379681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, "FreeBSD", SMB_CS_NONE); /* Client's OS */ 380681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, "NETSMB", SMB_CS_NONE); /* Client name */ 381681a5bbeSBoris Popov } 382681a5bbeSBoris Popov smb_rq_bend(rqp); 383681a5bbeSBoris Popov if (ntencpass) 384681a5bbeSBoris Popov free(ntencpass, M_SMBTEMP); 385681a5bbeSBoris Popov error = smb_rq_simple(rqp); 386681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 387681a5bbeSBoris Popov if (error) { 388681a5bbeSBoris Popov if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnoaccess) 389681a5bbeSBoris Popov error = EAUTH; 390681a5bbeSBoris Popov goto bad; 391681a5bbeSBoris Popov } 392681a5bbeSBoris Popov vcp->vc_smbuid = rqp->sr_rpuid; 393681a5bbeSBoris Popov bad: 394681a5bbeSBoris Popov free(encpass, M_SMBTEMP); 395681a5bbeSBoris Popov free(pbuf, M_SMBTEMP); 396681a5bbeSBoris Popov smb_rq_done(rqp); 39770f9b9d9SBoris Popov if (error && upper == 1 && vcp->vc_sopt.sv_sm & SMB_SM_USER) 39870f9b9d9SBoris Popov goto again; 399681a5bbeSBoris Popov return error; 400681a5bbeSBoris Popov } 401681a5bbeSBoris Popov 402681a5bbeSBoris Popov int 403681a5bbeSBoris Popov smb_smb_ssnclose(struct smb_vc *vcp, struct smb_cred *scred) 404681a5bbeSBoris Popov { 405681a5bbeSBoris Popov struct smb_rq *rqp; 406681a5bbeSBoris Popov struct mbchain *mbp; 407681a5bbeSBoris Popov int error; 408681a5bbeSBoris Popov 409681a5bbeSBoris Popov if (vcp->vc_smbuid == SMB_UID_UNKNOWN) 410681a5bbeSBoris Popov return 0; 411681a5bbeSBoris Popov 4126e551fb6SDavid E. O'Brien if (smb_smb_nomux(vcp, scred, __func__) != 0) 413681a5bbeSBoris Popov return EINVAL; 414681a5bbeSBoris Popov 415681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_LOGOFF_ANDX, scred, &rqp); 416681a5bbeSBoris Popov if (error) 417681a5bbeSBoris Popov return error; 418681a5bbeSBoris Popov mbp = &rqp->sr_rq; 419681a5bbeSBoris Popov smb_rq_wstart(rqp); 420681a5bbeSBoris Popov mb_put_uint8(mbp, 0xff); 421681a5bbeSBoris Popov mb_put_uint8(mbp, 0); 422681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); 423681a5bbeSBoris Popov smb_rq_wend(rqp); 424681a5bbeSBoris Popov smb_rq_bstart(rqp); 425681a5bbeSBoris Popov smb_rq_bend(rqp); 426681a5bbeSBoris Popov error = smb_rq_simple(rqp); 427681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 428681a5bbeSBoris Popov smb_rq_done(rqp); 429681a5bbeSBoris Popov return error; 430681a5bbeSBoris Popov } 431681a5bbeSBoris Popov 432681a5bbeSBoris Popov static char smb_any_share[] = "?????"; 433681a5bbeSBoris Popov 434681a5bbeSBoris Popov static char * 435681a5bbeSBoris Popov smb_share_typename(int stype) 436681a5bbeSBoris Popov { 437681a5bbeSBoris Popov char *pp; 438681a5bbeSBoris Popov 439681a5bbeSBoris Popov switch (stype) { 440681a5bbeSBoris Popov case SMB_ST_DISK: 441681a5bbeSBoris Popov pp = "A:"; 442681a5bbeSBoris Popov break; 443681a5bbeSBoris Popov case SMB_ST_PRINTER: 444681a5bbeSBoris Popov pp = smb_any_share; /* can't use LPT: here... */ 445681a5bbeSBoris Popov break; 446681a5bbeSBoris Popov case SMB_ST_PIPE: 447681a5bbeSBoris Popov pp = "IPC"; 448681a5bbeSBoris Popov break; 449681a5bbeSBoris Popov case SMB_ST_COMM: 450681a5bbeSBoris Popov pp = "COMM"; 451681a5bbeSBoris Popov break; 452681a5bbeSBoris Popov case SMB_ST_ANY: 453681a5bbeSBoris Popov default: 454681a5bbeSBoris Popov pp = smb_any_share; 455681a5bbeSBoris Popov break; 456681a5bbeSBoris Popov } 457681a5bbeSBoris Popov return pp; 458681a5bbeSBoris Popov } 459681a5bbeSBoris Popov 460681a5bbeSBoris Popov int 461681a5bbeSBoris Popov smb_smb_treeconnect(struct smb_share *ssp, struct smb_cred *scred) 462681a5bbeSBoris Popov { 463681a5bbeSBoris Popov struct smb_vc *vcp; 464681a5bbeSBoris Popov struct smb_rq rq, *rqp = &rq; 465681a5bbeSBoris Popov struct mbchain *mbp; 466681a5bbeSBoris Popov char *pp, *pbuf, *encpass; 46770f9b9d9SBoris Popov int error, plen, caseopt, upper; 46870f9b9d9SBoris Popov 46970f9b9d9SBoris Popov upper = 0; 47070f9b9d9SBoris Popov 47170f9b9d9SBoris Popov again: 47270f9b9d9SBoris Popov 47370f9b9d9SBoris Popov #if 0 47470f9b9d9SBoris Popov /* Disable Unicode for SMB_COM_TREE_CONNECT_ANDX requests */ 47570f9b9d9SBoris Popov if (SSTOVC(ssp)->vc_hflags2 & SMB_FLAGS2_UNICODE) { 47670f9b9d9SBoris Popov vcp = SSTOVC(ssp); 47770f9b9d9SBoris Popov if (vcp->vc_toserver) { 47870f9b9d9SBoris Popov iconv_close(vcp->vc_toserver); 47970f9b9d9SBoris Popov /* Use NULL until UTF-8 -> ASCII works */ 48070f9b9d9SBoris Popov vcp->vc_toserver = NULL; 48170f9b9d9SBoris Popov } 48270f9b9d9SBoris Popov if (vcp->vc_tolocal) { 48370f9b9d9SBoris Popov iconv_close(vcp->vc_tolocal); 48470f9b9d9SBoris Popov /* Use NULL until ASCII -> UTF-8 works*/ 48570f9b9d9SBoris Popov vcp->vc_tolocal = NULL; 48670f9b9d9SBoris Popov } 48770f9b9d9SBoris Popov vcp->vc_hflags2 &= ~SMB_FLAGS2_UNICODE; 48870f9b9d9SBoris Popov } 48970f9b9d9SBoris Popov #endif 490681a5bbeSBoris Popov 491681a5bbeSBoris Popov ssp->ss_tid = SMB_TID_UNKNOWN; 492681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_CONNECT_ANDX, scred, &rqp); 493681a5bbeSBoris Popov if (error) 494681a5bbeSBoris Popov return error; 495681a5bbeSBoris Popov vcp = rqp->sr_vc; 496681a5bbeSBoris Popov caseopt = SMB_CS_NONE; 497681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_USER) { 498681a5bbeSBoris Popov plen = 1; 499681a5bbeSBoris Popov pp = ""; 500681a5bbeSBoris Popov pbuf = NULL; 501681a5bbeSBoris Popov encpass = NULL; 502681a5bbeSBoris Popov } else { 503681a5bbeSBoris Popov pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK); 504681a5bbeSBoris Popov encpass = malloc(24, M_SMBTEMP, M_WAITOK); 50570f9b9d9SBoris Popov /* 50670f9b9d9SBoris Popov * We try w/o uppercasing first so Samba mixed case 50770f9b9d9SBoris Popov * passwords work. If that fails we come back and try 50870f9b9d9SBoris Popov * uppercasing to satisfy OS/2 and Windows for Workgroups. 50970f9b9d9SBoris Popov */ 51070f9b9d9SBoris Popov if (upper++) { 51170f9b9d9SBoris Popov iconv_convstr(vcp->vc_toupper, pbuf, 51270f9b9d9SBoris Popov smb_share_getpass(ssp)/*, 51370f9b9d9SBoris Popov SMB_MAXPASSWORDLEN*/); 51470f9b9d9SBoris Popov } else { 51570f9b9d9SBoris Popov strncpy(pbuf, smb_share_getpass(ssp), 51670f9b9d9SBoris Popov SMB_MAXPASSWORDLEN); 51770f9b9d9SBoris Popov pbuf[SMB_MAXPASSWORDLEN] = '\0'; 51870f9b9d9SBoris Popov } 519681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) { 520681a5bbeSBoris Popov plen = 24; 521681a5bbeSBoris Popov smb_encrypt(pbuf, vcp->vc_ch, encpass); 522681a5bbeSBoris Popov pp = encpass; 523681a5bbeSBoris Popov } else { 524681a5bbeSBoris Popov plen = strlen(pbuf) + 1; 525681a5bbeSBoris Popov pp = pbuf; 526681a5bbeSBoris Popov } 527681a5bbeSBoris Popov } 528681a5bbeSBoris Popov mbp = &rqp->sr_rq; 529681a5bbeSBoris Popov smb_rq_wstart(rqp); 530681a5bbeSBoris Popov mb_put_uint8(mbp, 0xff); 531681a5bbeSBoris Popov mb_put_uint8(mbp, 0); 532681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); 533681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); /* Flags */ 534681a5bbeSBoris Popov mb_put_uint16le(mbp, plen); 535681a5bbeSBoris Popov smb_rq_wend(rqp); 536681a5bbeSBoris Popov smb_rq_bstart(rqp); 537681a5bbeSBoris Popov mb_put_mem(mbp, pp, plen, MB_MSYSTEM); 538681a5bbeSBoris Popov smb_put_dmem(mbp, vcp, "\\\\", 2, caseopt); 539681a5bbeSBoris Popov pp = vcp->vc_srvname; 540681a5bbeSBoris Popov smb_put_dmem(mbp, vcp, pp, strlen(pp), caseopt); 541681a5bbeSBoris Popov smb_put_dmem(mbp, vcp, "\\", 1, caseopt); 542681a5bbeSBoris Popov pp = ssp->ss_name; 543681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, pp, caseopt); 544681a5bbeSBoris Popov pp = smb_share_typename(ssp->ss_type); 545681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, pp, caseopt); 546681a5bbeSBoris Popov smb_rq_bend(rqp); 547681a5bbeSBoris Popov error = smb_rq_simple(rqp); 548681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 549681a5bbeSBoris Popov if (error) 550681a5bbeSBoris Popov goto bad; 551681a5bbeSBoris Popov ssp->ss_tid = rqp->sr_rptid; 552681a5bbeSBoris Popov ssp->ss_vcgenid = vcp->vc_genid; 553681a5bbeSBoris Popov ssp->ss_flags |= SMBS_CONNECTED; 554681a5bbeSBoris Popov bad: 555681a5bbeSBoris Popov if (encpass) 556681a5bbeSBoris Popov free(encpass, M_SMBTEMP); 557681a5bbeSBoris Popov if (pbuf) 558681a5bbeSBoris Popov free(pbuf, M_SMBTEMP); 559681a5bbeSBoris Popov smb_rq_done(rqp); 56070f9b9d9SBoris Popov if (error && upper == 1) 56170f9b9d9SBoris Popov goto again; 562681a5bbeSBoris Popov return error; 563681a5bbeSBoris Popov } 564681a5bbeSBoris Popov 565681a5bbeSBoris Popov int 566681a5bbeSBoris Popov smb_smb_treedisconnect(struct smb_share *ssp, struct smb_cred *scred) 567681a5bbeSBoris Popov { 568681a5bbeSBoris Popov struct smb_rq *rqp; 569681a5bbeSBoris Popov struct mbchain *mbp; 570681a5bbeSBoris Popov int error; 571681a5bbeSBoris Popov 572681a5bbeSBoris Popov if (ssp->ss_tid == SMB_TID_UNKNOWN) 573681a5bbeSBoris Popov return 0; 574681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_DISCONNECT, scred, &rqp); 575681a5bbeSBoris Popov if (error) 576681a5bbeSBoris Popov return error; 577681a5bbeSBoris Popov mbp = &rqp->sr_rq; 578681a5bbeSBoris Popov smb_rq_wstart(rqp); 579681a5bbeSBoris Popov smb_rq_wend(rqp); 580681a5bbeSBoris Popov smb_rq_bstart(rqp); 581681a5bbeSBoris Popov smb_rq_bend(rqp); 582681a5bbeSBoris Popov error = smb_rq_simple(rqp); 583681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 584681a5bbeSBoris Popov smb_rq_done(rqp); 585681a5bbeSBoris Popov ssp->ss_tid = SMB_TID_UNKNOWN; 586681a5bbeSBoris Popov return error; 587681a5bbeSBoris Popov } 588681a5bbeSBoris Popov 589681a5bbeSBoris Popov static __inline int 590e2a70cffSBoris Popov smb_smb_readx(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid, 591e2a70cffSBoris Popov struct uio *uio, struct smb_cred *scred) 592e2a70cffSBoris Popov { 593e2a70cffSBoris Popov struct smb_rq *rqp; 594e2a70cffSBoris Popov struct mbchain *mbp; 595e2a70cffSBoris Popov struct mdchain *mdp; 596e2a70cffSBoris Popov u_int8_t wc; 597e2a70cffSBoris Popov int error; 598e2a70cffSBoris Popov u_int16_t residhi, residlo, off, doff; 599e2a70cffSBoris Popov u_int32_t resid; 600e2a70cffSBoris Popov 601e2a70cffSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ_ANDX, scred, &rqp); 602e2a70cffSBoris Popov if (error) 603e2a70cffSBoris Popov return error; 604e2a70cffSBoris Popov smb_rq_getrequest(rqp, &mbp); 605e2a70cffSBoris Popov smb_rq_wstart(rqp); 606e2a70cffSBoris Popov mb_put_uint8(mbp, 0xff); /* no secondary command */ 607e2a70cffSBoris Popov mb_put_uint8(mbp, 0); /* MBZ */ 608e2a70cffSBoris Popov mb_put_uint16le(mbp, 0); /* offset to secondary */ 609e2a70cffSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM); 610e2a70cffSBoris Popov mb_put_uint32le(mbp, uio->uio_offset); 611e2a70cffSBoris Popov *len = min(SSTOVC(ssp)->vc_rxmax, *len); 612e2a70cffSBoris Popov mb_put_uint16le(mbp, *len); /* MaxCount */ 613e2a70cffSBoris Popov mb_put_uint16le(mbp, *len); /* MinCount (only indicates blocking) */ 614e2a70cffSBoris Popov mb_put_uint32le(mbp, (unsigned)*len >> 16); /* MaxCountHigh */ 615e2a70cffSBoris Popov mb_put_uint16le(mbp, *len); /* Remaining ("obsolete") */ 616e2a70cffSBoris Popov mb_put_uint32le(mbp, uio->uio_offset >> 32); /* OffsetHigh */ 617e2a70cffSBoris Popov smb_rq_wend(rqp); 618e2a70cffSBoris Popov smb_rq_bstart(rqp); 619e2a70cffSBoris Popov smb_rq_bend(rqp); 620e2a70cffSBoris Popov do { 621e2a70cffSBoris Popov error = smb_rq_simple(rqp); 622e2a70cffSBoris Popov if (error) 623e2a70cffSBoris Popov break; 624e2a70cffSBoris Popov smb_rq_getreply(rqp, &mdp); 625e2a70cffSBoris Popov off = SMB_HDRLEN; 626e2a70cffSBoris Popov md_get_uint8(mdp, &wc); 627e2a70cffSBoris Popov off++; 628e2a70cffSBoris Popov if (wc != 12) { 629e2a70cffSBoris Popov error = EBADRPC; 630e2a70cffSBoris Popov break; 631e2a70cffSBoris Popov } 632e2a70cffSBoris Popov md_get_uint8(mdp, NULL); 633e2a70cffSBoris Popov off++; 634e2a70cffSBoris Popov md_get_uint8(mdp, NULL); 635e2a70cffSBoris Popov off++; 636e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); 637e2a70cffSBoris Popov off += 2; 638e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); 639e2a70cffSBoris Popov off += 2; 640e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); /* data compaction mode */ 641e2a70cffSBoris Popov off += 2; 642e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); 643e2a70cffSBoris Popov off += 2; 644e2a70cffSBoris Popov md_get_uint16le(mdp, &residlo); 645e2a70cffSBoris Popov off += 2; 646e2a70cffSBoris Popov md_get_uint16le(mdp, &doff); /* data offset */ 647e2a70cffSBoris Popov off += 2; 648e2a70cffSBoris Popov md_get_uint16le(mdp, &residhi); 649e2a70cffSBoris Popov off += 2; 650e2a70cffSBoris Popov resid = (residhi << 16) | residlo; 651e2a70cffSBoris Popov md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM); 652e2a70cffSBoris Popov off += 4*2; 653e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); /* ByteCount */ 654e2a70cffSBoris Popov off += 2; 655e2a70cffSBoris Popov if (doff > off) /* pad byte(s)? */ 656e2a70cffSBoris Popov md_get_mem(mdp, NULL, doff - off, MB_MSYSTEM); 657e2a70cffSBoris Popov if (resid == 0) { 658e2a70cffSBoris Popov *rresid = resid; 659e2a70cffSBoris Popov break; 660e2a70cffSBoris Popov } 661e2a70cffSBoris Popov error = md_get_uio(mdp, uio, resid); 662e2a70cffSBoris Popov if (error) 663e2a70cffSBoris Popov break; 664e2a70cffSBoris Popov *rresid = resid; 665e2a70cffSBoris Popov } while(0); 666e2a70cffSBoris Popov smb_rq_done(rqp); 667e2a70cffSBoris Popov return (error); 668e2a70cffSBoris Popov } 669e2a70cffSBoris Popov 670e2a70cffSBoris Popov static __inline int 671e2a70cffSBoris Popov smb_smb_writex(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid, 672e2a70cffSBoris Popov struct uio *uio, struct smb_cred *scred) 673e2a70cffSBoris Popov { 674e2a70cffSBoris Popov struct smb_rq *rqp; 675e2a70cffSBoris Popov struct mbchain *mbp; 676e2a70cffSBoris Popov struct mdchain *mdp; 677e2a70cffSBoris Popov int error; 678e2a70cffSBoris Popov u_int8_t wc; 679e2a70cffSBoris Popov u_int16_t resid; 680e2a70cffSBoris Popov 681e2a70cffSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE_ANDX, scred, &rqp); 682e2a70cffSBoris Popov if (error) 683e2a70cffSBoris Popov return (error); 684e2a70cffSBoris Popov smb_rq_getrequest(rqp, &mbp); 685e2a70cffSBoris Popov smb_rq_wstart(rqp); 686e2a70cffSBoris Popov mb_put_uint8(mbp, 0xff); /* no secondary command */ 687e2a70cffSBoris Popov mb_put_uint8(mbp, 0); /* MBZ */ 688e2a70cffSBoris Popov mb_put_uint16le(mbp, 0); /* offset to secondary */ 689e2a70cffSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM); 690e2a70cffSBoris Popov mb_put_uint32le(mbp, uio->uio_offset); 691e2a70cffSBoris Popov mb_put_uint32le(mbp, 0); /* MBZ (timeout) */ 692e2a70cffSBoris Popov mb_put_uint16le(mbp, 0); /* !write-thru */ 693e2a70cffSBoris Popov mb_put_uint16le(mbp, 0); 694e2a70cffSBoris Popov *len = min(SSTOVC(ssp)->vc_wxmax, *len); 695e2a70cffSBoris Popov mb_put_uint16le(mbp, (unsigned)*len >> 16); 696e2a70cffSBoris Popov mb_put_uint16le(mbp, *len); 697e2a70cffSBoris Popov mb_put_uint16le(mbp, 64); /* data offset from header start */ 698e2a70cffSBoris Popov mb_put_uint32le(mbp, uio->uio_offset >> 32); /* OffsetHigh */ 699e2a70cffSBoris Popov smb_rq_wend(rqp); 700e2a70cffSBoris Popov smb_rq_bstart(rqp); 701e2a70cffSBoris Popov do { 702e2a70cffSBoris Popov mb_put_uint8(mbp, 0xee); /* mimic xp pad byte! */ 703e2a70cffSBoris Popov error = mb_put_uio(mbp, uio, *len); 704e2a70cffSBoris Popov if (error) 705e2a70cffSBoris Popov break; 706e2a70cffSBoris Popov smb_rq_bend(rqp); 707e2a70cffSBoris Popov error = smb_rq_simple(rqp); 708e2a70cffSBoris Popov if (error) 709e2a70cffSBoris Popov break; 710e2a70cffSBoris Popov smb_rq_getreply(rqp, &mdp); 711e2a70cffSBoris Popov md_get_uint8(mdp, &wc); 712e2a70cffSBoris Popov if (wc != 6) { 713e2a70cffSBoris Popov error = EBADRPC; 714e2a70cffSBoris Popov break; 715e2a70cffSBoris Popov } 716e2a70cffSBoris Popov md_get_uint8(mdp, NULL); 717e2a70cffSBoris Popov md_get_uint8(mdp, NULL); 718e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); 719e2a70cffSBoris Popov md_get_uint16le(mdp, &resid); 720e2a70cffSBoris Popov *rresid = resid; 721e2a70cffSBoris Popov } while(0); 722e2a70cffSBoris Popov 723e2a70cffSBoris Popov smb_rq_done(rqp); 724e2a70cffSBoris Popov return (error); 725e2a70cffSBoris Popov } 726e2a70cffSBoris Popov 727e2a70cffSBoris Popov static __inline int 728681a5bbeSBoris Popov smb_smb_read(struct smb_share *ssp, u_int16_t fid, 729681a5bbeSBoris Popov int *len, int *rresid, struct uio *uio, struct smb_cred *scred) 730681a5bbeSBoris Popov { 731681a5bbeSBoris Popov struct smb_rq *rqp; 732681a5bbeSBoris Popov struct mbchain *mbp; 733681a5bbeSBoris Popov struct mdchain *mdp; 734681a5bbeSBoris Popov u_int16_t resid, bc; 735681a5bbeSBoris Popov u_int8_t wc; 736681a5bbeSBoris Popov int error, rlen, blksz; 737681a5bbeSBoris Popov 738e2a70cffSBoris Popov if (SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_READX) 739e2a70cffSBoris Popov return (smb_smb_readx(ssp, fid, len, rresid, uio, scred)); 740e2a70cffSBoris Popov 741681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ, scred, &rqp); 742681a5bbeSBoris Popov if (error) 743681a5bbeSBoris Popov return error; 744681a5bbeSBoris Popov 745681a5bbeSBoris Popov blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16; 746681a5bbeSBoris Popov rlen = *len = min(blksz, *len); 747681a5bbeSBoris Popov 748681a5bbeSBoris Popov smb_rq_getrequest(rqp, &mbp); 749681a5bbeSBoris Popov smb_rq_wstart(rqp); 750681a5bbeSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM); 751681a5bbeSBoris Popov mb_put_uint16le(mbp, rlen); 752681a5bbeSBoris Popov mb_put_uint32le(mbp, uio->uio_offset); 753681a5bbeSBoris Popov mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff)); 754681a5bbeSBoris Popov smb_rq_wend(rqp); 755681a5bbeSBoris Popov smb_rq_bstart(rqp); 756681a5bbeSBoris Popov smb_rq_bend(rqp); 757681a5bbeSBoris Popov do { 758681a5bbeSBoris Popov error = smb_rq_simple(rqp); 759681a5bbeSBoris Popov if (error) 760681a5bbeSBoris Popov break; 761681a5bbeSBoris Popov smb_rq_getreply(rqp, &mdp); 762681a5bbeSBoris Popov md_get_uint8(mdp, &wc); 763681a5bbeSBoris Popov if (wc != 5) { 764681a5bbeSBoris Popov error = EBADRPC; 765681a5bbeSBoris Popov break; 766681a5bbeSBoris Popov } 767681a5bbeSBoris Popov md_get_uint16le(mdp, &resid); 768681a5bbeSBoris Popov md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM); 769681a5bbeSBoris Popov md_get_uint16le(mdp, &bc); 770681a5bbeSBoris Popov md_get_uint8(mdp, NULL); /* ignore buffer type */ 771681a5bbeSBoris Popov md_get_uint16le(mdp, &resid); 772681a5bbeSBoris Popov if (resid == 0) { 773681a5bbeSBoris Popov *rresid = resid; 774681a5bbeSBoris Popov break; 775681a5bbeSBoris Popov } 776681a5bbeSBoris Popov error = md_get_uio(mdp, uio, resid); 777681a5bbeSBoris Popov if (error) 778681a5bbeSBoris Popov break; 779681a5bbeSBoris Popov *rresid = resid; 780681a5bbeSBoris Popov } while(0); 781681a5bbeSBoris Popov smb_rq_done(rqp); 782681a5bbeSBoris Popov return error; 783681a5bbeSBoris Popov } 784681a5bbeSBoris Popov 785681a5bbeSBoris Popov int 786681a5bbeSBoris Popov smb_read(struct smb_share *ssp, u_int16_t fid, struct uio *uio, 787681a5bbeSBoris Popov struct smb_cred *scred) 788681a5bbeSBoris Popov { 789681a5bbeSBoris Popov int tsize, len, resid; 790681a5bbeSBoris Popov int error = 0; 791681a5bbeSBoris Popov 792681a5bbeSBoris Popov tsize = uio->uio_resid; 793681a5bbeSBoris Popov while (tsize > 0) { 794681a5bbeSBoris Popov len = tsize; 795681a5bbeSBoris Popov error = smb_smb_read(ssp, fid, &len, &resid, uio, scred); 796681a5bbeSBoris Popov if (error) 797681a5bbeSBoris Popov break; 798681a5bbeSBoris Popov tsize -= resid; 799681a5bbeSBoris Popov if (resid < len) 800681a5bbeSBoris Popov break; 801681a5bbeSBoris Popov } 802681a5bbeSBoris Popov return error; 803681a5bbeSBoris Popov } 804681a5bbeSBoris Popov 805681a5bbeSBoris Popov static __inline int 806681a5bbeSBoris Popov smb_smb_write(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid, 807681a5bbeSBoris Popov struct uio *uio, struct smb_cred *scred) 808681a5bbeSBoris Popov { 809681a5bbeSBoris Popov struct smb_rq *rqp; 810681a5bbeSBoris Popov struct mbchain *mbp; 811681a5bbeSBoris Popov struct mdchain *mdp; 812681a5bbeSBoris Popov u_int16_t resid; 813681a5bbeSBoris Popov u_int8_t wc; 814681a5bbeSBoris Popov int error, blksz; 815681a5bbeSBoris Popov 816e2a70cffSBoris Popov if (*len && SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX) 817e2a70cffSBoris Popov return (smb_smb_writex(ssp, fid, len, rresid, uio, scred)); 818e2a70cffSBoris Popov 819681a5bbeSBoris Popov blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16; 820681a5bbeSBoris Popov if (blksz > 0xffff) 821681a5bbeSBoris Popov blksz = 0xffff; 822681a5bbeSBoris Popov 823681a5bbeSBoris Popov resid = *len = min(blksz, *len); 824681a5bbeSBoris Popov 825681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE, scred, &rqp); 826681a5bbeSBoris Popov if (error) 827681a5bbeSBoris Popov return error; 828681a5bbeSBoris Popov smb_rq_getrequest(rqp, &mbp); 829681a5bbeSBoris Popov smb_rq_wstart(rqp); 830681a5bbeSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM); 831681a5bbeSBoris Popov mb_put_uint16le(mbp, resid); 832681a5bbeSBoris Popov mb_put_uint32le(mbp, uio->uio_offset); 833681a5bbeSBoris Popov mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff)); 834681a5bbeSBoris Popov smb_rq_wend(rqp); 835681a5bbeSBoris Popov smb_rq_bstart(rqp); 836681a5bbeSBoris Popov mb_put_uint8(mbp, SMB_DT_DATA); 837681a5bbeSBoris Popov mb_put_uint16le(mbp, resid); 838681a5bbeSBoris Popov do { 839681a5bbeSBoris Popov error = mb_put_uio(mbp, uio, resid); 840681a5bbeSBoris Popov if (error) 841681a5bbeSBoris Popov break; 842681a5bbeSBoris Popov smb_rq_bend(rqp); 843681a5bbeSBoris Popov error = smb_rq_simple(rqp); 844681a5bbeSBoris Popov if (error) 845681a5bbeSBoris Popov break; 846681a5bbeSBoris Popov smb_rq_getreply(rqp, &mdp); 847681a5bbeSBoris Popov md_get_uint8(mdp, &wc); 848681a5bbeSBoris Popov if (wc != 1) { 849681a5bbeSBoris Popov error = EBADRPC; 850681a5bbeSBoris Popov break; 851681a5bbeSBoris Popov } 852681a5bbeSBoris Popov md_get_uint16le(mdp, &resid); 853681a5bbeSBoris Popov *rresid = resid; 854681a5bbeSBoris Popov } while(0); 855681a5bbeSBoris Popov smb_rq_done(rqp); 856681a5bbeSBoris Popov return error; 857681a5bbeSBoris Popov } 858681a5bbeSBoris Popov 859681a5bbeSBoris Popov int 860681a5bbeSBoris Popov smb_write(struct smb_share *ssp, u_int16_t fid, struct uio *uio, 861681a5bbeSBoris Popov struct smb_cred *scred) 862681a5bbeSBoris Popov { 863681a5bbeSBoris Popov int error = 0, len, tsize, resid; 864681a5bbeSBoris Popov struct uio olduio; 865681a5bbeSBoris Popov 866681a5bbeSBoris Popov tsize = uio->uio_resid; 867681a5bbeSBoris Popov olduio = *uio; 868681a5bbeSBoris Popov while (tsize > 0) { 869681a5bbeSBoris Popov len = tsize; 870681a5bbeSBoris Popov error = smb_smb_write(ssp, fid, &len, &resid, uio, scred); 871681a5bbeSBoris Popov if (error) 872681a5bbeSBoris Popov break; 873681a5bbeSBoris Popov if (resid < len) { 874681a5bbeSBoris Popov error = EIO; 875681a5bbeSBoris Popov break; 876681a5bbeSBoris Popov } 877681a5bbeSBoris Popov tsize -= resid; 878681a5bbeSBoris Popov } 879681a5bbeSBoris Popov if (error) { 8806cd9842fSBoris Popov /* 8816cd9842fSBoris Popov * Errors can happen on the copyin, the rpc, etc. So they 8826cd9842fSBoris Popov * imply resid is unreliable. The only safe thing is 8836cd9842fSBoris Popov * to pretend zero bytes made it. We needn't restore the 8846cd9842fSBoris Popov * iovs because callers don't depend on them in error 8856cd9842fSBoris Popov * paths - uio_resid and uio_offset are what matter. 8866cd9842fSBoris Popov */ 887681a5bbeSBoris Popov *uio = olduio; 888681a5bbeSBoris Popov } 889681a5bbeSBoris Popov return error; 890681a5bbeSBoris Popov } 891681a5bbeSBoris Popov 892681a5bbeSBoris Popov int 893681a5bbeSBoris Popov smb_smb_echo(struct smb_vc *vcp, struct smb_cred *scred) 894681a5bbeSBoris Popov { 895681a5bbeSBoris Popov struct smb_rq *rqp; 896681a5bbeSBoris Popov struct mbchain *mbp; 897681a5bbeSBoris Popov int error; 898681a5bbeSBoris Popov 899681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_ECHO, scred, &rqp); 900681a5bbeSBoris Popov if (error) 901681a5bbeSBoris Popov return error; 902681a5bbeSBoris Popov mbp = &rqp->sr_rq; 903681a5bbeSBoris Popov smb_rq_wstart(rqp); 904681a5bbeSBoris Popov mb_put_uint16le(mbp, 1); 905681a5bbeSBoris Popov smb_rq_wend(rqp); 906681a5bbeSBoris Popov smb_rq_bstart(rqp); 907681a5bbeSBoris Popov mb_put_uint32le(mbp, 0); 908681a5bbeSBoris Popov smb_rq_bend(rqp); 909681a5bbeSBoris Popov error = smb_rq_simple(rqp); 910681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 911681a5bbeSBoris Popov smb_rq_done(rqp); 912681a5bbeSBoris Popov return error; 913681a5bbeSBoris Popov } 914