KickJava   Java API By Example, From Geeks To Geeks.

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

47
48     /* All batch limits are single batch only until further notice
49      */

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