1 18 19 package com.knowgate.jcifs.smb; 20 21 import com.knowgate.jcifs.Config; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 25 class SmbComSessionSetupAndX extends AndXServerMessageBlock { 26 27 private static final int BATCH_LIMIT = 28 Config.getInt( "jcifs.smb.client.SessionSetupAndX.TreeConnectAndX", 1 ); 29 private static final boolean DISABLE_PLAIN_TEXT_PASSWORDS = 30 Config.getBoolean( "jcifs.smb.client.disablePlainTextPasswords", true ); 31 32 private byte[] accountPassword, unicodePassword; 33 private int passwordLength, unicodePasswordLength; 34 private int sessionKey; 35 private String accountName, primaryDomain; 36 37 SmbSession session; 38 NtlmPasswordAuthentication auth; 39 40 SmbComSessionSetupAndX( SmbSession session, ServerMessageBlock andx ) throws SmbException { 41 super( andx ); 42 command = SMB_COM_SESSION_SETUP_ANDX; 43 this.session = session; 44 this.auth = session.auth; 45 if( auth.hashesExternal && auth.challenge != session.transport.server.encryptionKey ) { 46 throw new SmbAuthException( SmbException.NT_STATUS_ACCESS_VIOLATION ); 47 } 48 } 49 50 int getBatchLimit( byte command ) { 51 return command == SMB_COM_TREE_CONNECT_ANDX ? BATCH_LIMIT : 0; 52 } 53 int writeParameterWordsWireFormat( byte[] dst, int dstIndex ) { 54 int start = dstIndex; 55 56 if( session.transport.server.security == SECURITY_USER && 57 ( auth.hashesExternal || auth.password.length() > 0 )) { 58 if( session.transport.server.encryptedPasswords ) { 59 accountPassword = auth.getAnsiHash( session.transport.server.encryptionKey ); 61 unicodePassword = auth.getUnicodeHash( session.transport.server.encryptionKey ); 62 passwordLength = unicodePasswordLength = 24; 63 if (unicodePassword.length == 0) unicodePasswordLength = 0; 65 } else if( DISABLE_PLAIN_TEXT_PASSWORDS ) { 66 throw new RuntimeException ( "Plain text passwords are disabled" ); 67 } else if( useUnicode ) { 68 String password = auth.getPassword(); 70 accountPassword = new byte[0]; 71 passwordLength = 0; 72 unicodePassword = new byte[(password.length() + 1) * 2]; 73 unicodePasswordLength = writeString( password, unicodePassword, 0 ); 74 } else { 75 String password = auth.getPassword(); 77 accountPassword = new byte[(password.length() + 1) * 2]; 78 passwordLength = writeString( password, accountPassword, 0 ); 79 unicodePassword = new byte[0]; 80 unicodePasswordLength = 0; 81 } 82 } else { 83 passwordLength = unicodePasswordLength = 0; 85 } 86 87 sessionKey = session.transport.sessionKey; 88 89 writeInt2( session.transport.snd_buf_size, dst, dstIndex ); 90 dstIndex += 2; 91 writeInt2( session.transport.maxMpxCount, dst, dstIndex ); 92 dstIndex += 2; 93 writeInt2( session.transport.VC_NUMBER, dst, dstIndex ); 94 dstIndex += 2; 95 writeInt4( sessionKey, dst, dstIndex ); 96 dstIndex += 4; 97 writeInt2( passwordLength, dst, dstIndex ); 98 dstIndex += 2; 99 writeInt2( unicodePasswordLength, dst, dstIndex ); 100 dstIndex += 2; 101 dst[dstIndex++] = (byte)0x00; 102 dst[dstIndex++] = (byte)0x00; 103 dst[dstIndex++] = (byte)0x00; 104 dst[dstIndex++] = (byte)0x00; 105 writeInt4( session.transport.capabilities, dst, dstIndex ); 106 dstIndex += 4; 107 108 return dstIndex - start; 109 } 110 int writeBytesWireFormat( byte[] dst, int dstIndex ) { 111 int start = dstIndex; 112 113 accountName = auth.username.toUpperCase(); 114 primaryDomain = auth.domain.toUpperCase(); 115 116 if( session.transport.server.security == SECURITY_USER && 117 ( auth.hashesExternal || auth.password.length() > 0 )) { 118 System.arraycopy( accountPassword, 0, dst, dstIndex, passwordLength ); 119 dstIndex += passwordLength; 120 System.arraycopy( unicodePassword, 0, dst, dstIndex, unicodePasswordLength ); 121 dstIndex += unicodePasswordLength; 122 } 123 if( useUnicode ) { 124 dst[dstIndex++] = (byte)'\0'; 126 } 127 128 dstIndex += writeString( accountName, dst, dstIndex ); 129 dstIndex += writeString( primaryDomain, dst, dstIndex ); 130 dstIndex += writeString( session.transport.NATIVE_OS, dst, dstIndex ); 131 dstIndex += writeString( session.transport.NATIVE_LANMAN, dst, dstIndex ); 132 133 return dstIndex - start; 134 } 135 int readParameterWordsWireFormat( byte[] buffer, int bufferIndex ) { 136 return 0; 137 } 138 int readBytesWireFormat( byte[] buffer, int bufferIndex ) { 139 return 0; 140 } 141 int readBytesDirectWireFormat( InputStream in, int byteCount, 142 byte[] buffer, int bufferIndex ) throws IOException { 143 return 0; 144 } 145 public String toString() { 146 String result = new String ( "SmbComSessionSetupAndX[" + 147 super.toString() + 148 ",snd_buf_size=" + session.transport.snd_buf_size + 149 ",maxMpxCount=" + session.transport.maxMpxCount + 150 ",VC_NUMBER=" + session.transport.VC_NUMBER + 151 ",sessionKey=" + sessionKey + 152 ",passwordLength=" + passwordLength + 153 ",unicodePasswordLength=" + unicodePasswordLength + 154 ",capabilities=" + session.transport.capabilities + 155 ",accountName=" + accountName + 156 ",primaryDomain=" + primaryDomain + 157 ",NATIVE_OS=" + session.transport.NATIVE_OS + 158 ",NATIVE_LANMAN=" + session.transport.NATIVE_LANMAN + "]" ); 159 return result; 160 } 161 } 162 | Popular Tags |