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:
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: accountPassword = auth
065: .getAnsiHash(session.transport.server.encryptionKey);
066: passwordLength = accountPassword.length;
067: unicodePassword = auth
068: .getUnicodeHash(session.transport.server.encryptionKey);
069: unicodePasswordLength = unicodePassword.length;
070: // prohibit HTTP auth attempts for the null session
071: if (unicodePasswordLength == 0 && passwordLength == 0) {
072: throw new RuntimeException("Null setup prohibited.");
073: }
074: } else if (DISABLE_PLAIN_TEXT_PASSWORDS) {
075: throw new RuntimeException(
076: "Plain text passwords are disabled");
077: } else if (useUnicode) {
078: // plain text
079: String password = auth.getPassword();
080: accountPassword = new byte[0];
081: passwordLength = 0;
082: unicodePassword = new byte[(password.length() + 1) * 2];
083: unicodePasswordLength = writeString(password,
084: unicodePassword, 0);
085: } else {
086: // plain text
087: String password = auth.getPassword();
088: accountPassword = new byte[(password.length() + 1) * 2];
089: passwordLength = writeString(password, accountPassword,
090: 0);
091: unicodePassword = new byte[0];
092: unicodePasswordLength = 0;
093: }
094: } else {
095: // no password in session setup
096: passwordLength = unicodePasswordLength = 0;
097: }
098:
099: sessionKey = session.transport.sessionKey;
100:
101: writeInt2(session.transport.snd_buf_size, dst, dstIndex);
102: dstIndex += 2;
103: writeInt2(session.transport.maxMpxCount, dst, dstIndex);
104: dstIndex += 2;
105: writeInt2(session.transport.VC_NUMBER, dst, dstIndex);
106: dstIndex += 2;
107: writeInt4(sessionKey, dst, dstIndex);
108: dstIndex += 4;
109: writeInt2(passwordLength, dst, dstIndex);
110: dstIndex += 2;
111: writeInt2(unicodePasswordLength, dst, dstIndex);
112: dstIndex += 2;
113: dst[dstIndex++] = (byte) 0x00;
114: dst[dstIndex++] = (byte) 0x00;
115: dst[dstIndex++] = (byte) 0x00;
116: dst[dstIndex++] = (byte) 0x00;
117: writeInt4(session.transport.capabilities, dst, dstIndex);
118: dstIndex += 4;
119:
120: return dstIndex - start;
121: }
122:
123: int writeBytesWireFormat(byte[] dst, int dstIndex) {
124: int start = dstIndex;
125:
126: accountName = useUnicode ? auth.username : auth.username
127: .toUpperCase();
128: primaryDomain = auth.domain.toUpperCase();
129:
130: if (session.transport.server.security == SECURITY_USER
131: && (auth.hashesExternal || auth.password.length() > 0)) {
132: System.arraycopy(accountPassword, 0, dst, dstIndex,
133: passwordLength);
134: dstIndex += passwordLength;
135: System.arraycopy(unicodePassword, 0, dst, dstIndex,
136: unicodePasswordLength);
137: dstIndex += unicodePasswordLength;
138: }
139: if (useUnicode) {
140: // at least NT 4 observed needing this only with unicode
141: dst[dstIndex++] = (byte) '\0';
142: }
143:
144: dstIndex += writeString(accountName, dst, dstIndex);
145: dstIndex += writeString(primaryDomain, dst, dstIndex);
146: dstIndex += writeString(session.transport.NATIVE_OS, dst,
147: dstIndex);
148: dstIndex += writeString(session.transport.NATIVE_LANMAN, dst,
149: dstIndex);
150:
151: return dstIndex - start;
152: }
153:
154: int readParameterWordsWireFormat(byte[] buffer, int bufferIndex) {
155: return 0;
156: }
157:
158: int readBytesWireFormat(byte[] buffer, int bufferIndex) {
159: return 0;
160: }
161:
162: int readBytesDirectWireFormat(InputStream in, int byteCount,
163: byte[] buffer, int bufferIndex) throws IOException {
164: return 0;
165: }
166:
167: public String toString() {
168: String result = new String("SmbComSessionSetupAndX["
169: + super .toString() + ",snd_buf_size="
170: + session.transport.snd_buf_size + ",maxMpxCount="
171: + session.transport.maxMpxCount + ",VC_NUMBER="
172: + session.transport.VC_NUMBER + ",sessionKey="
173: + sessionKey + ",passwordLength=" + passwordLength
174: + ",unicodePasswordLength=" + unicodePasswordLength
175: + ",capabilities=" + session.transport.capabilities
176: + ",accountName=" + accountName + ",primaryDomain="
177: + primaryDomain + ",NATIVE_OS="
178: + session.transport.NATIVE_OS + ",NATIVE_LANMAN="
179: + session.transport.NATIVE_LANMAN + "]");
180: return result;
181: }
182: }
|