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

Issues 855, 856, 857 #858

Merged
merged 3 commits into from
Jan 24, 2025
Merged
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
2 changes: 1 addition & 1 deletion src/main/java/com/hierynomus/smbj/SMBClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void close() {
logger.info("Going to close all remaining connections");
for (Connection connection : connectionTable.values()) {
try {
connection.close();
connection.close(true);
} catch (Exception e) {
logger.debug("Error closing connection to host {}", connection.getRemoteHostname());
logger.debug("Exception was: ", e);
Expand Down
88 changes: 47 additions & 41 deletions src/main/java/com/hierynomus/smbj/share/DiskShare.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,28 @@ public SMB2CreateResponseContext apply(SmbPath target) {
return new SMB2CreateResponseContext(resp, path, this);
} catch (PathResolveException e) {
throw new SMBApiException(e.getStatusCode(), SMB2MessageCommandCode.SMB2_CREATE,
"Cannot resolve path " + path, e);
"Cannot resolve path " + path, e);
}
}

private SMB2CreateResponseContext resolveAndCreateFile(final SmbPath path,
final SMB2ImpersonationLevel impersonationLevel, final Set<AccessMask> accessMask,
final Set<FileAttributes> fileAttributes, final Set<SMB2ShareAccess> shareAccess,
final SMB2CreateDisposition createDisposition, final Set<SMB2CreateOptions> createOptions) {
final SMB2ImpersonationLevel impersonationLevel, final Set<AccessMask> accessMask,
final Set<FileAttributes> fileAttributes, final Set<SMB2ShareAccess> shareAccess,
final SMB2CreateDisposition createDisposition, final Set<SMB2CreateOptions> createOptions) {
try {
SMB2CreateResponseContext target = resolver.resolve(session, path, new PathResolver.ResolveAction<SMB2CreateResponseContext>(){
SMB2CreateResponseContext target = resolver.resolve(session, path, new PathResolver.ResolveAction<SMB2CreateResponseContext>() {
@Override
public SMB2CreateResponseContext apply(SmbPath target) {
DiskShare resolvedShare = rerouteIfNeeded(path, target);
return resolvedShare.createFileAndResolve(target, impersonationLevel, accessMask, fileAttributes,
shareAccess, createDisposition, createOptions); }
shareAccess, createDisposition, createOptions);
}
});

return target;
} catch (PathResolveException pre) {
throw new SMBApiException(pre.getStatus().getValue(), SMB2MessageCommandCode.SMB2_CREATE,
"Cannot resolve path " + path, pre);
"Cannot resolve path " + path, pre);
}
}

