Skip to content

Commit

Permalink
Merge branch 'AgNO3:master' into Add_symlink_functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBragg authored Feb 1, 2023
2 parents 4504c60 + 4d45352 commit 2a52d08
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 39 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
jcifs-ng 2.1.9
- Properly set broadcast flag when NetBIOS name requests (#314)
- Fix default credential initialization (#324)
- Properly initialize AndX respones, guard against NPE (#321)
- Fix AndX SMB1 batching configuration, disable by default
- Fixup short names in root DFS referral, if enabled

jcifs-ng 2.1.8
- Relax SMB2 tree id check, all but -1 are valid

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Latest stable release:
<dependency>
<groupId>eu.agno3.jcifs</groupId>
<artifactId>jcifs-ng</artifactId>
<version>2.1.8</version>
<version>2.1.9</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>jcifs-ng</artifactId>
<groupId>eu.agno3.jcifs</groupId>
<packaging>bundle</packaging>
<version>2.1.9-SNAPSHOT</version>
<version>2.1.10-SNAPSHOT</version>
<description>A pure-java CIFS/SMB client library</description>
<url>https://github.com/AgNO3/jcifs-ng/</url>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jcifs/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public interface Configuration {

/**
*
* Property <tt>jcifs.smb.client.useBatching</tt> (boolean, default true)
* Property <tt>jcifs.smb.client.useBatching</tt> (boolean, default false)
*
* @return whether to enable support for SMB1 AndX command batching
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jcifs/config/BaseConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class BaseConfiguration implements Configuration {
protected int localPid = -1;
protected TimeZone localTimeZone;
protected SecureRandom random;
protected boolean useBatching = true;
protected boolean useBatching = false;
protected boolean useUnicode = true;
protected boolean forceUnicode = false;
protected boolean signingPreferred = false;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jcifs/config/PropertyConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class PropertyConfiguration extends BaseConfiguration implements Co
*
*/
public PropertyConfiguration ( Properties p ) throws CIFSException {
this.useBatching = Config.getBoolean(p, "jcifs.smb.client.useBatching", true);
this.useBatching = Config.getBoolean(p, "jcifs.smb.client.useBatching", false);
this.useUnicode = Config.getBoolean(p, "jcifs.smb.client.useUnicode", true);
this.useLargeReadWrite = Config.getBoolean(p, "jcifs.smb.client.useLargeReadWrite", true);
this.forceUnicode = Config.getBoolean(p, "jcifs.smb.client.forceUnicode", false);
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/jcifs/context/BaseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ public BaseContext ( Configuration config ) {
this.nameServiceClient = new NameServiceClientImpl(this);
this.bufferCache = new BufferCacheImpl(this.config);
this.transportPool = new SmbTransportPoolImpl();
this.defaultCredentials = new NtlmPasswordAuthenticator();
String defUser = config.getDefaultUsername();
String defPassword = config.getDefaultPassword();
String defDomain = config.getDefaultDomain();
if ( defUser != null ) {
this.defaultCredentials = new NtlmPasswordAuthenticator(defDomain, defUser, defPassword);
} else {
this.defaultCredentials = new NtlmPasswordAuthenticator();
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ protected int writeAndXWireFormat ( byte[] dst, int dstIndex ) {
* very indirect and simple batching control mechanism.
*/

if ( this.andx == null || getConfig().isUseBatching() || this.batchLevel >= getBatchLimit(getConfig(), (byte) this.andx.getCommand()) ) {
if ( this.andx == null || !getConfig().isUseBatching() || this.batchLevel >= getBatchLimit(getConfig(), (byte) this.andx.getCommand()) ) {
this.andxCommand = (byte) 0xFF;
this.andx = null;

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/jcifs/smb/DfsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,10 @@ else if ( path.charAt(path.length() - 1) == '\\' ) {
}

if ( dr == null ) {
if ( tf.getConfig().isDfsConvertToFQDN()) {
rootDr.fixupDomain(domain);
}

try ( SmbTransportInternal trans = getReferralTransport(tf, rootDr) ) {
if ( trans == null )
return null;
Expand Down
59 changes: 44 additions & 15 deletions src/main/java/jcifs/smb/SmbTransportImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -924,20 +924,7 @@ protected void doSend ( Request request ) throws IOException {
@SuppressWarnings ( "unchecked" )
public <T extends CommonServerMessageBlockResponse> T sendrecv ( CommonServerMessageBlockRequest request, T response, Set<RequestParam> params )
throws IOException {
if ( request instanceof jcifs.internal.Request ) {
if ( response == null ) {
response = (T) ( (jcifs.internal.Request<?>) request ).initResponse(getContext());
}
else if ( isSMB2() ) {
throw new IOException("Should not provide response argument for SMB2");
}
}
else {
request.setResponse(response);
}
if ( response == null ) {
throw new IOException("Invalid response");
}
response = setupResponses(request, response);

CommonServerMessageBlockRequest curHead = request;

Expand Down Expand Up @@ -1050,7 +1037,9 @@ else if ( last == null ) {

CommonServerMessageBlockResponse resp = curReq.getResponse();

if ( resp.isReceived() ) {
if ( resp == null ) {
log.warn("Response not properly set up for" + curReq );
} else if ( resp.isReceived() ) {
grantedCredits += resp.getGrantedCredits();
}
CommonServerMessageBlockRequest next = curReq.getNext();
Expand Down Expand Up @@ -1084,6 +1073,46 @@ else if ( !curReq.isResponseAsync() ) {

}

private <T extends CommonServerMessageBlockResponse> T setupResponses(CommonServerMessageBlockRequest request, T response) throws IOException {
if ( request instanceof jcifs.internal.Request ) {
if ( response == null ) {
response = (T) ( (jcifs.internal.Request<?>) request).initResponse(getContext());
}
else if ( isSMB2() ) {
throw new IOException("Should not provide response argument for SMB2");
}
}
else if ( request instanceof AndXServerMessageBlock && response instanceof AndXServerMessageBlock ) {
AndXServerMessageBlock curReq = (AndXServerMessageBlock) request;
AndXServerMessageBlock curResp = (AndXServerMessageBlock) response;

do {
curReq.setResponse(curResp);

ServerMessageBlock nextReq = curReq.getAndx();
if ( nextReq == null ) {
break;
}
ServerMessageBlock nextResp = curResp.getAndx();
nextReq.setResponse(nextReq);

if ( ! ( nextReq instanceof AndXServerMessageBlock ) || ! ( nextResp instanceof AndXServerMessageBlock )) {
break;
}
curReq = (AndXServerMessageBlock) nextReq;
curResp = (AndXServerMessageBlock) nextResp;

} while ( true );
}
else {
request.setResponse(response);
}
if ( response == null ) {
throw new IOException("Invalid response");
}
return response;
}


@Override
protected <T extends Response> boolean handleIntermediate ( Request request, T response ) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jcifs/smb/SmbTreeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public int hashCode () {
public <T extends CommonServerMessageBlockResponse> T send ( jcifs.internal.Request<T> request, RequestParam... params ) throws CIFSException {
return send(
(CommonServerMessageBlockRequest) request,
null,
request.getResponse(),
( params != null && params.length > 0 ) ? EnumSet.copyOf(Arrays.asList(params)) : EnumSet.noneOf(RequestParam.class));
}

Expand Down
56 changes: 51 additions & 5 deletions src/test/java/jcifs/tests/ContextConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
package jcifs.tests;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import jcifs.context.BaseContext;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -41,6 +39,9 @@
import jcifs.context.SingletonContext;
import jcifs.smb.NtlmPasswordAuthenticator;
import jcifs.smb.SmbFile;
import org.mockito.Matchers;

import static org.junit.Assert.*;


/**
Expand Down Expand Up @@ -196,4 +197,49 @@ public void testLegacyConfig () throws CIFSException {
assertEquals(DialectVersion.SMB202, p3.getMinimumVersion());
}


@Test
// #324
public void testDefaultCredentials() throws CIFSException
{
Properties props = new Properties();
props.put("jcifs.smb.client.domain", "my-domain");
props.put("jcifs.smb.client.username", "my-default-user-id");
props.put("jcifs.smb.client.password", "my-default-password");

CIFSContext auth = new BaseContext(new PropertyConfiguration(props));
assertTrue(auth.hasDefaultCredentials());
assertTrue( auth.getCredentials() instanceof NtlmPasswordAuthenticator);

NtlmPasswordAuthenticator pa = (NtlmPasswordAuthenticator) auth.getCredentials();

assertEquals("my-domain", pa.getUserDomain());
assertEquals("my-default-user-id", pa.getUsername());
assertEquals("my-default-password", pa.getPassword());

assertTrue(auth.withAnonymousCredentials().withDefaultCredentials().hasDefaultCredentials());

}



@Test
public void testPasswordAt() throws MalformedURLException, CIFSException {
Config.registerSmbURLHandler();

try ( SmbFile f = new SmbFile("smb://foo:b%[email protected]/") ) {
Assert.assertEquals("foo:b%40r", f.getLocator().getURL().getUserInfo());
NtlmPasswordAuthenticator na = f.getContext().getCredentials().unwrap(NtlmPasswordAuthenticator.class);
Assert.assertEquals("b@r", na.getPassword());
}

try ( SmbFile f = new SmbFile(new URL("smb://foo:b%[email protected]/")) ) {
Assert.assertEquals("foo:b%40r", f.getLocator().getURL().getUserInfo());
NtlmPasswordAuthenticator na = f.getContext().getCredentials().unwrap(NtlmPasswordAuthenticator.class);
Assert.assertEquals("b@r", na.getPassword());
}


}

}
5 changes: 4 additions & 1 deletion src/test/java/jcifs/tests/EnumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ public void testEnumDeepVirgin () throws IOException {
while ( chld.hasNext() ) {

try ( SmbResource next = chld.next() ) {
try ( CloseableIterator<SmbResource> children = next.children() ) {}
if ( next.isDirectory() ) {
try (CloseableIterator<SmbResource> children = next.children()) {
}
}
names.add(next.getName());
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/jcifs/tests/FileAttributesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ public void testSetAttributes () throws CIFSException, MalformedURLException, Un
catch ( Exception e ) {
// ignore
}
f.delete();
try {
f.delete();
} catch ( Exception e ) {
// ignore
}
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions src/test/java/jcifs/tests/FileOperationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.Collection;
import java.util.Map;

import jcifs.smb.*;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -39,9 +41,6 @@
import jcifs.CIFSException;
import jcifs.SmbResource;
import jcifs.SmbTreeHandle;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbRandomAccessFile;
import jcifs.smb.SmbUnsupportedOperationException;


/**
Expand Down Expand Up @@ -166,13 +165,24 @@ public void testRenameDifferentTrees () throws CIFSException, MalformedURLExcept
boolean renamed = false;
try {
f.renameTo(tgt, true);
Assert.assertFalse("Should not be able to rename between trees", true);
}
catch ( SmbUnsupportedOperationException e ) {
try ( SmbTreeHandle th = defaultShareRoot.getTreeHandle() ) {
Assume.assumeTrue("Not SMB2", th.isSMB2());
}
throw e;
}
catch ( SmbAuthException e ) {
// guest share not accessible
}
catch ( SmbException e) {
if ("Cannot rename between different trees".equals(e.getMessage())) {
// expected
return;
}
throw e;
}
finally {
if ( !renamed && f.exists() ) {
f.delete();
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/jcifs/tests/OplockTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,17 @@ else if ( trans.hasCapability(SmbConstants.CAP_NT_SMBS) ) {
int options = 0;
String uncPath = "foo-oplock";

SmbComNTCreateAndXResponse resp = null;
SmbComNTCreateAndXResponse resp = new SmbComNTCreateAndXResponse(sess.getConfig());
SmbComNTCreateAndX req = new SmbComNTCreateAndX(sess.getConfig(), uncPath, flags, access, sharing, attrs, options, null);
req.addFlags0(0x2); // REQUEST_OPLOCK
req.setResponse(resp);
try {
resp = tree.send(req);
SmbComNTCreateAndXResponse resp2 = null;
SmbComNTCreateAndXResponse resp2 = new SmbComNTCreateAndXResponse(sess.getConfig());
SmbComNTCreateAndX req2 = new SmbComNTCreateAndX(sess.getConfig(), uncPath, flags, access, sharing, attrs, options, null);
req2.addFlags0(0x2); // REQUEST_OPLOCK
req2.setOverrideTimeout(1000);
req2.setResponse(resp2);

try {
resp2 = tree.send(req2);
Expand Down
14 changes: 10 additions & 4 deletions src/test/java/jcifs/tests/SessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,12 @@ public void testNoLeakRequest () throws CIFSException, MalformedURLException {
SmbSessionInternal sess = th.getSession().unwrap(SmbSessionInternal.class);
SmbTransportInternal t = (SmbTransportInternal) sess.getTransport() ) {

assertEquals(0, t.getInflightRequests());
int before = t.getInflightRequests();
if ( before > 0 ) {
log.warn("Have existing inflight requests");
}
f.exists();
assertEquals(0, t.getInflightRequests());
assertEquals(before, t.getInflightRequests());
}
}
}
Expand All @@ -335,14 +338,17 @@ public void testNoLeakRequestError () throws IOException {
SmbSessionInternal sess = th.getSession().unwrap(SmbSessionInternal.class);
SmbTransportInternal t = (SmbTransportInternal) sess.getTransport() ) {

assertEquals(0, t.getInflightRequests());
int before = t.getInflightRequests();
if ( before > 0 ) {
log.warn("Have existing inflight requests");
}
try ( InputStream is = f.openInputStream() ) {

}
catch ( SmbException e ) {
// expected
}
assertEquals(0, t.getInflightRequests());
assertEquals(before, t.getInflightRequests());
}
}
}
Expand Down

0 comments on commit 2a52d08

Please sign in to comment.