KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.knowgate.misc.Gadgets;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
27
28 class SmbComTreeConnectAndX extends AndXServerMessageBlock {
29
30     private static final boolean DISABLE_PLAIN_TEXT_PASSWORDS =
31             Config.getBoolean( "jcifs.smb.client.disablePlainTextPasswords", true );
32
33     private SmbSession session;
34     private boolean disconnectTid = false;
35     private String JavaDoc path, service;
36     private byte[] password;
37     private int passwordLength;
38
39     /* batchLimits indecies
40      *
41      * 0 = SMB_COM_CHECK_DIRECTORY
42      * 2 = SMB_COM_CREATE_DIRECTORY
43      * 3 = SMB_COM_DELETE
44      * 4 = SMB_COM_DELETE_DIRECTORY
45      * 5 = SMB_COM_OPEN_ANDX
46      * 6 = SMB_COM_RENAME
47      * 7 = SMB_COM_TRANSACTION
48      * 8 = SMB_COM_QUERY_INFORMATION
49      */

50
51     /* All batch limits are single batch only until further notice
52      */

53
54     private static byte[] batchLimits = {
55         1, 1, 1, 1, 1, 1, 1, 1, 0
56     };
57
58     static {
59         String JavaDoc s;
60
61         if(( s = Config.getProperty( "jcifs.smb.client.TreeConnectAndX.CheckDirectory" )) != null ) {
62             batchLimits[0] = Byte.parseByte( s );
63         }
64         if(( s = Config.getProperty( "jcifs.smb.client.TreeConnectAndX.CreateDirectory" )) != null ) {
65             batchLimits[2] = Byte.parseByte( s );
66         }
67         if(( s = Config.getProperty( "jcifs.smb.client.TreeConnectAndX.Delete" )) != null ) {
68             batchLimits[3] = Byte.parseByte( s );
69         }
70         if(( s = Config.getProperty( "jcifs.smb.client.TreeConnectAndX.DeleteDirectory" )) != null ) {
71             batchLimits[4] = Byte.parseByte( s );
72         }
73         if(( s = Config.getProperty( "jcifs.smb.client.TreeConnectAndX.OpenAndX" )) != null ) {
74             batchLimits[5] = Byte.parseByte( s );
75         }
76         if(( s = Config.getProperty( "jcifs.smb.client.TreeConnectAndX.Rename" )) != null ) {
77             batchLimits[6] = Byte.parseByte( s );
78         }
79         if(( s = Config.getProperty( "jcifs.smb.client.TreeConnectAndX.Transaction" )) != null ) {
80             batchLimits[7] = Byte.parseByte( s );
81         }
82         if(( s = Config.getProperty( "jcifs.smb.client.TreeConnectAndX.QueryInformation" )) != null ) {
83             batchLimits[8] = Byte.parseByte( s );
84         }
85     }
86
87     SmbComTreeConnectAndX( SmbSession session, String JavaDoc path,
88                                 String JavaDoc service, ServerMessageBlock andx ) {
89         super( andx );
90         this.session = session;
91         this.path = path;
92         this.service = service;
93         command = SMB_COM_TREE_CONNECT_ANDX;
94     }
95
96     int getBatchLimit( byte command ) {
97         int c = (int)( command & 0xFF );
98         // why isn't this just return batchLimits[c]?
99
switch( c ) {
100             case SMB_COM_CHECK_DIRECTORY:
101                 return batchLimits[0];
102             case SMB_COM_CREATE_DIRECTORY:
103                 return batchLimits[2];
104             case SMB_COM_DELETE:
105                 return batchLimits[3];
106             case SMB_COM_DELETE_DIRECTORY:
107                 return batchLimits[4];
108             case SMB_COM_OPEN_ANDX:
109                 return batchLimits[5];
110             case SMB_COM_RENAME:
111                 return batchLimits[6];
112             case SMB_COM_TRANSACTION:
113                 return batchLimits[7];
114             case SMB_COM_QUERY_INFORMATION:
115                 return batchLimits[8];
116         }
117         return 0;
118     }
119
120     int writeParameterWordsWireFormat( byte[] dst, int dstIndex ) {
121
122         if( session.transport.server.security == SECURITY_SHARE &&
123                         ( session.auth.hashesExternal ||
124                         session.auth.password.length() > 0 )) {
125
126             if( session.transport.server.encryptedPasswords ) {
127                 // encrypted
128
password = session.auth.getAnsiHash( session.transport.server.encryptionKey );
129                 passwordLength = password.length;
130             } else if( DISABLE_PLAIN_TEXT_PASSWORDS ) {
131                 throw new RuntimeException JavaDoc( "Plain text passwords are disabled" );
132             } else {
133                 // plain text
134
password = new byte[(session.auth.password.length() + 1) * 2];
135                 passwordLength = writeString( session.auth.password, password, 0 );
136             }
137         } else {
138             // no password in tree connect
139
passwordLength = 1;
140         }
141
142         dst[dstIndex++] = disconnectTid ? (byte)0x01 : (byte)0x00;
143         dst[dstIndex++] = (byte)0x00;
144         writeInt2( passwordLength, dst, dstIndex );
145         return 4;
146     }
147     int writeBytesWireFormat( byte[] dst, int dstIndex ) {
148         int start = dstIndex;
149
150         if( session.transport.server.security == SECURITY_SHARE &&
151                         ( session.auth.hashesExternal ||
152                         session.auth.password.length() > 0 )) {
153             System.arraycopy( password, 0, dst, dstIndex, passwordLength );
154             dstIndex += passwordLength;
155         } else {
156             // no password in tree connect
157
dst[dstIndex++] = (byte)0x00;
158         }
159         dstIndex += writeString( path, dst, dstIndex );
160         try {
161             System.arraycopy( service.getBytes( "ASCII" ), 0, dst, dstIndex, service.length() );
162         } catch( UnsupportedEncodingException JavaDoc uee ) {
163             return 0;
164         }
165         dstIndex += service.length();
166         dst[dstIndex++] = (byte)'\0';
167
168         return dstIndex - start;
169     }
170     int readParameterWordsWireFormat( byte[] buffer, int bufferIndex ) {
171         return 0;
172     }
173     int readBytesWireFormat( byte[] buffer, int bufferIndex ) {
174         return 0;
175     }
176     int readBytesDirectWireFormat( InputStream JavaDoc in, int byteCount,
177                 byte[] buffer, int bufferIndex ) throws IOException JavaDoc {
178         return 0;
179     }
180     public String JavaDoc toString() {
181         String JavaDoc result = new String JavaDoc( "SmbComTreeConnectAndX[" +
182             super.toString() +
183             ",disconnectTid=" + disconnectTid +
184             ",passwordLength=" + passwordLength +
185             ",password=" + Gadgets.toHexString( password, passwordLength, 0 ) +
186             ",path=" + path +
187             ",service=" + service + "]" );
188         return result;
189     }
190 }
191
192
Popular Tags