KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > jcifs > smb > SmbComSessionSetupAndX


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package com.knowgate.jcifs.smb;
20
21 import com.knowgate.jcifs.Config;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
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 JavaDoc 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                 // encrypted
60
accountPassword = auth.getAnsiHash( session.transport.server.encryptionKey );
61                 unicodePassword = auth.getUnicodeHash( session.transport.server.encryptionKey );
62                 passwordLength = unicodePasswordLength = 24;
63                 // fix for win9x clients
64
if (unicodePassword.length == 0) unicodePasswordLength = 0;
65             } else if( DISABLE_PLAIN_TEXT_PASSWORDS ) {
66                 throw new RuntimeException JavaDoc( "Plain text passwords are disabled" );
67             } else if( useUnicode ) {
68                 // plain text
69
String JavaDoc 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                 // plain text
76
String JavaDoc 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             // no password in session setup
84
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             // at least NT 4 observed needing this only with unicode
125
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 JavaDoc in, int byteCount,
142                 byte[] buffer, int bufferIndex ) throws IOException JavaDoc {
143         return 0;
144     }
145     public String JavaDoc toString() {
146         String JavaDoc result = new String JavaDoc( "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