Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Async Create Request/Response with Oplock implementation. #354

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 164 additions & 1 deletion src/it/groovy/com/hierynomus/smbj/SMB2FileIntegrationTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,31 @@ package com.hierynomus.smbj
import com.hierynomus.msdtyp.AccessMask
import com.hierynomus.mserref.NtStatus
import com.hierynomus.mssmb2.SMB2CreateDisposition
import com.hierynomus.mssmb2.SMB2FileId
import com.hierynomus.mssmb2.SMB2OplockLevel
import com.hierynomus.mssmb2.SMB2ShareAccess
import com.hierynomus.mssmb2.SMBApiException
import com.hierynomus.smb.SMBPacket
import com.hierynomus.smbj.auth.AuthenticationContext
import com.hierynomus.smbj.connection.Connection
import com.hierynomus.smbj.event.AsyncCreateResponseNotification
import com.hierynomus.smbj.event.OplockBreakNotification
import com.hierynomus.smbj.event.handler.AbstractNotificationHandler
import com.hierynomus.smbj.event.handler.MessageIdCallback
import com.hierynomus.smbj.io.ArrayByteChunkProvider
import com.hierynomus.smbj.session.Session
import com.hierynomus.smbj.share.DiskEntry
import com.hierynomus.smbj.share.DiskShare
import com.hierynomus.smbj.transport.tcp.async.AsyncDirectTcpTransportFactory
import spock.lang.Specification
import spock.lang.Unroll

import java.nio.charset.StandardCharsets
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicBoolean

import static com.hierynomus.mssmb2.SMB2CreateDisposition.FILE_CREATE
import static com.hierynomus.mssmb2.SMB2CreateDisposition.FILE_OPEN
import static com.hierynomus.mssmb2.SMB2CreateDisposition.FILE_OPEN_IF