Expand Down Expand Up @@ -256,7 +257,7 @@ public <I extends FileDirectoryQueryableInformation> List<I> list(String path, C
*/
public <I extends FileDirectoryQueryableInformation> List<I> list(String path, Class<I> informationClass, String searchPattern, EnumSet<AccessMask> accessMask) {
Directory d = openDirectory(path,
accessMask == null ? of(FILE_LIST_DIRECTORY, FILE_READ_ATTRIBUTES, FILE_READ_EA) : accessMask,
accessMask == null ? of(FILE_LIST_DIRECTORY, FILE_READ_ATTRIBUTES, FILE_READ_EA) : accessMask,
null, ALL, FILE_OPEN, null);
try {
return d.list(informationClass, searchPattern);
Expand Down Expand Up @@ -405,37 +406,38 @@ public void rmdir(String path, boolean recursive) throws SMBApiException {
if (path == null || path.isEmpty()) {
throw new IllegalArgumentException("rmdir: path should be non-null and non-empty");
}

if (recursive) {
List<FileIdBothDirectoryInformation> list = list(path);
for (FileIdBothDirectoryInformation fi : list) {
if (fi.getFileName().equals(".") || fi.getFileName().equals("..")) {
continue;
try {
if (recursive) {
List<FileIdBothDirectoryInformation> list = list(path);
for (FileIdBothDirectoryInformation fi : list) {
if (fi.getFileName().equals(".") || fi.getFileName().equals("..")) {
continue;
}
String childPath = path + "\\" + fi.getFileName();
if (!EnumWithValue.EnumUtils.isSet(fi.getFileAttributes(), FILE_ATTRIBUTE_DIRECTORY)) {
rm(childPath);
} else {
rmdir(childPath, true);
}
}
String childPath = path + "\\" + fi.getFileName();
if (!EnumWithValue.EnumUtils.isSet(fi.getFileAttributes(), FILE_ATTRIBUTE_DIRECTORY)) {
rm(childPath);
} else {
rmdir(childPath, true);
rmdir(path, false);
} else {
try (DiskEntry e = open(
path,
of(DELETE),
of(FILE_ATTRIBUTE_DIRECTORY),
of(FILE_SHARE_DELETE, FILE_SHARE_WRITE, FILE_SHARE_READ),
FILE_OPEN,
of(FILE_DIRECTORY_FILE)
)) {
e.deleteOnClose();
}
}
rmdir(path, false);
} else {
try (DiskEntry e = open(
path,
of(DELETE),
of(FILE_ATTRIBUTE_DIRECTORY),
of(FILE_SHARE_DELETE, FILE_SHARE_WRITE, FILE_SHARE_READ),
FILE_OPEN,
of(FILE_DIRECTORY_FILE)
)) {
e.deleteOnClose();
} catch (SMBApiException sae) {
if (ALREADY_DELETED_STATUS_HANDLER.isSuccess(sae.getStatusCode())) {
return;
}
throw sae;
} catch (SMBApiException sae) {
if (ALREADY_DELETED_STATUS_HANDLER.isSuccess(sae.getStatusCode())) {
return;
}
throw sae;
}
}

Expand Down Expand Up @@ -467,7 +469,8 @@ public void deleteOnClose(SMB2FileId fileId) {
/**
* The SecurityDescriptor(MS-DTYP 2.4.6 SECURITY_DESCRIPTOR) for the Given Path
*/
public SecurityDescriptor getSecurityInfo(String path, Set<SecurityInformation> securityInfo) throws SMBApiException {
public SecurityDescriptor getSecurityInfo(String path, Set<SecurityInformation> securityInfo) throws
SMBApiException {
EnumSet<AccessMask> accessMask = of(READ_CONTROL);
if (securityInfo.contains(SecurityInformation.SACL_SECURITY_INFORMATION)) {
accessMask.add(ACCESS_SYSTEM_SECURITY);
Expand All @@ -481,7 +484,8 @@ public SecurityDescriptor getSecurityInfo(String path, Set<SecurityInformation>
/**
* The SecurityDescriptor(MS-DTYP 2.4.6 SECURITY_DESCRIPTOR) for the Given FileId
*/
public SecurityDescriptor getSecurityInfo(SMB2FileId fileId, Set<SecurityInformation> securityInfo) throws SMBApiException {
public SecurityDescriptor getSecurityInfo(SMB2FileId fileId, Set<SecurityInformation> securityInfo) throws
SMBApiException {

byte[] outputBuffer = queryInfo(fileId, SMB2_0_INFO_SECURITY, securityInfo, null, null).getOutputBuffer();
try {
Expand All @@ -494,12 +498,13 @@ public SecurityDescriptor getSecurityInfo(SMB2FileId fileId, Set<SecurityInforma
/**
* The SecurityDescriptor(MS-DTYP 2.4.6 SECURITY_DESCRIPTOR) for the Given FileId
*/
public void setSecurityInfo(String path, Set<SecurityInformation> securityInfo, SecurityDescriptor securityDescriptor) throws SMBApiException {
public void setSecurityInfo(String path, Set<SecurityInformation> securityInfo, SecurityDescriptor
securityDescriptor) throws SMBApiException {
Set<AccessMask> accessMask = noneOf(AccessMask.class);
if (securityInfo.contains(SecurityInformation.SACL_SECURITY_INFORMATION)) {
accessMask.add(ACCESS_SYSTEM_SECURITY);
}
if (securityInfo.contains(SecurityInformation.OWNER_SECURITY_INFORMATION) || securityInfo.contains(SecurityInformation. GROUP_SECURITY_INFORMATION)) {
if (securityInfo.contains(SecurityInformation.OWNER_SECURITY_INFORMATION) || securityInfo.contains(SecurityInformation.GROUP_SECURITY_INFORMATION)) {
accessMask.add(WRITE_OWNER);
}
if (securityInfo.contains(SecurityInformation.DACL_SECURITY_INFORMATION)) {
Expand All @@ -514,7 +519,8 @@ public void setSecurityInfo(String path, Set<SecurityInformation> securityInfo,
/**
* The SecurityDescriptor(MS-DTYP 2.4.6 SECURITY_DESCRIPTOR) for the Given FileId
*/
public void setSecurityInfo(SMB2FileId fileId, Set<SecurityInformation> securityInfo, SecurityDescriptor securityDescriptor) throws SMBApiException {
public void setSecurityInfo(SMB2FileId fileId, Set<SecurityInformation> securityInfo, SecurityDescriptor
securityDescriptor) throws SMBApiException {
SMBBuffer buffer = new SMBBuffer();
securityDescriptor.write(buffer);

Expand All @@ -534,7 +540,7 @@ public String toString() {

/**
* A return object for the {@link #createFileAndResolve(SmbPath, SMB2ImpersonationLevel, Set, Set, Set, SMB2CreateDisposition, Set)} call.
*
* <p>
* This object wraps the {@link SMB2CreateResponse} and the actual {@link Share} which generated it if the path needed to be resolved.
*/
static class SMB2CreateResponseContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public PacketReader(String host, InputStream in, PacketReceiver<D> handler) {
this.in = new BufferedInputStream(in);
}
this.handler = handler;
this.thread = new Thread(this, "Packet Reader for " + host);
this.thread = new Thread(this, "Packet Reader for " + host+", Original Thread name: "+Thread.currentThread().getName());
this.thread.setDaemon(true);
}

Expand Down
Loading