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 java.io.IOException;
023: import java.io.InputStream;
024:
025: class SmbComSessionSetupAndX extends AndXServerMessageBlock {
026:
027: private static final int BATCH_LIMIT = Config.getInt(
028: "jcifs.smb.client.SessionSetupAndX.TreeConnectAndX", 1);
029: private static final boolean DISABLE_PLAIN_TEXT_PASSWORDS = Config
030: .getBoolean("jcifs.smb.client.disablePlainTextPasswords",
031: true);
032:
033: private byte[] accountPassword, unicodePassword;
034: private int passwordLength, unicodePasswordLength;
035: private int sessionKey;
036: private String accountName, primaryDomain;
037:
038: SmbSession session;
039: NtlmPasswordAuthentication auth;
040:
041: SmbComSessionSetupAndX(SmbSession session, ServerMessageBlock andx)
042: throws SmbException {
043: super (andx);
044: command = SMB_COM_SESSION_SETUP_ANDX;
045: this .session = session;
046: this .auth = session.auth;
047: if (auth.hashesExternal
048: && auth.challenge != session.transport.server.encryptionKey) {
049: throw new SmbAuthException(
050: SmbException.NT_STATUS_ACCESS_VIOLATION);
051: }
052: }
053:
054: int getBatchLimit(byte command) {
055: return command == SMB_COM_TREE_CONNECT_ANDX ? BATCH_LIMIT : 0;
056: }
057:
058: int writeParameterWordsWireFormat(byte[] dst, int dstIndex) {
059: int start = dstIndex;
060:
061: if (session.transport.server.security == SECURITY_USER
062: && (auth.hashesExternal || auth.password.length() > 0)) {
063: if (session.transport.server.encryptedPasswords) {
064: // encrypted
065: accountPassword = auth
066: .getAnsiHash(session.transport.server.encryptionKey);
067: unicodePassword = auth
068: .getUnicodeHash(session.transport.server.encryptionKey);
069: passwordLength = unicodePasswordLength = 24;
070: // fix for win9x clients
071: if (unicodePassword.length == 0)
072: unicodePasswordLength = 0;
073: } else if (DISABLE_PLAIN_TEXT_PASSWORDS) {
074: throw new RuntimeException(
075: "Plain text passwords are disabled");
076: } else if (useUnicode) {
077: // plain text
078: String password = auth.getPassword();
079: accountPassword = new byte[0];
080: passwordLength = 0;
081: unicodePassword = new byte[(password.length() + 1) * 2];
082: unicodePasswordLength = writeString(password,
083: unicodePassword, 0);
084: } else {
085: // plain text
086: String password = auth.getPassword();
087: accountPassword = new byte[(password.length() + 1) * 2];
088: passwordLength = writeString(password, accountPassword,
089: 0);
090: unicodePassword = new byte[0];
091: unicodePasswordLength = 0;
092: }
093: } else {
094: // no password in session setup
095: passwordLength = unicodePasswordLength = 0;
096: }
097:
098: sessionKey = session.transport.sessionKey;
099:
100: writeInt2(session.transport.snd_buf_size, dst, dstIndex);
101: dstIndex += 2;
102: writeInt2(session.transport.maxMpxCount, dst, dstIndex);
103: dstIndex += 2;
104: writeInt2(session.transport.VC_NUMBER, dst, dstIndex);
105: dstIndex += 2;
106: writeInt4(sessionKey, dst, dstIndex);
107: dstIndex += 4;
108: writeInt2(passwordLength, dst, dstIndex);
109: dstIndex += 2;
110: writeInt2(unicodePasswordLength, dst, dstIndex);
111: dstIndex += 2;
112: dst[dstIndex++] = (byte) 0x00;
113: dst[dstIndex++] = (byte) 0x00;
114: dst[dstIndex++] = (byte) 0x00;
115: dst[dstIndex++] = (byte) 0x00;
116: writeInt4(session.transport.capabilities, dst, dstIndex);
117: dstIndex += 4;
118:
119: return dstIndex - start;
120: }
121:
122: int writeBytesWireFormat(byte[] dst, int dstIndex) {
123: int start = dstIndex;
124:
125: accountName = auth.username.toUpperCase();
126: primaryDomain = auth.domain.toUpperCase();
127:
128: if (session.transport.server.security == SECURITY_USER
129: && (auth.hashesExternal || auth.password.length() > 0)) {
130: System.arraycopy(accountPassword, 0, dst, dstIndex,
131: passwordLength);
132: dstIndex += passwordLength;
133: System.arraycopy(unicodePassword, 0, dst, dstIndex,
134: unicodePasswordLength);
135: dstIndex += unicodePasswordLength;
136: }
137: if (useUnicode) {
138: // at least NT 4 observed needing this only with unicode
139: dst[dstIndex++] = (byte) '\0';
140: }
141:
142: dstIndex += writeString(accountName, dst, dstIndex);
143: dstIndex += writeString(primaryDomain, dst, dstIndex);
144: dstIndex += writeString(session.transport.NATIVE_OS, dst,
145: dstIndex);
146: dstIndex += writeString(session.transport.NATIVE_LANMAN, dst,
147: dstIndex);
148:
149: return dstIndex - start;
150: }
151:
152: int readParameterWordsWireFormat(byte[] buffer, int bufferIndex) {
153: return 0;
154: }
155:
156: int readBytesWireFormat(byte[] buffer, int bufferIndex) {
157: return 0;
158: }
159:
160: int readBytesDirectWireFormat(InputStream in, int byteCount,
161: byte[] buffer, int bufferIndex) throws IOException {
162: return 0;
163: }
164:
165: public String toString() {
166: String result = new String("SmbComSessionSetupAndX["
167: + super .toString() + ",snd_buf_size="
168: + session.transport.snd_buf_size + ",maxMpxCount="
169: + session.transport.maxMpxCount + ",VC_NUMBER="
170: + session.transport.VC_NUMBER + ",sessionKey="
171: + sessionKey + ",passwordLength=" + passwordLength
172: + ",unicodePasswordLength=" + unicodePasswordLength
173: + ",capabilities=" + session.transport.capabilities
174: + ",accountName=" + accountName + ",primaryDomain="
175: + primaryDomain + ",NATIVE_OS="
176: + session.transport.NATIVE_OS + ",NATIVE_LANMAN="
177: + session.transport.NATIVE_LANMAN + "]");
178: return result;
179: }
180: }
|