/// <summary> /// Connect share specified by shareName, this function does not issue tcp or netbios /// connect, it does Negotiate -> SessionSetup -> TreeConnect /// </summary> /// <param name="serverName">The server name</param> /// <param name="domain">The domain</param> /// <param name="userName">The user name</param> /// <param name="password">The password</param> /// <param name="shareName">The share name</param> /// <param name="securityPackage">The security package</param> private void InternalConnectShare(string serverName, string domain, string userName, string password, string shareName, SecurityPackageType securityPackage) { SmbPacket request; SmbPacket response; uint status; // Negotiate: request = this.smbClient.CreateNegotiateRequest( defaultSignState, new string[] { DialectNameString.NTLANMAN }); response = this.SendAndExpectSmbPacket(request, internalTimeout, out status); if (status != 0) { throw new InvalidOperationException("Negotiate Failed. ErrorCode: " + status); } SecurityModes securityMode = (response as SmbNegotiateResponsePacket).SmbParameters.SecurityMode; this.isSignRequired = (securityMode & SecurityModes.NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) == SecurityModes.NEGOTIATE_SECURITY_SIGNATURES_REQUIRED; SmbSecurityPackage secPkg; switch (securityPackage) { case SecurityPackageType.Ntlm: secPkg = SmbSecurityPackage.NTLM; break; case SecurityPackageType.Kerberos: secPkg = SmbSecurityPackage.Kerberos; break; case SecurityPackageType.Negotiate: secPkg = SmbSecurityPackage.Negotiate; break; default: throw new ArgumentException("Unsupported securityPackage: " + securityPackage.ToString()); } // Session setup: request = this.smbClient.CreateFirstSessionSetupRequest(secPkg, serverName, domain, userName, password); response = this.SendAndExpectSmbPacket(request, internalTimeout, out status); while (status != 0) { if ((int)status == (int)SmbStatus.STATUS_MORE_PROCESSING_REQUIRED) { this.uid = (response as SmbSessionSetupAndxResponsePacket).SmbHeader.Uid; request = this.smbClient.CreateSecondSessionSetupRequest(this.uid, secPkg); response = this.SendAndExpectSmbPacket(request, internalTimeout, out status); } else { throw new InvalidOperationException("Session Setup Failed. ErrorCode: " + status); } } this.uid = (response as SmbSessionSetupAndxResponsePacket).SmbHeader.Uid; if (isSignRequired) { CifsClientPerSession session = this.smbClient.Context.GetSession( this.smbClient.Context.Connection.ConnectionId, this.uid); this.sessionKey = session.SessionKey; } // Tree connect: string sharePath = "\\\\" + serverName + '\\' + shareName; request = this.smbClient.CreateTreeConnectRequest(this.uid, sharePath); if (this.isSignRequired) { request.Sign(this.NextSequenceNumber, this.sessionKey); } response = this.SendAndExpectSmbPacket(request, internalTimeout, out status); if (status != 0) { throw new InvalidOperationException("Tree Connect Failed. ErrorCode: " + status); } this.treeId = (response as SmbTreeConnectAndxResponsePacket).SmbHeader.Tid; }
/// <summary> /// Constructor with base class. /// </summary> internal SmbClientSession(CifsClientPerSession session) : base(session) { }