KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 jcifs.smb;
20
21 import jcifs.Config;
22
23 class SmbComSessionSetupAndX extends AndXServerMessageBlock {
24
25     private static final int BATCH_LIMIT =
26             Config.getInt( "jcifs.smb.client.SessionSetupAndX.TreeConnectAndX", 1 );
27     private static final boolean DISABLE_PLAIN_TEXT_PASSWORDS =
28             Config.getBoolean( "jcifs.smb.client.disablePlainTextPasswords", true );
29
30     private byte[] accountPassword, unicodePassword;
31     private int passwordLength, unicodePasswordLength;
32     private int sessionKey;
33     private String JavaDoc accountName, primaryDomain;
34
35     SmbSession session;
36     NtlmPasswordAuthentication auth;
37
38     SmbComSessionSetupAndX( SmbSession session, ServerMessageBlock andx ) throws SmbException {
39         super( andx );
40         command = SMB_COM_SESSION_SETUP_ANDX;
41         this.session = session;
42         this.auth = session.auth;
43         if( auth.hashesExternal && auth.challenge != session.transport.server.encryptionKey ) {
44             throw new SmbAuthException( SmbException.NT_STATUS_ACCESS_VIOLATION );
45         }
46     }
47
48     int getBatchLimit( byte command ) {
49         return command == SMB_COM_TREE_CONNECT_ANDX ? BATCH_LIMIT : 0;
50     }
51     int writeParameterWordsWireFormat( byte[] dst, int dstIndex ) {
52         int start = dstIndex;
53
54         if( session.transport.server.security == SECURITY_USER &&
55                         ( auth.hashesExternal || auth.password.length() > 0 )) {
56             if( session.transport.server.encryptedPasswords ) {
57                 accountPassword = auth.getAnsiHash( session.transport.server.encryptionKey );
58                 passwordLength = accountPassword.length;
59                 unicodePassword = auth.getUnicodeHash( session.transport.server.encryptionKey );
60                 unicodePasswordLength = unicodePassword.length;
61                 // prohibit HTTP auth attempts for the null session
62
if (unicodePasswordLength == 0 && passwordLength == 0) {
63                     throw new RuntimeException JavaDoc("Null setup prohibited.");
64                 }
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 = useUnicode ? auth.username : 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             if (session.transport.server.encryptedPasswords == false && useUnicode) {
121                 /* Align Unicode plain text password manually
122                  */

123                 if ((( dstIndex - headerStart ) % 2 ) != 0 ) {
124                     dst[dstIndex++] = (byte)'\0';
125                 }
126             }
127             System.arraycopy( unicodePassword, 0, dst, dstIndex, unicodePasswordLength );
128             dstIndex += unicodePasswordLength;
129         }
130
131         dstIndex += writeString( accountName, dst, dstIndex );
132         dstIndex += writeString( primaryDomain, dst, dstIndex );
133         dstIndex += writeString( session.transport.NATIVE_OS, dst, dstIndex );
134         dstIndex += writeString( session.transport.NATIVE_LANMAN, dst, dstIndex );
135
136         return dstIndex - start;
137     }
138     int readParameterWordsWireFormat( byte[] buffer, int bufferIndex ) {
139         return 0;
140     }
141     int readBytesWireFormat( byte[] buffer, int bufferIndex ) {
142         return 0;
143     }
144     public String JavaDoc toString() {
145         String JavaDoc result = new String JavaDoc( "SmbComSessionSetupAndX[" +
146             super.toString() +
147             ",snd_buf_size=" + session.transport.snd_buf_size +
148             ",maxMpxCount=" + session.transport.maxMpxCount +
149             ",VC_NUMBER=" + session.transport.VC_NUMBER +
150             ",sessionKey=" + sessionKey +
151             ",passwordLength=" + passwordLength +
152             ",unicodePasswordLength=" + unicodePasswordLength +
153             ",capabilities=" + session.transport.capabilities +
154             ",accountName=" + accountName +
155             ",primaryDomain=" + primaryDomain +
156             ",NATIVE_OS=" + session.transport.NATIVE_OS +
157             ",NATIVE_LANMAN=" + session.transport.NATIVE_LANMAN + "]" );
158         return result;
159     }
160 }
161
Popular Tags