001: /* jcifs smb client library in Java
002: * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.knowgate.jcifs.smb;
020:
021: import com.knowgate.jcifs.Config;
022: import com.knowgate.misc.Gadgets;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.UnsupportedEncodingException;
027:
028: class SmbComTreeConnectAndX extends AndXServerMessageBlock {
029:
030: private static final boolean DISABLE_PLAIN_TEXT_PASSWORDS = Config
031: .getBoolean("jcifs.smb.client.disablePlainTextPasswords",
032: true);
033:
034: private SmbSession session;
035: private boolean disconnectTid = false;
036: private String path, service;
037: private byte[] password;
038: private int passwordLength;
039:
040: /* batchLimits indecies
041: *
042: * 0 = SMB_COM_CHECK_DIRECTORY
043: * 2 = SMB_COM_CREATE_DIRECTORY
044: * 3 = SMB_COM_DELETE
045: * 4 = SMB_COM_DELETE_DIRECTORY
046: * 5 = SMB_COM_OPEN_ANDX
047: * 6 = SMB_COM_RENAME
048: * 7 = SMB_COM_TRANSACTION
049: * 8 = SMB_COM_QUERY_INFORMATION
050: */
051:
052: /* All batch limits are single batch only until further notice
053: */
054:
055: private static byte[] batchLimits = { 1, 1, 1, 1, 1, 1, 1, 1, 0 };
056:
057: static {
058: String s;
059:
060: if ((s = Config
061: .getProperty("jcifs.smb.client.TreeConnectAndX.CheckDirectory")) != null) {
062: batchLimits[0] = Byte.parseByte(s);
063: }
064: if ((s = Config
065: .getProperty("jcifs.smb.client.TreeConnectAndX.CreateDirectory")) != null) {
066: batchLimits[2] = Byte.parseByte(s);
067: }
068: if ((s = Config
069: .getProperty("jcifs.smb.client.TreeConnectAndX.Delete")) != null) {
070: batchLimits[3] = Byte.parseByte(s);
071: }
072: if ((s = Config
073: .getProperty("jcifs.smb.client.TreeConnectAndX.DeleteDirectory")) != null) {
074: batchLimits[4] = Byte.parseByte(s);
075: }
076: if ((s = Config
077: .getProperty("jcifs.smb.client.TreeConnectAndX.OpenAndX")) != null) {
078: batchLimits[5] = Byte.parseByte(s);
079: }
080: if ((s = Config
081: .getProperty("jcifs.smb.client.TreeConnectAndX.Rename")) != null) {
082: batchLimits[6] = Byte.parseByte(s);
083: }
084: if ((s = Config
085: .getProperty("jcifs.smb.client.TreeConnectAndX.Transaction")) != null) {
086: batchLimits[7] = Byte.parseByte(s);
087: }
088: if ((s = Config
089: .getProperty("jcifs.smb.client.TreeConnectAndX.QueryInformation")) != null) {
090: batchLimits[8] = Byte.parseByte(s);
091: }
092: }
093:
094: SmbComTreeConnectAndX(SmbSession session, String path,
095: String service, ServerMessageBlock andx) {
096: super (andx);
097: this .session = session;
098: this .path = path;
099: this .service = service;
100: command = SMB_COM_TREE_CONNECT_ANDX;
101: }
102:
103: int getBatchLimit(byte command) {
104: int c = (int) (command & 0xFF);
105: // why isn't this just return batchLimits[c]?
106: switch (c) {
107: case SMB_COM_CHECK_DIRECTORY:
108: return batchLimits[0];
109: case SMB_COM_CREATE_DIRECTORY:
110: return batchLimits[2];
111: case SMB_COM_DELETE:
112: return batchLimits[3];
113: case SMB_COM_DELETE_DIRECTORY:
114: return batchLimits[4];
115: case SMB_COM_OPEN_ANDX:
116: return batchLimits[5];
117: case SMB_COM_RENAME:
118: return batchLimits[6];
119: case SMB_COM_TRANSACTION:
120: return batchLimits[7];
121: case SMB_COM_QUERY_INFORMATION:
122: return batchLimits[8];
123: }
124: return 0;
125: }
126:
127: int writeParameterWordsWireFormat(byte[] dst, int dstIndex) {
128:
129: if (session.transport.server.security == SECURITY_SHARE
130: && (session.auth.hashesExternal || session.auth.password
131: .length() > 0)) {
132:
133: if (session.transport.server.encryptedPasswords) {
134: // encrypted
135: password = session.auth
136: .getAnsiHash(session.transport.server.encryptionKey);
137: passwordLength = password.length;
138: } else if (DISABLE_PLAIN_TEXT_PASSWORDS) {
139: throw new RuntimeException(
140: "Plain text passwords are disabled");
141: } else {
142: // plain text
143: password = new byte[(session.auth.password.length() + 1) * 2];
144: passwordLength = writeString(session.auth.password,
145: password, 0);
146: }
147: } else {
148: // no password in tree connect
149: passwordLength = 1;
150: }
151:
152: dst[dstIndex++] = disconnectTid ? (byte) 0x01 : (byte) 0x00;
153: dst[dstIndex++] = (byte) 0x00;
154: writeInt2(passwordLength, dst, dstIndex);
155: return 4;
156: }
157:
158: int writeBytesWireFormat(byte[] dst, int dstIndex) {
159: int start = dstIndex;
160:
161: if (session.transport.server.security == SECURITY_SHARE
162: && (session.auth.hashesExternal || session.auth.password
163: .length() > 0)) {
164: System
165: .arraycopy(password, 0, dst, dstIndex,
166: passwordLength);
167: dstIndex += passwordLength;
168: } else {
169: // no password in tree connect
170: dst[dstIndex++] = (byte) 0x00;
171: }
172: dstIndex += writeString(path, dst, dstIndex);
173: try {
174: System.arraycopy(service.getBytes("ASCII"), 0, dst,
175: dstIndex, service.length());
176: } catch (UnsupportedEncodingException uee) {
177: return 0;
178: }
179: dstIndex += service.length();
180: dst[dstIndex++] = (byte) '\0';
181:
182: return dstIndex - start;
183: }
184:
185: int readParameterWordsWireFormat(byte[] buffer, int bufferIndex) {
186: return 0;
187: }
188:
189: int readBytesWireFormat(byte[] buffer, int bufferIndex) {
190: return 0;
191: }
192:
193: int readBytesDirectWireFormat(InputStream in, int byteCount,
194: byte[] buffer, int bufferIndex) throws IOException {
195: return 0;
196: }
197:
198: public String toString() {
199: String result = new String("SmbComTreeConnectAndX["
200: + super .toString() + ",disconnectTid=" + disconnectTid
201: + ",passwordLength=" + passwordLength + ",password="
202: + Gadgets.toHexString(password, passwordLength, 0)
203: + ",path=" + path + ",service=" + service + "]");
204: return result;
205: }
206: }
|