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 jcifs.smb;
020:
021: import jcifs.Config;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.UnsupportedEncodingException;
025: import jcifs.util.Hexdump;
026:
027: class SmbComTreeConnectAndX extends AndXServerMessageBlock {
028:
029: private static final boolean DISABLE_PLAIN_TEXT_PASSWORDS = Config
030: .getBoolean("jcifs.smb.client.disablePlainTextPasswords",
031: true);
032:
033: private SmbSession session;
034: private boolean disconnectTid = false;
035: private String path, service;
036: private byte[] password;
037: private int passwordLength;
038:
039: /* batchLimits indecies
040: *
041: * 0 = SMB_COM_CHECK_DIRECTORY
042: * 2 = SMB_COM_CREATE_DIRECTORY
043: * 3 = SMB_COM_DELETE
044: * 4 = SMB_COM_DELETE_DIRECTORY
045: * 5 = SMB_COM_OPEN_ANDX
046: * 6 = SMB_COM_RENAME
047: * 7 = SMB_COM_TRANSACTION
048: * 8 = SMB_COM_QUERY_INFORMATION
049: */
050:
051: /* All batch limits are single batch only until further notice
052: */
053:
054: private static byte[] batchLimits = { 1, 1, 1, 1, 1, 1, 1, 1, 0 };
055:
056: static {
057: String s;
058:
059: if ((s = Config
060: .getProperty("jcifs.smb.client.TreeConnectAndX.CheckDirectory")) != null) {
061: batchLimits[0] = Byte.parseByte(s);
062: }
063: if ((s = Config
064: .getProperty("jcifs.smb.client.TreeConnectAndX.CreateDirectory")) != null) {
065: batchLimits[2] = Byte.parseByte(s);
066: }
067: if ((s = Config
068: .getProperty("jcifs.smb.client.TreeConnectAndX.Delete")) != null) {
069: batchLimits[3] = Byte.parseByte(s);
070: }
071: if ((s = Config
072: .getProperty("jcifs.smb.client.TreeConnectAndX.DeleteDirectory")) != null) {
073: batchLimits[4] = Byte.parseByte(s);
074: }
075: if ((s = Config
076: .getProperty("jcifs.smb.client.TreeConnectAndX.OpenAndX")) != null) {
077: batchLimits[5] = Byte.parseByte(s);
078: }
079: if ((s = Config
080: .getProperty("jcifs.smb.client.TreeConnectAndX.Rename")) != null) {
081: batchLimits[6] = Byte.parseByte(s);
082: }
083: if ((s = Config
084: .getProperty("jcifs.smb.client.TreeConnectAndX.Transaction")) != null) {
085: batchLimits[7] = Byte.parseByte(s);
086: }
087: if ((s = Config
088: .getProperty("jcifs.smb.client.TreeConnectAndX.QueryInformation")) != null) {
089: batchLimits[8] = Byte.parseByte(s);
090: }
091: }
092:
093: SmbComTreeConnectAndX(SmbSession session, String path,
094: String service, ServerMessageBlock andx) {
095: super (andx);
096: this .session = session;
097: this .path = path;
098: this .service = service;
099: command = SMB_COM_TREE_CONNECT_ANDX;
100: }
101:
102: int getBatchLimit(byte command) {
103: int c = (int) (command & 0xFF);
104: // why isn't this just return batchLimits[c]?
105: switch (c) {
106: case SMB_COM_CHECK_DIRECTORY:
107: return batchLimits[0];
108: case SMB_COM_CREATE_DIRECTORY:
109: return batchLimits[2];
110: case SMB_COM_DELETE:
111: return batchLimits[3];
112: case SMB_COM_DELETE_DIRECTORY:
113: return batchLimits[4];
114: case SMB_COM_OPEN_ANDX:
115: return batchLimits[5];
116: case SMB_COM_RENAME:
117: return batchLimits[6];
118: case SMB_COM_TRANSACTION:
119: return batchLimits[7];
120: case SMB_COM_QUERY_INFORMATION:
121: return batchLimits[8];
122: }
123: return 0;
124: }
125:
126: int writeParameterWordsWireFormat(byte[] dst, int dstIndex) {
127:
128: if (session.transport.server.security == SECURITY_SHARE
129: && (session.auth.hashesExternal || session.auth.password
130: .length() > 0)) {
131:
132: if (session.transport.server.encryptedPasswords) {
133: // encrypted
134: password = session.auth
135: .getAnsiHash(session.transport.server.encryptionKey);
136: passwordLength = password.length;
137: } else if (DISABLE_PLAIN_TEXT_PASSWORDS) {
138: throw new RuntimeException(
139: "Plain text passwords are disabled");
140: } else {
141: // plain text
142: password = new byte[(session.auth.password.length() + 1) * 2];
143: passwordLength = writeString(session.auth.password,
144: password, 0);
145: }
146: } else {
147: // no password in tree connect
148: passwordLength = 1;
149: }
150:
151: dst[dstIndex++] = disconnectTid ? (byte) 0x01 : (byte) 0x00;
152: dst[dstIndex++] = (byte) 0x00;
153: writeInt2(passwordLength, dst, dstIndex);
154: return 4;
155: }
156:
157: int writeBytesWireFormat(byte[] dst, int dstIndex) {
158: int start = dstIndex;
159:
160: if (session.transport.server.security == SECURITY_SHARE
161: && (session.auth.hashesExternal || session.auth.password
162: .length() > 0)) {
163: System
164: .arraycopy(password, 0, dst, dstIndex,
165: passwordLength);
166: dstIndex += passwordLength;
167: } else {
168: // no password in tree connect
169: dst[dstIndex++] = (byte) 0x00;
170: }
171: dstIndex += writeString(path, dst, dstIndex);
172: try {
173: System.arraycopy(service.getBytes("ASCII"), 0, dst,
174: dstIndex, service.length());
175: } catch (UnsupportedEncodingException uee) {
176: return 0;
177: }
178: dstIndex += service.length();
179: dst[dstIndex++] = (byte) '\0';
180:
181: return dstIndex - start;
182: }
183:
184: int readParameterWordsWireFormat(byte[] buffer, int bufferIndex) {
185: return 0;
186: }
187:
188: int readBytesWireFormat(byte[] buffer, int bufferIndex) {
189: return 0;
190: }
191:
192: int readBytesDirectWireFormat(InputStream in, int byteCount,
193: byte[] buffer, int bufferIndex) throws IOException {
194: return 0;
195: }
196:
197: public String toString() {
198: String result = new String("SmbComTreeConnectAndX["
199: + super .toString() + ",disconnectTid=" + disconnectTid
200: + ",passwordLength=" + passwordLength + ",password="
201: + Hexdump.toHexString(password, passwordLength, 0)
202: + ",path=" + path + ",service=" + service + "]");
203: return result;
204: }
205: }
|