/
SmbComTreeConnectAndX.java
187 lines (162 loc) · 6.61 KB
/
SmbComTreeConnectAndX.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* jcifs smb client library in Java
* Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package jcifs.internal.smb1.com;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import jcifs.CIFSContext;
import jcifs.Configuration;
import jcifs.RuntimeCIFSException;
import jcifs.SmbConstants;
import jcifs.internal.smb1.AndXServerMessageBlock;
import jcifs.internal.smb1.ServerMessageBlock;
import jcifs.internal.util.SMBUtil;
import jcifs.smb.NtlmPasswordAuthenticator;
import jcifs.util.Hexdump;
/**
*
*/
public class SmbComTreeConnectAndX extends AndXServerMessageBlock {
private boolean disconnectTid = false;
private String service;
private byte[] password;
private int passwordLength;
private CIFSContext ctx;
private ServerData server;
/**
*
* @param ctx
* @param server
* @param path
* @param service
* @param andx
*/
public SmbComTreeConnectAndX ( CIFSContext ctx, ServerData server, String path, String service, ServerMessageBlock andx ) {
super(ctx.getConfig(), SMB_COM_TREE_CONNECT_ANDX, andx);
this.ctx = ctx;
this.server = server;
this.path = path;
this.service = service;
}
@Override
protected int getBatchLimit ( Configuration cfg, byte cmd ) {
int c = cmd & 0xFF;
switch ( c ) {
case SMB_COM_CHECK_DIRECTORY:
return cfg.getBatchLimit("TreeConnectAndX.CheckDirectory");
case SMB_COM_CREATE_DIRECTORY:
return cfg.getBatchLimit("TreeConnectAndX.CreateDirectory");
case SMB_COM_DELETE:
return cfg.getBatchLimit("TreeConnectAndX.Delete");
case SMB_COM_DELETE_DIRECTORY:
return cfg.getBatchLimit("TreeConnectAndX.DeleteDirectory");
case SMB_COM_OPEN_ANDX:
return cfg.getBatchLimit("TreeConnectAndX.OpenAndX");
case SMB_COM_RENAME:
return cfg.getBatchLimit("TreeConnectAndX.Rename");
case SMB_COM_TRANSACTION:
return cfg.getBatchLimit("TreeConnectAndX.Transaction");
case SMB_COM_QUERY_INFORMATION:
return cfg.getBatchLimit("TreeConnectAndX.QueryInformation");
}
return 0;
}
@Override
protected int writeParameterWordsWireFormat ( byte[] dst, int dstIndex ) {
if ( this.server.security == SmbConstants.SECURITY_SHARE && this.ctx.getCredentials() instanceof NtlmPasswordAuthenticator ) {
NtlmPasswordAuthenticator pwAuth = (NtlmPasswordAuthenticator) this.ctx.getCredentials();
if ( isExternalAuth(pwAuth) ) {
this.passwordLength = 1;
}
else if ( this.server.encryptedPasswords ) {
// encrypted
try {
this.password = pwAuth.getAnsiHash(this.ctx, this.server.encryptionKey);
}
catch ( GeneralSecurityException e ) {
throw new RuntimeCIFSException("Failed to encrypt password", e);
}
this.passwordLength = this.password.length;
}
else if ( this.ctx.getConfig().isDisablePlainTextPasswords() ) {
throw new RuntimeCIFSException("Plain text passwords are disabled");
}
else {
// plain text
this.password = new byte[ ( pwAuth.getPassword().length() + 1 ) * 2];
this.passwordLength = writeString(pwAuth.getPassword(), this.password, 0);
}
}
else {
// no password in tree connect
this.passwordLength = 1;
}
dst[ dstIndex++ ] = this.disconnectTid ? (byte) 0x01 : (byte) 0x00;
dst[ dstIndex++ ] = (byte) 0x00;
SMBUtil.writeInt2(this.passwordLength, dst, dstIndex);
return 4;
}
@SuppressWarnings ( "deprecation" )
private static boolean isExternalAuth ( NtlmPasswordAuthenticator pwAuth ) {
return pwAuth instanceof jcifs.smb.NtlmPasswordAuthentication && ! ( (jcifs.smb.NtlmPasswordAuthentication) pwAuth ).areHashesExternal()
&& pwAuth.getPassword().isEmpty();
}
@Override
protected int writeBytesWireFormat ( byte[] dst, int dstIndex ) {
int start = dstIndex;
if ( this.server.security == SmbConstants.SECURITY_SHARE && this.ctx.getCredentials() instanceof NtlmPasswordAuthenticator ) {
NtlmPasswordAuthenticator pwAuth = (NtlmPasswordAuthenticator) this.ctx.getCredentials();
if ( isExternalAuth(pwAuth) ) {
dst[ dstIndex++ ] = (byte) 0x00;
}
else {
System.arraycopy(this.password, 0, dst, dstIndex, this.passwordLength);
dstIndex += this.passwordLength;
}
}
else {
// no password in tree connect
dst[ dstIndex++ ] = (byte) 0x00;
}
dstIndex += writeString(this.path, dst, dstIndex);
try {
System.arraycopy(this.service.getBytes("ASCII"), 0, dst, dstIndex, this.service.length());
}
catch ( UnsupportedEncodingException uee ) {
return 0;
}
dstIndex += this.service.length();
dst[ dstIndex++ ] = (byte) '\0';
return dstIndex - start;
}
@Override
protected int readParameterWordsWireFormat ( byte[] buffer, int bufferIndex ) {
return 0;
}
@Override
protected int readBytesWireFormat ( byte[] buffer, int bufferIndex ) {
return 0;
}
@Override
public String toString () {
String result = new String(
"SmbComTreeConnectAndX[" + super.toString() + ",disconnectTid=" + this.disconnectTid + ",passwordLength=" + this.passwordLength
+ ",password=" + Hexdump.toHexString(this.password, this.passwordLength, 0) + ",path=" + this.path + ",service=" + this.service
+ "]");
return result;
}
}