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 74681a5bbeSBoris Popov static int 75681a5bbeSBoris Popov smb_smb_nomux(struct smb_vc *vcp, struct smb_cred *scred, const char *name) 76681a5bbeSBoris Popov { 77fce6fbfaSBoris Popov if (scred->scr_td->td_proc == vcp->vc_iod->iod_p) 78681a5bbeSBoris Popov return 0; 79681a5bbeSBoris Popov SMBERROR("wrong function called(%s)\n", name); 80681a5bbeSBoris Popov return EINVAL; 81681a5bbeSBoris Popov } 82681a5bbeSBoris Popov 83681a5bbeSBoris Popov int 84681a5bbeSBoris Popov smb_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred) 85681a5bbeSBoris Popov { 86681a5bbeSBoris Popov struct smb_dialect *dp; 87681a5bbeSBoris Popov struct smb_sopt *sp = NULL; 88681a5bbeSBoris Popov struct smb_rq *rqp; 89681a5bbeSBoris Popov struct mbchain *mbp; 90681a5bbeSBoris Popov struct mdchain *mdp; 91681a5bbeSBoris Popov u_int8_t wc, stime[8], sblen; 92681a5bbeSBoris Popov u_int16_t dindex, tw, tw1, swlen, bc; 93681a5bbeSBoris Popov int error, maxqsz; 94681a5bbeSBoris Popov 95681a5bbeSBoris Popov if (smb_smb_nomux(vcp, scred, __FUNCTION__) != 0) 96681a5bbeSBoris Popov return EINVAL; 97681a5bbeSBoris Popov vcp->vc_hflags = 0; 98681a5bbeSBoris Popov vcp->vc_hflags2 = 0; 99681a5bbeSBoris Popov vcp->obj.co_flags &= ~(SMBV_ENCRYPT); 100681a5bbeSBoris Popov sp = &vcp->vc_sopt; 101681a5bbeSBoris Popov bzero(sp, sizeof(struct smb_sopt)); 102681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_NEGOTIATE, scred, &rqp); 103681a5bbeSBoris Popov if (error) 104681a5bbeSBoris Popov return error; 105681a5bbeSBoris Popov smb_rq_getrequest(rqp, &mbp); 106681a5bbeSBoris Popov smb_rq_wstart(rqp); 107681a5bbeSBoris Popov smb_rq_wend(rqp); 108681a5bbeSBoris Popov smb_rq_bstart(rqp); 109681a5bbeSBoris Popov for(dp = smb_dialects; dp->d_id != -1; dp++) { 110681a5bbeSBoris Popov mb_put_uint8(mbp, SMB_DT_DIALECT); 111681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, dp->d_name, SMB_CS_NONE); 112681a5bbeSBoris Popov } 113681a5bbeSBoris Popov smb_rq_bend(rqp); 114681a5bbeSBoris Popov error = smb_rq_simple(rqp); 115681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 116681a5bbeSBoris Popov if (error) 117681a5bbeSBoris Popov goto bad; 118681a5bbeSBoris Popov smb_rq_getreply(rqp, &mdp); 119681a5bbeSBoris Popov do { 120681a5bbeSBoris Popov error = md_get_uint8(mdp, &wc); 121681a5bbeSBoris Popov if (error) 122681a5bbeSBoris Popov break; 123681a5bbeSBoris Popov error = md_get_uint16le(mdp, &dindex); 124681a5bbeSBoris Popov if (error) 125681a5bbeSBoris Popov break; 126681a5bbeSBoris Popov if (dindex > 7) { 127681a5bbeSBoris Popov SMBERROR("Don't know how to talk with server %s (%d)\n", "xxx", dindex); 128681a5bbeSBoris Popov error = EBADRPC; 129681a5bbeSBoris Popov break; 130681a5bbeSBoris Popov } 131681a5bbeSBoris Popov dp = smb_dialects + dindex; 132681a5bbeSBoris Popov sp->sv_proto = dp->d_id; 133681a5bbeSBoris Popov SMBSDEBUG("Dialect %s (%d, %d)\n", dp->d_name, dindex, wc); 134681a5bbeSBoris Popov error = EBADRPC; 135681a5bbeSBoris Popov if (dp->d_id >= SMB_DIALECT_NTLM0_12) { 136681a5bbeSBoris Popov if (wc != 17) 137681a5bbeSBoris Popov break; 138681a5bbeSBoris Popov md_get_uint8(mdp, &sp->sv_sm); 139681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxmux); 140681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxvcs); 141681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_maxtx); 142681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_maxraw); 143681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_skey); 144681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_caps); 145681a5bbeSBoris Popov md_get_mem(mdp, stime, 8, MB_MSYSTEM); 146681a5bbeSBoris Popov md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz); 147681a5bbeSBoris Popov md_get_uint8(mdp, &sblen); 148681a5bbeSBoris Popov if (sblen && (sp->sv_sm & SMB_SM_ENCRYPT)) { 149681a5bbeSBoris Popov if (sblen != SMB_MAXCHALLENGELEN) { 150681a5bbeSBoris Popov SMBERROR("Unexpected length of security blob (%d)\n", sblen); 151681a5bbeSBoris Popov break; 152681a5bbeSBoris Popov } 153681a5bbeSBoris Popov error = md_get_uint16(mdp, &bc); 154681a5bbeSBoris Popov if (error) 155681a5bbeSBoris Popov break; 156681a5bbeSBoris Popov if (sp->sv_caps & SMB_CAP_EXT_SECURITY) 157681a5bbeSBoris Popov md_get_mem(mdp, NULL, 16, MB_MSYSTEM); 158681a5bbeSBoris Popov error = md_get_mem(mdp, vcp->vc_ch, sblen, MB_MSYSTEM); 159681a5bbeSBoris Popov if (error) 160681a5bbeSBoris Popov break; 161681a5bbeSBoris Popov vcp->vc_chlen = sblen; 162681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_ENCRYPT; 163681a5bbeSBoris Popov } 164681a5bbeSBoris Popov vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES; 165681a5bbeSBoris Popov if (dp->d_id == SMB_DIALECT_NTLM0_12 && 166681a5bbeSBoris Popov sp->sv_maxtx < 4096 && 167681a5bbeSBoris Popov (sp->sv_caps & SMB_CAP_NT_SMBS) == 0) { 168681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_WIN95; 169681a5bbeSBoris Popov SMBSDEBUG("Win95 detected\n"); 170681a5bbeSBoris Popov } 171681a5bbeSBoris Popov } else if (dp->d_id > SMB_DIALECT_CORE) { 172681a5bbeSBoris Popov md_get_uint16le(mdp, &tw); 173681a5bbeSBoris Popov sp->sv_sm = tw; 174681a5bbeSBoris Popov md_get_uint16le(mdp, &tw); 175681a5bbeSBoris Popov sp->sv_maxtx = tw; 176681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxmux); 177681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxvcs); 178681a5bbeSBoris Popov md_get_uint16le(mdp, &tw); /* rawmode */ 179681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_skey); 180681a5bbeSBoris Popov if (wc == 13) { /* >= LANMAN1 */ 181681a5bbeSBoris Popov md_get_uint16(mdp, &tw); /* time */ 182681a5bbeSBoris Popov md_get_uint16(mdp, &tw1); /* date */ 183681a5bbeSBoris Popov md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz); 184681a5bbeSBoris Popov md_get_uint16le(mdp, &swlen); 185681a5bbeSBoris Popov if (swlen > SMB_MAXCHALLENGELEN) 186681a5bbeSBoris Popov break; 187681a5bbeSBoris Popov md_get_uint16(mdp, NULL); /* mbz */ 188681a5bbeSBoris Popov if (md_get_uint16(mdp, &bc) != 0) 189681a5bbeSBoris Popov break; 190681a5bbeSBoris Popov if (bc < swlen) 191681a5bbeSBoris Popov break; 192681a5bbeSBoris Popov if (swlen && (sp->sv_sm & SMB_SM_ENCRYPT)) { 193681a5bbeSBoris Popov error = md_get_mem(mdp, vcp->vc_ch, swlen, MB_MSYSTEM); 194681a5bbeSBoris Popov if (error) 195681a5bbeSBoris Popov break; 196681a5bbeSBoris Popov vcp->vc_chlen = swlen; 197681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_ENCRYPT; 198681a5bbeSBoris Popov } 199681a5bbeSBoris Popov } 200681a5bbeSBoris Popov vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES; 201681a5bbeSBoris Popov } else { /* an old CORE protocol */ 202681a5bbeSBoris Popov sp->sv_maxmux = 1; 203681a5bbeSBoris Popov } 204681a5bbeSBoris Popov error = 0; 205681a5bbeSBoris Popov } while (0); 206681a5bbeSBoris Popov if (error == 0) { 207681a5bbeSBoris Popov vcp->vc_maxvcs = sp->sv_maxvcs; 208681a5bbeSBoris Popov if (vcp->vc_maxvcs <= 1) { 209681a5bbeSBoris Popov if (vcp->vc_maxvcs == 0) 210681a5bbeSBoris Popov vcp->vc_maxvcs = 1; 211681a5bbeSBoris Popov } 212681a5bbeSBoris Popov if (sp->sv_maxtx <= 0 || sp->sv_maxtx > 0xffff) 213681a5bbeSBoris Popov sp->sv_maxtx = 1024; 214681a5bbeSBoris Popov SMB_TRAN_GETPARAM(vcp, SMBTP_SNDSZ, &maxqsz); 215681a5bbeSBoris Popov vcp->vc_txmax = min(sp->sv_maxtx, maxqsz); 216681a5bbeSBoris Popov SMBSDEBUG("TZ = %d\n", sp->sv_tz); 217681a5bbeSBoris Popov SMBSDEBUG("CAPS = %x\n", sp->sv_caps); 218681a5bbeSBoris Popov SMBSDEBUG("MAXMUX = %d\n", sp->sv_maxmux); 219681a5bbeSBoris Popov SMBSDEBUG("MAXVCS = %d\n", sp->sv_maxvcs); 220681a5bbeSBoris Popov SMBSDEBUG("MAXRAW = %d\n", sp->sv_maxraw); 221681a5bbeSBoris Popov SMBSDEBUG("MAXTX = %d\n", sp->sv_maxtx); 222681a5bbeSBoris Popov } 223681a5bbeSBoris Popov bad: 224681a5bbeSBoris Popov smb_rq_done(rqp); 225681a5bbeSBoris Popov return error; 226681a5bbeSBoris Popov } 227681a5bbeSBoris Popov 228681a5bbeSBoris Popov int 229681a5bbeSBoris Popov smb_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred) 230681a5bbeSBoris Popov { 231681a5bbeSBoris Popov struct smb_rq *rqp; 232681a5bbeSBoris Popov struct mbchain *mbp; 233681a5bbeSBoris Popov /* u_int8_t wc; 234681a5bbeSBoris Popov u_int16_t tw, tw1;*/ 235681a5bbeSBoris Popov smb_uniptr unipp, ntencpass = NULL; 236681a5bbeSBoris Popov char *pp, *up, *pbuf, *encpass; 237681a5bbeSBoris Popov int error, plen, uniplen, ulen; 238681a5bbeSBoris Popov 239681a5bbeSBoris Popov vcp->vc_smbuid = SMB_UID_UNKNOWN; 240681a5bbeSBoris Popov 241681a5bbeSBoris Popov if (smb_smb_nomux(vcp, scred, __FUNCTION__) != 0) 242681a5bbeSBoris Popov return EINVAL; 243681a5bbeSBoris Popov 244681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_SESSION_SETUP_ANDX, scred, &rqp); 245681a5bbeSBoris Popov if (error) 246681a5bbeSBoris Popov return error; 247681a5bbeSBoris Popov pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK); 248681a5bbeSBoris Popov encpass = malloc(24, M_SMBTEMP, M_WAITOK); 249681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_USER) { 250681a5bbeSBoris Popov iconv_convstr(vcp->vc_toupper, pbuf, smb_vc_getpass(vcp)); 251681a5bbeSBoris Popov iconv_convstr(vcp->vc_toserver, pbuf, pbuf); 252681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) { 253681a5bbeSBoris Popov uniplen = plen = 24; 254681a5bbeSBoris Popov smb_encrypt(pbuf, vcp->vc_ch, encpass); 255681a5bbeSBoris Popov ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK); 256681a5bbeSBoris Popov iconv_convstr(vcp->vc_toserver, pbuf, smb_vc_getpass(vcp)); 257681a5bbeSBoris Popov smb_ntencrypt(pbuf, vcp->vc_ch, (u_char*)ntencpass); 258681a5bbeSBoris Popov pp = encpass; 259681a5bbeSBoris Popov unipp = ntencpass; 260681a5bbeSBoris Popov } else { 261681a5bbeSBoris Popov plen = strlen(pbuf) + 1; 262681a5bbeSBoris Popov pp = pbuf; 263681a5bbeSBoris Popov uniplen = plen * 2; 264681a5bbeSBoris Popov ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK); 265681a5bbeSBoris Popov smb_strtouni(ntencpass, smb_vc_getpass(vcp)); 266681a5bbeSBoris Popov plen--; 267681a5bbeSBoris Popov uniplen = 0/*-= 2*/; 268681a5bbeSBoris Popov unipp = ntencpass; 269681a5bbeSBoris Popov } 270681a5bbeSBoris Popov } else { 271681a5bbeSBoris Popov /* 272681a5bbeSBoris Popov * In the share security mode password will be used 273681a5bbeSBoris Popov * only in the tree authentication 274681a5bbeSBoris Popov */ 275681a5bbeSBoris Popov pp = ""; 276681a5bbeSBoris Popov plen = 1; 277681a5bbeSBoris Popov unipp = &smb_unieol; 278681a5bbeSBoris Popov uniplen = sizeof(smb_unieol); 279681a5bbeSBoris Popov } 280681a5bbeSBoris Popov smb_rq_wstart(rqp); 281681a5bbeSBoris Popov mbp = &rqp->sr_rq; 282681a5bbeSBoris Popov up = vcp->vc_username; 283681a5bbeSBoris Popov ulen = strlen(up) + 1; 284681a5bbeSBoris Popov mb_put_uint8(mbp, 0xff); 285681a5bbeSBoris Popov mb_put_uint8(mbp, 0); 286681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); 287681a5bbeSBoris Popov mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxtx); 288681a5bbeSBoris Popov mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxmux); 289681a5bbeSBoris Popov mb_put_uint16le(mbp, vcp->vc_number); 290681a5bbeSBoris Popov mb_put_uint32le(mbp, vcp->vc_sopt.sv_skey); 291681a5bbeSBoris Popov mb_put_uint16le(mbp, plen); 292681a5bbeSBoris Popov if (SMB_DIALECT(vcp) < SMB_DIALECT_NTLM0_12) { 293681a5bbeSBoris Popov mb_put_uint32le(mbp, 0); 294681a5bbeSBoris Popov smb_rq_wend(rqp); 295681a5bbeSBoris Popov smb_rq_bstart(rqp); 296681a5bbeSBoris Popov mb_put_mem(mbp, pp, plen, MB_MSYSTEM); 297681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, up, SMB_CS_NONE); 298681a5bbeSBoris Popov } else { 299681a5bbeSBoris Popov mb_put_uint16le(mbp, uniplen); 300681a5bbeSBoris Popov mb_put_uint32le(mbp, 0); /* reserved */ 301681a5bbeSBoris Popov mb_put_uint32le(mbp, 0); /* my caps */ 302681a5bbeSBoris Popov smb_rq_wend(rqp); 303681a5bbeSBoris Popov smb_rq_bstart(rqp); 304681a5bbeSBoris Popov mb_put_mem(mbp, pp, plen, MB_MSYSTEM); 305681a5bbeSBoris Popov mb_put_mem(mbp, (caddr_t)unipp, uniplen, MB_MSYSTEM); 306681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, up, SMB_CS_NONE); /* AccountName */ 307681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, vcp->vc_domain, SMB_CS_NONE); /* PrimaryDomain */ 308681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, "FreeBSD", SMB_CS_NONE); /* Client's OS */ 309681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, "NETSMB", SMB_CS_NONE); /* Client name */ 310681a5bbeSBoris Popov } 311681a5bbeSBoris Popov smb_rq_bend(rqp); 312681a5bbeSBoris Popov if (ntencpass) 313681a5bbeSBoris Popov free(ntencpass, M_SMBTEMP); 314681a5bbeSBoris Popov error = smb_rq_simple(rqp); 315681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 316681a5bbeSBoris Popov if (error) { 317681a5bbeSBoris Popov if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnoaccess) 318681a5bbeSBoris Popov error = EAUTH; 319681a5bbeSBoris Popov goto bad; 320681a5bbeSBoris Popov } 321681a5bbeSBoris Popov vcp->vc_smbuid = rqp->sr_rpuid; 322681a5bbeSBoris Popov bad: 323681a5bbeSBoris Popov free(encpass, M_SMBTEMP); 324681a5bbeSBoris Popov free(pbuf, M_SMBTEMP); 325681a5bbeSBoris Popov smb_rq_done(rqp); 326681a5bbeSBoris Popov return error; 327681a5bbeSBoris Popov } 328681a5bbeSBoris Popov 329681a5bbeSBoris Popov int 330681a5bbeSBoris Popov smb_smb_ssnclose(struct smb_vc *vcp, struct smb_cred *scred) 331681a5bbeSBoris Popov { 332681a5bbeSBoris Popov struct smb_rq *rqp; 333681a5bbeSBoris Popov struct mbchain *mbp; 334681a5bbeSBoris Popov int error; 335681a5bbeSBoris Popov 336681a5bbeSBoris Popov if (vcp->vc_smbuid == SMB_UID_UNKNOWN) 337681a5bbeSBoris Popov return 0; 338681a5bbeSBoris Popov 339681a5bbeSBoris Popov if (smb_smb_nomux(vcp, scred, __FUNCTION__) != 0) 340681a5bbeSBoris Popov return EINVAL; 341681a5bbeSBoris Popov 342681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_LOGOFF_ANDX, scred, &rqp); 343681a5bbeSBoris Popov if (error) 344681a5bbeSBoris Popov return error; 345681a5bbeSBoris Popov mbp = &rqp->sr_rq; 346681a5bbeSBoris Popov smb_rq_wstart(rqp); 347681a5bbeSBoris Popov mb_put_uint8(mbp, 0xff); 348681a5bbeSBoris Popov mb_put_uint8(mbp, 0); 349681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); 350681a5bbeSBoris Popov smb_rq_wend(rqp); 351681a5bbeSBoris Popov smb_rq_bstart(rqp); 352681a5bbeSBoris Popov smb_rq_bend(rqp); 353681a5bbeSBoris Popov error = smb_rq_simple(rqp); 354681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 355681a5bbeSBoris Popov smb_rq_done(rqp); 356681a5bbeSBoris Popov return error; 357681a5bbeSBoris Popov } 358681a5bbeSBoris Popov 359681a5bbeSBoris Popov static char smb_any_share[] = "?????"; 360681a5bbeSBoris Popov 361681a5bbeSBoris Popov static char * 362681a5bbeSBoris Popov smb_share_typename(int stype) 363681a5bbeSBoris Popov { 364681a5bbeSBoris Popov char *pp; 365681a5bbeSBoris Popov 366681a5bbeSBoris Popov switch (stype) { 367681a5bbeSBoris Popov case SMB_ST_DISK: 368681a5bbeSBoris Popov pp = "A:"; 369681a5bbeSBoris Popov break; 370681a5bbeSBoris Popov case SMB_ST_PRINTER: 371681a5bbeSBoris Popov pp = smb_any_share; /* can't use LPT: here... */ 372681a5bbeSBoris Popov break; 373681a5bbeSBoris Popov case SMB_ST_PIPE: 374681a5bbeSBoris Popov pp = "IPC"; 375681a5bbeSBoris Popov break; 376681a5bbeSBoris Popov case SMB_ST_COMM: 377681a5bbeSBoris Popov pp = "COMM"; 378681a5bbeSBoris Popov break; 379681a5bbeSBoris Popov case SMB_ST_ANY: 380681a5bbeSBoris Popov default: 381681a5bbeSBoris Popov pp = smb_any_share; 382681a5bbeSBoris Popov break; 383681a5bbeSBoris Popov } 384681a5bbeSBoris Popov return pp; 385681a5bbeSBoris Popov } 386681a5bbeSBoris Popov 387681a5bbeSBoris Popov int 388681a5bbeSBoris Popov smb_smb_treeconnect(struct smb_share *ssp, struct smb_cred *scred) 389681a5bbeSBoris Popov { 390681a5bbeSBoris Popov struct smb_vc *vcp; 391681a5bbeSBoris Popov struct smb_rq rq, *rqp = &rq; 392681a5bbeSBoris Popov struct mbchain *mbp; 393681a5bbeSBoris Popov char *pp, *pbuf, *encpass; 394681a5bbeSBoris Popov int error, plen, caseopt; 395681a5bbeSBoris Popov 396681a5bbeSBoris Popov ssp->ss_tid = SMB_TID_UNKNOWN; 397681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_CONNECT_ANDX, scred, &rqp); 398681a5bbeSBoris Popov if (error) 399681a5bbeSBoris Popov return error; 400681a5bbeSBoris Popov vcp = rqp->sr_vc; 401681a5bbeSBoris Popov caseopt = SMB_CS_NONE; 402681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_USER) { 403681a5bbeSBoris Popov plen = 1; 404681a5bbeSBoris Popov pp = ""; 405681a5bbeSBoris Popov pbuf = NULL; 406681a5bbeSBoris Popov encpass = NULL; 407681a5bbeSBoris Popov } else { 408681a5bbeSBoris Popov pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK); 409681a5bbeSBoris Popov encpass = malloc(24, M_SMBTEMP, M_WAITOK); 410681a5bbeSBoris Popov iconv_convstr(vcp->vc_toupper, pbuf, smb_share_getpass(ssp)); 411681a5bbeSBoris Popov iconv_convstr(vcp->vc_toserver, pbuf, pbuf); 412681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) { 413681a5bbeSBoris Popov plen = 24; 414681a5bbeSBoris Popov smb_encrypt(pbuf, vcp->vc_ch, encpass); 415681a5bbeSBoris Popov pp = encpass; 416681a5bbeSBoris Popov } else { 417681a5bbeSBoris Popov plen = strlen(pbuf) + 1; 418681a5bbeSBoris Popov pp = pbuf; 419681a5bbeSBoris Popov } 420681a5bbeSBoris Popov } 421681a5bbeSBoris Popov mbp = &rqp->sr_rq; 422681a5bbeSBoris Popov smb_rq_wstart(rqp); 423681a5bbeSBoris Popov mb_put_uint8(mbp, 0xff); 424681a5bbeSBoris Popov mb_put_uint8(mbp, 0); 425681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); 426681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); /* Flags */ 427681a5bbeSBoris Popov mb_put_uint16le(mbp, plen); 428681a5bbeSBoris Popov smb_rq_wend(rqp); 429681a5bbeSBoris Popov smb_rq_bstart(rqp); 430681a5bbeSBoris Popov mb_put_mem(mbp, pp, plen, MB_MSYSTEM); 431681a5bbeSBoris Popov smb_put_dmem(mbp, vcp, "\\\\", 2, caseopt); 432681a5bbeSBoris Popov pp = vcp->vc_srvname; 433681a5bbeSBoris Popov smb_put_dmem(mbp, vcp, pp, strlen(pp), caseopt); 434681a5bbeSBoris Popov smb_put_dmem(mbp, vcp, "\\", 1, caseopt); 435681a5bbeSBoris Popov pp = ssp->ss_name; 436681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, pp, caseopt); 437681a5bbeSBoris Popov pp = smb_share_typename(ssp->ss_type); 438681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, pp, caseopt); 439681a5bbeSBoris Popov smb_rq_bend(rqp); 440681a5bbeSBoris Popov error = smb_rq_simple(rqp); 441681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 442681a5bbeSBoris Popov if (error) 443681a5bbeSBoris Popov goto bad; 444681a5bbeSBoris Popov ssp->ss_tid = rqp->sr_rptid; 445681a5bbeSBoris Popov ssp->ss_vcgenid = vcp->vc_genid; 446681a5bbeSBoris Popov ssp->ss_flags |= SMBS_CONNECTED; 447681a5bbeSBoris Popov bad: 448681a5bbeSBoris Popov if (encpass) 449681a5bbeSBoris Popov free(encpass, M_SMBTEMP); 450681a5bbeSBoris Popov if (pbuf) 451681a5bbeSBoris Popov free(pbuf, M_SMBTEMP); 452681a5bbeSBoris Popov smb_rq_done(rqp); 453681a5bbeSBoris Popov return error; 454681a5bbeSBoris Popov } 455681a5bbeSBoris Popov 456681a5bbeSBoris Popov int 457681a5bbeSBoris Popov smb_smb_treedisconnect(struct smb_share *ssp, struct smb_cred *scred) 458681a5bbeSBoris Popov { 459681a5bbeSBoris Popov struct smb_rq *rqp; 460681a5bbeSBoris Popov struct mbchain *mbp; 461681a5bbeSBoris Popov int error; 462681a5bbeSBoris Popov 463681a5bbeSBoris Popov if (ssp->ss_tid == SMB_TID_UNKNOWN) 464681a5bbeSBoris Popov return 0; 465681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_DISCONNECT, scred, &rqp); 466681a5bbeSBoris Popov if (error) 467681a5bbeSBoris Popov return error; 468681a5bbeSBoris Popov mbp = &rqp->sr_rq; 469681a5bbeSBoris Popov smb_rq_wstart(rqp); 470681a5bbeSBoris Popov smb_rq_wend(rqp); 471681a5bbeSBoris Popov smb_rq_bstart(rqp); 472681a5bbeSBoris Popov smb_rq_bend(rqp); 473681a5bbeSBoris Popov error = smb_rq_simple(rqp); 474681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 475681a5bbeSBoris Popov smb_rq_done(rqp); 476681a5bbeSBoris Popov ssp->ss_tid = SMB_TID_UNKNOWN; 477681a5bbeSBoris Popov return error; 478681a5bbeSBoris Popov } 479681a5bbeSBoris Popov 480681a5bbeSBoris Popov static __inline int 481681a5bbeSBoris Popov smb_smb_read(struct smb_share *ssp, u_int16_t fid, 482681a5bbeSBoris Popov int *len, int *rresid, struct uio *uio, struct smb_cred *scred) 483681a5bbeSBoris Popov { 484681a5bbeSBoris Popov struct smb_rq *rqp; 485681a5bbeSBoris Popov struct mbchain *mbp; 486681a5bbeSBoris Popov struct mdchain *mdp; 487681a5bbeSBoris Popov u_int16_t resid, bc; 488681a5bbeSBoris Popov u_int8_t wc; 489681a5bbeSBoris Popov int error, rlen, blksz; 490681a5bbeSBoris Popov 491681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ, scred, &rqp); 492681a5bbeSBoris Popov if (error) 493681a5bbeSBoris Popov return error; 494681a5bbeSBoris Popov 495681a5bbeSBoris Popov blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16; 496681a5bbeSBoris Popov rlen = *len = min(blksz, *len); 497681a5bbeSBoris Popov 498681a5bbeSBoris Popov smb_rq_getrequest(rqp, &mbp); 499681a5bbeSBoris Popov smb_rq_wstart(rqp); 500681a5bbeSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM); 501681a5bbeSBoris Popov mb_put_uint16le(mbp, rlen); 502681a5bbeSBoris Popov mb_put_uint32le(mbp, uio->uio_offset); 503681a5bbeSBoris Popov mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff)); 504681a5bbeSBoris Popov smb_rq_wend(rqp); 505681a5bbeSBoris Popov smb_rq_bstart(rqp); 506681a5bbeSBoris Popov smb_rq_bend(rqp); 507681a5bbeSBoris Popov do { 508681a5bbeSBoris Popov error = smb_rq_simple(rqp); 509681a5bbeSBoris Popov if (error) 510681a5bbeSBoris Popov break; 511681a5bbeSBoris Popov smb_rq_getreply(rqp, &mdp); 512681a5bbeSBoris Popov md_get_uint8(mdp, &wc); 513681a5bbeSBoris Popov if (wc != 5) { 514681a5bbeSBoris Popov error = EBADRPC; 515681a5bbeSBoris Popov break; 516681a5bbeSBoris Popov } 517681a5bbeSBoris Popov md_get_uint16le(mdp, &resid); 518681a5bbeSBoris Popov md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM); 519681a5bbeSBoris Popov md_get_uint16le(mdp, &bc); 520681a5bbeSBoris Popov md_get_uint8(mdp, NULL); /* ignore buffer type */ 521681a5bbeSBoris Popov md_get_uint16le(mdp, &resid); 522681a5bbeSBoris Popov if (resid == 0) { 523681a5bbeSBoris Popov *rresid = resid; 524681a5bbeSBoris Popov break; 525681a5bbeSBoris Popov } 526681a5bbeSBoris Popov error = md_get_uio(mdp, uio, resid); 527681a5bbeSBoris Popov if (error) 528681a5bbeSBoris Popov break; 529681a5bbeSBoris Popov *rresid = resid; 530681a5bbeSBoris Popov } while(0); 531681a5bbeSBoris Popov smb_rq_done(rqp); 532681a5bbeSBoris Popov return error; 533681a5bbeSBoris Popov } 534681a5bbeSBoris Popov 535681a5bbeSBoris Popov int 536681a5bbeSBoris Popov smb_read(struct smb_share *ssp, u_int16_t fid, struct uio *uio, 537681a5bbeSBoris Popov struct smb_cred *scred) 538681a5bbeSBoris Popov { 539681a5bbeSBoris Popov int tsize, len, resid; 540681a5bbeSBoris Popov int error = 0; 541681a5bbeSBoris Popov 542681a5bbeSBoris Popov tsize = uio->uio_resid; 543681a5bbeSBoris Popov while (tsize > 0) { 544681a5bbeSBoris Popov len = tsize; 545681a5bbeSBoris Popov error = smb_smb_read(ssp, fid, &len, &resid, uio, scred); 546681a5bbeSBoris Popov if (error) 547681a5bbeSBoris Popov break; 548681a5bbeSBoris Popov tsize -= resid; 549681a5bbeSBoris Popov if (resid < len) 550681a5bbeSBoris Popov break; 551681a5bbeSBoris Popov } 552681a5bbeSBoris Popov return error; 553681a5bbeSBoris Popov } 554681a5bbeSBoris Popov 555681a5bbeSBoris Popov static __inline int 556681a5bbeSBoris Popov smb_smb_write(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid, 557681a5bbeSBoris Popov struct uio *uio, struct smb_cred *scred) 558681a5bbeSBoris Popov { 559681a5bbeSBoris Popov struct smb_rq *rqp; 560681a5bbeSBoris Popov struct mbchain *mbp; 561681a5bbeSBoris Popov struct mdchain *mdp; 562681a5bbeSBoris Popov u_int16_t resid; 563681a5bbeSBoris Popov u_int8_t wc; 564681a5bbeSBoris Popov int error, blksz; 565681a5bbeSBoris Popov 566681a5bbeSBoris Popov blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16; 567681a5bbeSBoris Popov if (blksz > 0xffff) 568681a5bbeSBoris Popov blksz = 0xffff; 569681a5bbeSBoris Popov 570681a5bbeSBoris Popov resid = *len = min(blksz, *len); 571681a5bbeSBoris Popov 572681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE, scred, &rqp); 573681a5bbeSBoris Popov if (error) 574681a5bbeSBoris Popov return error; 575681a5bbeSBoris Popov smb_rq_getrequest(rqp, &mbp); 576681a5bbeSBoris Popov smb_rq_wstart(rqp); 577681a5bbeSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM); 578681a5bbeSBoris Popov mb_put_uint16le(mbp, resid); 579681a5bbeSBoris Popov mb_put_uint32le(mbp, uio->uio_offset); 580681a5bbeSBoris Popov mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff)); 581681a5bbeSBoris Popov smb_rq_wend(rqp); 582681a5bbeSBoris Popov smb_rq_bstart(rqp); 583681a5bbeSBoris Popov mb_put_uint8(mbp, SMB_DT_DATA); 584681a5bbeSBoris Popov mb_put_uint16le(mbp, resid); 585681a5bbeSBoris Popov do { 586681a5bbeSBoris Popov error = mb_put_uio(mbp, uio, resid); 587681a5bbeSBoris Popov if (error) 588681a5bbeSBoris Popov break; 589681a5bbeSBoris Popov smb_rq_bend(rqp); 590681a5bbeSBoris Popov error = smb_rq_simple(rqp); 591681a5bbeSBoris Popov if (error) 592681a5bbeSBoris Popov break; 593681a5bbeSBoris Popov smb_rq_getreply(rqp, &mdp); 594681a5bbeSBoris Popov md_get_uint8(mdp, &wc); 595681a5bbeSBoris Popov if (wc != 1) { 596681a5bbeSBoris Popov error = EBADRPC; 597681a5bbeSBoris Popov break; 598681a5bbeSBoris Popov } 599681a5bbeSBoris Popov md_get_uint16le(mdp, &resid); 600681a5bbeSBoris Popov *rresid = resid; 601681a5bbeSBoris Popov } while(0); 602681a5bbeSBoris Popov smb_rq_done(rqp); 603681a5bbeSBoris Popov return error; 604681a5bbeSBoris Popov } 605681a5bbeSBoris Popov 606681a5bbeSBoris Popov int 607681a5bbeSBoris Popov smb_write(struct smb_share *ssp, u_int16_t fid, struct uio *uio, 608681a5bbeSBoris Popov struct smb_cred *scred) 609681a5bbeSBoris Popov { 610681a5bbeSBoris Popov int error = 0, len, tsize, resid; 611681a5bbeSBoris Popov struct uio olduio; 612681a5bbeSBoris Popov 613681a5bbeSBoris Popov /* 614681a5bbeSBoris Popov * review: manage iov more precisely 615681a5bbeSBoris Popov */ 616681a5bbeSBoris Popov if (uio->uio_iovcnt != 1) { 617681a5bbeSBoris Popov SMBERROR("can't handle iovcnt > 1\n"); 618681a5bbeSBoris Popov return EIO; 619681a5bbeSBoris Popov } 620681a5bbeSBoris Popov tsize = uio->uio_resid; 621681a5bbeSBoris Popov olduio = *uio; 622681a5bbeSBoris Popov while (tsize > 0) { 623681a5bbeSBoris Popov len = tsize; 624681a5bbeSBoris Popov error = smb_smb_write(ssp, fid, &len, &resid, uio, scred); 625681a5bbeSBoris Popov if (error) 626681a5bbeSBoris Popov break; 627681a5bbeSBoris Popov if (resid < len) { 628681a5bbeSBoris Popov error = EIO; 629681a5bbeSBoris Popov break; 630681a5bbeSBoris Popov } 631681a5bbeSBoris Popov tsize -= resid; 632681a5bbeSBoris Popov } 633681a5bbeSBoris Popov if (error) { 634681a5bbeSBoris Popov *uio = olduio; 635681a5bbeSBoris Popov } 636681a5bbeSBoris Popov return error; 637681a5bbeSBoris Popov } 638681a5bbeSBoris Popov 639681a5bbeSBoris Popov int 640681a5bbeSBoris Popov smb_smb_echo(struct smb_vc *vcp, struct smb_cred *scred) 641681a5bbeSBoris Popov { 642681a5bbeSBoris Popov struct smb_rq *rqp; 643681a5bbeSBoris Popov struct mbchain *mbp; 644681a5bbeSBoris Popov int error; 645681a5bbeSBoris Popov 646681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_ECHO, scred, &rqp); 647681a5bbeSBoris Popov if (error) 648681a5bbeSBoris Popov return error; 649681a5bbeSBoris Popov mbp = &rqp->sr_rq; 650681a5bbeSBoris Popov smb_rq_wstart(rqp); 651681a5bbeSBoris Popov mb_put_uint16le(mbp, 1); 652681a5bbeSBoris Popov smb_rq_wend(rqp); 653681a5bbeSBoris Popov smb_rq_bstart(rqp); 654681a5bbeSBoris Popov mb_put_uint32le(mbp, 0); 655681a5bbeSBoris Popov smb_rq_bend(rqp); 656681a5bbeSBoris Popov error = smb_rq_simple(rqp); 657681a5bbeSBoris Popov SMBSDEBUG("%d\n", error); 658681a5bbeSBoris Popov smb_rq_done(rqp); 659681a5bbeSBoris Popov return error; 660681a5bbeSBoris Popov } 661