class SMB2FileIntegrationTest extends Specification {

Expand Down Expand Up @@ -185,4 +194,158 @@ class SMB2FileIntegrationTest extends Specification {
cleanup:
share.rm("bigfile")
}

def "should able to async create"() {
given:
def path = "createAsync.txt"
// In actual implementation, the path is not available for createResponse complete. Map is required.
def messageIdPathMap = new ConcurrentHashMap<Long, String>()
// Should call async listener, just calling dummy in test case
def testSucceed = new AtomicBoolean(false)
share.setNotificationHandler( new AbstractNotificationHandler() {

@Override
void handleAsyncCreateResponseNotification(
AsyncCreateResponseNotification asyncCreateResponseNotification) {
def createResponse = asyncCreateResponseNotification.createResponse
def getPath = messageIdPathMap.remove(createResponse.header.messageId)
if(getPath == null) {
System.out.println("Could not find path in map. Should not related to async create, ignored.")
return
}

if(createResponse.header.status != NtStatus.STATUS_SUCCESS) {
throw new IllegalStateException("Async create failed with status " + createResponse.header.status.value)
}

def diskEntry = share.getDiskEntry(getPath, new DiskShare.SMB2CreateResponseContext(createResponse, share))

if(diskEntry != null) {
// Should call async listener, just calling dummy in test case
testSucceed.compareAndSet(false, true)
}
}

})

when:
share.openAsync(path, null, null, EnumSet.of(AccessMask.GENERIC_READ, AccessMask.GENERIC_WRITE), null, SMB2ShareAccess.ALL, FILE_CREATE, null, new MessageIdCallback() {

@Override
void callback(long messageId) {
messageIdPathMap.put(messageId, path)
}
})

then:
// 1 second should be enough for the whole process complete in docker
Thread.sleep(1000L)

expect:
testSucceed.get() == true

cleanup:
share.rm(path)
messageIdPathMap.clear()

}

def "should able to receive oplock break notification and response acknowledgement then receive acknowledgement response"() {
given:
def path = "createAsyncOplock.txt"
// In actual implementation, the path is not available for createResponse complete. Map is required.
def messageIdPathMap = new ConcurrentHashMap<Long, String>()
// Should call async listener, just using hashmap as dummy in test case
def messageIdDiskEntryMap = new ConcurrentHashMap<Long, DiskEntry>()
def fileIdDiskEntryMap = new ConcurrentHashMap<SMB2FileId, DiskEntry>()
def succeedBreakToLevel2 = new AtomicBoolean(false)
def oplockBreakAcknowledgmentResponseSucceed = new AtomicBoolean(false)
share.setNotificationHandler( new AbstractNotificationHandler() {

@Override
void handleAsyncCreateResponseNotification(
AsyncCreateResponseNotification asyncCreateResponseNotification) {
def createResponse = asyncCreateResponseNotification.createResponse
def getPath = messageIdPathMap.remove(createResponse.header.messageId)
if(getPath == null) {
System.out.println("Could not find path in map. Should not related to async create, ignored.")
return
}

if(createResponse.header.status != NtStatus.STATUS_SUCCESS) {
throw new IllegalStateException("Async create failed with status " + createResponse.header.status.value)
}

def diskEntry = share.getDiskEntry(getPath, new DiskShare.SMB2CreateResponseContext(createResponse, share))

if(diskEntry != null) {
// Should call async listener, just calling dummy in test case
messageIdDiskEntryMap.put(createResponse.header.messageId, diskEntry)
fileIdDiskEntryMap.put(diskEntry.fileId, diskEntry)
}
}

@Override
void handleOplockBreakNotification(OplockBreakNotification oplockBreakNotification) {
def oplockBreakLevel = oplockBreakNotification.oplockLevel
def getDiskEntry = fileIdDiskEntryMap.get(oplockBreakNotification.fileId)
if(getDiskEntry == null) {
throw new IllegalStateException("Unable to get corresponding diskEntry!")
}
// Assume we already notify client and had succeed handled client cache to break
if(oplockBreakLevel) {
// In this test case, this code should only run exactly once.
succeedBreakToLevel2.compareAndSet(false, true)
}
// Should return to client for handling the client cache, dummy in test case
def oplockBreakAcknowledgmentResponse = getDiskEntry.acknowledgeOplockBreak(oplockBreakLevel)
if(oplockBreakAcknowledgmentResponse.header.status == NtStatus.STATUS_SUCCESS) {
// In this test case, this code should only run exactly once.
oplockBreakAcknowledgmentResponseSucceed.compareAndSet(false, true)
}
}
})

when:
def firstCreateMessageId = 0L
share.openAsync(path, SMB2OplockLevel.SMB2_OPLOCK_LEVEL_EXCLUSIVE, null, EnumSet.of(AccessMask.GENERIC_READ, AccessMask.GENERIC_WRITE), null, SMB2ShareAccess.ALL, FILE_OPEN_IF, null, new MessageIdCallback() {

@Override
void callback(long messageId) {
messageIdPathMap.put(messageId, path)
firstCreateMessageId = messageId
}
})

then:
// 1 second should be enough for the whole process complete in docker
Thread.sleep(1000L)
def firstCreateDiskEntry = messageIdDiskEntryMap.remove(firstCreateMessageId)
// another create to the same file with SMB2_OPLOCK_LEVEL_EXCLUSIVE to trigger oplock break notification in Server.
def secondCreateMessageId = 0L
share.openAsync(path, SMB2OplockLevel.SMB2_OPLOCK_LEVEL_EXCLUSIVE, null, EnumSet.of(AccessMask.GENERIC_READ, AccessMask.GENERIC_WRITE), null, SMB2ShareAccess.ALL, FILE_OPEN_IF, null, new MessageIdCallback() {

@Override
void callback(long messageId) {
messageIdPathMap.put(messageId, path)
secondCreateMessageId = messageId
}
})
// 1 second should be enough for the whole process complete in docker
Thread.sleep(1000L)
def secondCreateDiskEntry = messageIdDiskEntryMap.remove(secondCreateMessageId)

expect:
firstCreateDiskEntry != null
secondCreateDiskEntry != null
succeedBreakToLevel2.get() == true
oplockBreakAcknowledgmentResponseSucceed.get() == true

cleanup:
share.rm(path)
messageIdPathMap.clear()
messageIdDiskEntryMap.clear()
fileIdDiskEntryMap.clear()

}
}
1 change: 1 addition & 0 deletions src/main/java/com/hierynomus/mserref/NtStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public enum NtStatus implements EnumWithValue<NtStatus> {
STATUS_NOT_SAME_DEVICE(0xC00000D4L),
STATUS_FILE_RENAMED(0xC00000D5L),
STATUS_OPLOCK_NOT_GRANTED(0xC00000E2L),
STATUS_INVALID_OPLOCK_PROTOCOL(0xC00000E3L),
STATUS_INTERNAL_ERROR(0xC00000E5L),
STATUS_UNEXPECTED_IO_ERROR(0xC00000E9L),
STATUS_DIRECTORY_NOT_EMPTY(0xC0000101L),
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/hierynomus/mssmb2/SMB2FileId.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.hierynomus.protocol.commons.buffer.Buffer;
import com.hierynomus.smb.SMBBuffer;

import java.util.Arrays;

/**
* [MS-SMB2].pdf 2.2.14.1 SMB2_FILEID
*/
Expand Down Expand Up @@ -53,4 +55,24 @@ public String toString() {
"persistentHandle=" + ByteArrayUtils.printHex(persistentHandle) +
'}';
}

public String toHexString() {
return ByteArrayUtils.toHex(persistentHandle) + ByteArrayUtils.toHex(volatileHandle);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SMB2FileId smb2FileId = (SMB2FileId) o;
return Arrays.equals(persistentHandle, smb2FileId.persistentHandle) &&
Arrays.equals(volatileHandle, smb2FileId.volatileHandle);
}

@Override
public int hashCode() {
int result = Arrays.hashCode(persistentHandle);
result = 31 * result + Arrays.hashCode(volatileHandle);
return result;
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/hierynomus/mssmb2/SMB2OplockLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C)2016 - SMBJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hierynomus.mssmb2;

import com.hierynomus.protocol.commons.EnumWithValue;

/**
* [MS-SMB2].pdf 2.2.13 SMB2 CREATE Request - OplockLevel
* <p>
*/
public enum SMB2OplockLevel implements EnumWithValue<SMB2OplockLevel> {
SMB2_OPLOCK_LEVEL_NONE(0x00L),
SMB2_OPLOCK_LEVEL_II(0x01L),
SMB2_OPLOCK_LEVEL_EXCLUSIVE(0x08L),
SMB2_OPLOCK_LEVEL_BATCH(0x09L),
// TODO implement and support using lease
OPLOCK_LEVEL_LEASE(0xFFL);

private long value;

SMB2OplockLevel(long value) {
this.value = value;
}

@Override
public long getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ public class SMB2CreateRequest extends SMB2Packet {
private final SmbPath path;
private final Set<AccessMask> accessMask;
private final SMB2ImpersonationLevel impersonationLevel;
private final SMB2OplockLevel oplockLevel;

@SuppressWarnings("PMD.ExcessiveParameterList")
public SMB2CreateRequest(SMB2Dialect smbDialect,
long sessionId, long treeId,
SMB2OplockLevel oplockLevel,
SMB2ImpersonationLevel impersonationLevel,
Set<AccessMask> accessMask,
Set<FileAttributes> fileAttributes,
Set<SMB2ShareAccess> shareAccess, SMB2CreateDisposition createDisposition,
Set<SMB2CreateOptions> createOptions, SmbPath path) {
super(57, smbDialect, SMB2MessageCommandCode.SMB2_CREATE, sessionId, treeId);
this.oplockLevel = ensureNotNull(oplockLevel, SMB2OplockLevel.SMB2_OPLOCK_LEVEL_NONE);
this.impersonationLevel = ensureNotNull(impersonationLevel, SMB2ImpersonationLevel.Identification);
this.accessMask = accessMask;
this.fileAttributes = ensureNotNull(fileAttributes, FileAttributes.class);
Expand All @@ -62,7 +65,7 @@ public SMB2CreateRequest(SMB2Dialect smbDialect,
protected void writeTo(SMBBuffer buffer) {
buffer.putUInt16(structureSize); // StructureSize (2 bytes)
buffer.putByte((byte) 0); // SecurityFlags (1 byte) - Reserved
buffer.putByte((byte) 0); // RequestedOpLockLevel (1 byte) - None
buffer.putByte((byte)oplockLevel.getValue()); // RequestedOpLockLevel (1 byte)
buffer.putUInt32(impersonationLevel.getValue()); // ImpersonationLevel (4 bytes) - Identification
buffer.putReserved(8); // SmbCreateFlags (8 bytes)
buffer.putReserved(8); // Reserved (8 bytes)
Expand Down Expand Up @@ -95,4 +98,8 @@ protected void writeTo(SMBBuffer buffer) {

buffer.putRawBytes(nameBytes);
}

public SmbPath getPath() {
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.hierynomus.msfscc.FileAttributes;
import com.hierynomus.mssmb2.SMB2CreateAction;
import com.hierynomus.mssmb2.SMB2FileId;
import com.hierynomus.mssmb2.SMB2OplockLevel;
import com.hierynomus.mssmb2.SMB2Packet;
import com.hierynomus.protocol.commons.EnumWithValue;
import com.hierynomus.protocol.commons.buffer.Buffer;
Expand All @@ -34,26 +35,29 @@
*/
public class SMB2CreateResponse extends SMB2Packet {

private SMB2OplockLevel oplockLevel;
private SMB2CreateAction createAction;
private FileTime creationTime;
private FileTime lastAccessTime;
private FileTime lastWriteTime;
private FileTime changeTime;
private long allocationSize;
private long endOfFile;
private Set<FileAttributes> fileAttributes;
private SMB2FileId fileId;

@Override
protected void readMessage(SMBBuffer buffer) throws Buffer.BufferException {
buffer.readUInt16(); // StructureSize (2 bytes)
buffer.readByte(); // OpLockLevel (1 byte) - Not used yet
oplockLevel = EnumWithValue.EnumUtils.valueOf(buffer.readByte(), SMB2OplockLevel.class, SMB2OplockLevel.SMB2_OPLOCK_LEVEL_NONE); // OpLockLevel (1 byte)
buffer.readByte(); // Flags (1 byte) - Only for 3.x else Reserved
createAction = EnumWithValue.EnumUtils.valueOf(buffer.readUInt32(), SMB2CreateAction.class, null); // CreateAction (4 bytes)
creationTime = MsDataTypes.readFileTime(buffer); // CreationTime (8 bytes)
lastAccessTime = MsDataTypes.readFileTime(buffer); // LastAccessTime (8 bytes)
lastWriteTime = MsDataTypes.readFileTime(buffer); // LastWriteTime (8 bytes)
changeTime = MsDataTypes.readFileTime(buffer); // ChangeTime (8 bytes)
buffer.readRawBytes(8); // AllocationSize (8 bytes) - Ignore
buffer.readRawBytes(8); // EndOfFile (8 bytes)
allocationSize = buffer.readLong(); // AllocationSize (8 bytes)
endOfFile = buffer.readUInt64(); // EndOfFile (8 bytes)
fileAttributes = toEnumSet(buffer.readUInt32(), FileAttributes.class); // FileAttributes (4 bytes)
buffer.skip(4); // Reserved2 (4 bytes)
fileId = SMB2FileId.read(buffer); // FileId (16 bytes)
Expand All @@ -63,6 +67,10 @@ protected void readMessage(SMBBuffer buffer) throws Buffer.BufferException {
buffer.readUInt32();// CreateContextsLength (4 bytes)
}

public SMB2OplockLevel getOplockLevel() {
return oplockLevel;
}

public SMB2CreateAction getCreateAction() {
return createAction;
}
Expand All @@ -83,6 +91,14 @@ public FileTime getChangeTime() {
return changeTime;
}

public long getAllocationSize() {
return allocationSize;
}

public long getEndOfFile() {
return endOfFile;
}

public Set<FileAttributes> getFileAttributes() {
return fileAttributes;
}
Expand Down
Loading