/
SmbEnumerationUtil.java
364 lines (309 loc) · 12.5 KB
/
SmbEnumerationUtil.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* © 2017 AgNO3 Gmbh & Co. KG
*
* 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.smb;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jcifs.Address;
import jcifs.CIFSContext;
import jcifs.CIFSException;
import jcifs.CloseableIterator;
import jcifs.ResourceFilter;
import jcifs.ResourceNameFilter;
import jcifs.SmbConstants;
import jcifs.SmbResource;
import jcifs.SmbResourceLocator;
import jcifs.dcerpc.DcerpcException;
import jcifs.dcerpc.DcerpcHandle;
import jcifs.dcerpc.msrpc.MsrpcDfsRootEnum;
import jcifs.dcerpc.msrpc.MsrpcShareEnum;
import jcifs.internal.smb1.net.NetShareEnum;
import jcifs.internal.smb1.net.NetShareEnumResponse;
import jcifs.internal.smb1.trans.SmbComTransaction;
import jcifs.internal.smb1.trans.SmbComTransactionResponse;
/**
* @author mbechler
*
*/
final class SmbEnumerationUtil {
private static final Logger log = LoggerFactory.getLogger(SmbEnumerationUtil.class);
/**
*
*/
private SmbEnumerationUtil () {}
private static DcerpcHandle getHandle ( CIFSContext ctx, SmbResourceLocator loc, Address address, String ep )
throws MalformedURLException, DcerpcException {
return DcerpcHandle.getHandle(String.format("ncacn_np:%s[endpoint=%s,address=%s]", loc.getServer(), ep, address.getHostAddress()), ctx);
}
static FileEntry[] doDfsRootEnum ( CIFSContext ctx, SmbResourceLocator loc, Address address ) throws IOException {
try ( DcerpcHandle handle = getHandle(ctx, loc, address, "\\PIPE\\netdfs") ) {
MsrpcDfsRootEnum rpc = new MsrpcDfsRootEnum(loc.getServer());
handle.sendrecv(rpc);
if ( rpc.retval != 0 ) {
throw new SmbException(rpc.retval, true);
}
return rpc.getEntries();
}
}
static FileEntry[] doMsrpcShareEnum ( CIFSContext ctx, SmbResourceLocator loc, Address address ) throws IOException {
try ( DcerpcHandle handle = getHandle(ctx, loc, address, "\\PIPE\\srvsvc") ) {
MsrpcShareEnum rpc = new MsrpcShareEnum(loc.getServer());
handle.sendrecv(rpc);
if ( rpc.retval != 0 ) {
throw new SmbException(rpc.retval, true);
}
return rpc.getEntries();
}
}
static FileEntry[] doNetShareEnum ( SmbTreeHandleImpl th ) throws CIFSException {
SmbComTransaction req = new NetShareEnum(th.getConfig());
SmbComTransactionResponse resp = new NetShareEnumResponse(th.getConfig());
th.send(req, resp);
if ( resp.getStatus() != WinError.ERROR_SUCCESS )
throw new SmbException(resp.getStatus(), true);
return resp.getResults();
}
static CloseableIterator<SmbResource> doShareEnum ( SmbFile parent, String wildcard, int searchAttributes, ResourceNameFilter fnf,
ResourceFilter ff ) throws CIFSException {
// clone the locator so that the address index is not modified
SmbResourceLocatorImpl locator = parent.fileLocator.clone();
CIFSContext tc = parent.getContext();
URL u = locator.getURL();
FileEntry[] entries;
if ( u.getPath().lastIndexOf('/') != ( u.getPath().length() - 1 ) )
throw new SmbException(u.toString() + " directory must end with '/'");
if ( locator.getType() != SmbConstants.TYPE_SERVER )
throw new SmbException("The requested list operations is invalid: " + u.toString());
Set<FileEntry> set = new HashSet<>();
if ( tc.getDfs().isTrustedDomain(tc, locator.getServer()) ) {
/*
* The server name is actually the name of a trusted
* domain. Add DFS roots to the list.
*/
try {
entries = doDfsRootEnum(tc, locator, locator.getAddress());
for ( int ei = 0; ei < entries.length; ei++ ) {
FileEntry e = entries[ ei ];
if ( !set.contains(e) && ( fnf == null || fnf.accept(parent, e.getName()) ) ) {
set.add(e);
}
}
}
catch ( IOException ioe ) {
log.debug("DS enumeration failed", ioe);
}
}
SmbTreeConnection treeConn = SmbTreeConnection.create(tc);
try ( SmbTreeHandleImpl th = treeConn.connectHost(locator, locator.getServerWithDfs());
SmbSessionImpl session = th.getSession();
SmbTransportImpl transport = session.getTransport() ) {
try {
entries = doMsrpcShareEnum(tc, locator, transport.getRemoteAddress());
}
catch ( IOException ioe ) {
if ( th.isSMB2() ) {
throw ioe;
}
log.debug("doMsrpcShareEnum failed", ioe);
entries = doNetShareEnum(th);
}
for ( int ei = 0; ei < entries.length; ei++ ) {
FileEntry e = entries[ ei ];
if ( !set.contains(e) && ( fnf == null || fnf.accept(parent, e.getName()) ) ) {
set.add(e);
}
}
}
catch ( SmbException e ) {
throw e;
}
catch ( IOException ioe ) {
log.debug("doNetShareEnum failed", ioe);
throw new SmbException(u.toString(), ioe);
}
return new ShareEnumIterator(parent, set.iterator(), ff);
}
@SuppressWarnings ( "resource" )
static CloseableIterator<SmbResource> doEnum ( SmbFile parent, String wildcard, int searchAttributes, ResourceNameFilter fnf, ResourceFilter ff )
throws CIFSException {
DosFileFilter dff = unwrapDOSFilter(ff);
if ( dff != null ) {
if ( dff.wildcard != null )
wildcard = dff.wildcard;
searchAttributes = dff.attributes;
}
SmbResourceLocator locator = parent.getLocator();
if ( locator.getURL().getHost().isEmpty() ) {
// smb:// -> enumerate servers through browsing
Address addr;
try {
addr = locator.getAddress();
}
catch ( CIFSException e ) {
if ( e.getCause() instanceof UnknownHostException ) {
log.debug("Failed to find master browser", e);
throw new SmbUnsupportedOperationException();
}
throw e;
}
try ( SmbFile browser = (SmbFile) parent.resolve(addr.getHostAddress()) ) {
try ( SmbTreeHandleImpl th = browser.ensureTreeConnected() ) {
if ( th.isSMB2() ) {
throw new SmbUnsupportedOperationException();
}
return new NetServerFileEntryAdapterIterator(parent, new NetServerEnumIterator(parent, th, wildcard, searchAttributes, fnf), ff);
}
}
}
else if ( locator.getType() == SmbConstants.TYPE_WORKGROUP ) {
try ( SmbTreeHandleImpl th = parent.ensureTreeConnected() ) {
if ( th.isSMB2() ) {
throw new SmbUnsupportedOperationException();
}
return new NetServerFileEntryAdapterIterator(parent, new NetServerEnumIterator(parent, th, wildcard, searchAttributes, fnf), ff);
}
}
else if ( locator.isRoot() ) {
return doShareEnum(parent, wildcard, searchAttributes, fnf, ff);
}
try ( SmbTreeHandleImpl th = parent.ensureTreeConnected() ) {
if ( th.isSMB2() ) {
return new DirFileEntryAdapterIterator(parent, new DirFileEntryEnumIterator2(th, parent, wildcard, fnf, searchAttributes), ff);
}
return new DirFileEntryAdapterIterator(parent, new DirFileEntryEnumIterator1(th, parent, wildcard, fnf, searchAttributes), ff);
}
}
private static DosFileFilter unwrapDOSFilter ( ResourceFilter ff ) {
if ( ff instanceof ResourceFilterWrapper ) {
SmbFileFilter sff = ( (ResourceFilterWrapper) ff ).getFileFilter();
if ( sff instanceof DosFileFilter ) {
return (DosFileFilter) sff;
}
}
return null;
}
static String[] list ( SmbFile root, String wildcard, int searchAttributes, final SmbFilenameFilter fnf, final SmbFileFilter ff )
throws SmbException {
try ( CloseableIterator<SmbResource> it = doEnum(root, wildcard, searchAttributes, fnf == null ? null : new ResourceNameFilter() {
@Override
public boolean accept ( SmbResource parent, String name ) throws CIFSException {
if ( ! ( parent instanceof SmbFile ) ) {
return false;
}
return fnf.accept((SmbFile) parent, name);
}
}, ff == null ? null : new ResourceFilter() {
@Override
public boolean accept ( SmbResource resource ) throws CIFSException {
if ( ! ( resource instanceof SmbFile ) ) {
return false;
}
return ff.accept((SmbFile) resource);
}
}) ) {
List<String> list = new ArrayList<>();
while ( it.hasNext() ) {
try ( SmbResource n = it.next() ) {
list.add(n.getName());
}
}
return list.toArray(new String[list.size()]);
}
catch ( CIFSException e ) {
throw SmbException.wrap(e);
}
}
static SmbFile[] listFiles ( SmbFile root, String wildcard, int searchAttributes, final SmbFilenameFilter fnf, final SmbFileFilter ff )
throws SmbException {
try ( CloseableIterator<SmbResource> it = doEnum(
root,
wildcard,
searchAttributes,
fnf == null ? null : new ResourceNameFilterWrapper(fnf),
ff == null ? null : new ResourceFilterWrapper(ff)) ) {
List<SmbFile> list = new ArrayList<>();
while ( it.hasNext() ) {
try ( SmbResource n = it.next() ) {
if ( n instanceof SmbFile ) {
list.add((SmbFile) n);
}
}
}
return list.toArray(new SmbFile[list.size()]);
}
catch ( CIFSException e ) {
throw SmbException.wrap(e);
}
}
/**
* @author mbechler
*
*/
private static final class ResourceFilterWrapper implements ResourceFilter {
/**
*
*/
private final SmbFileFilter ff;
/**
* @param ff
*/
ResourceFilterWrapper ( SmbFileFilter ff ) {
this.ff = ff;
}
SmbFileFilter getFileFilter () {
return this.ff;
}
@Override
public boolean accept ( SmbResource resource ) throws CIFSException {
if ( ! ( resource instanceof SmbFile ) ) {
return false;
}
return this.ff.accept((SmbFile) resource);
}
}
/**
* @author mbechler
*
*/
private static final class ResourceNameFilterWrapper implements ResourceNameFilter {
/**
*
*/
private final SmbFilenameFilter fnf;
/**
* @param fnf
*/
ResourceNameFilterWrapper ( SmbFilenameFilter fnf ) {
this.fnf = fnf;
}
@Override
public boolean accept ( SmbResource parent, String name ) throws CIFSException {
if ( ! ( parent instanceof SmbFile ) ) {
return false;
}
return this.fnf.accept((SmbFile) parent, name);
}
}
}