Skip to content

Commit

Permalink
Add check-file SSH_FXP_EXTENDED functionality (calculating hash)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmagyar committed May 30, 2024
1 parent 82bb193 commit f6248ee
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ FTP server app for android.
* Android 7 Quicksettings Tile
* Optional root access
* Optional support for Android Storage Access Framework to access external sd-card the official way (NOTE requires selecting a directory, not the root of the sd-card).
* Can calculate hash for complete files on server side (sftp extended command)


## Development Snapshot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.file.FileSystemAware;
Expand Down Expand Up @@ -898,8 +900,45 @@ protected void process(Buffer buffer) throws IOException {
}
case SSH_FXP_EXTENDED: {
String extension = buffer.getString();
log.info("Received unsupported SSH_FXP_EXTENDED({})", extension);
sendStatus(id, SSH_FX_OP_UNSUPPORTED, "Command SSH_FXP_EXTENDED(" + extension + ") is unsupported or not implemented");
switch (extension) {
case "check-file": {
String handle = buffer.getString();
String hashalgorithms = buffer.getString();
long hashoffset = buffer.getLong();
long hashlength = buffer.getLong();
int blocksize = buffer.getInt();
log.debug("Received SSH_FXP_EXTENDED({}(handle={}, hashalgorithms={}, offset={}, length={}, blocksize={}))",
new Object[] { extension, handle, hashalgorithms, hashoffset, hashlength, blocksize });
try {
Handle p = handles.get(handle);
if (!(p instanceof FileHandle)) {
sendStatus(id, SSH_FX_FAILURE, handle);
} else {
FileHandle fh = (FileHandle) p;
Object[] filehash = checkFileHash(fh, hashalgorithms, hashoffset, hashlength, blocksize);
String hashalgorithm = (String)filehash[0];
byte[] hash = (byte[])filehash[1];
Buffer buf = new Buffer(extension.length() + hashalgorithm.length() + hash.length + 2*4 + 5);
buf.putByte((byte) SSH_FXP_EXTENDED_REPLY);
buf.putInt(id);
buf.putString(extension);
buf.putString(hashalgorithm);
buf.putRawBytes(hash);
send(buf);
}
} catch (NoSuchAlgorithmException e) {
sendStatus(id, SSH_FX_OP_UNSUPPORTED, e.getMessage());
} catch (UnsupportedOperationException e) {
sendStatus(id, SSH_FX_OP_UNSUPPORTED, e.getMessage());
} catch (IOException e) {
sendStatus(id, SSH_FX_FAILURE, e.getMessage());
}
}
default: {
log.info("Received unsupported SSH_FXP_EXTENDED({})", extension);
sendStatus(id, SSH_FX_OP_UNSUPPORTED, "Command SSH_FXP_EXTENDED(" + extension + ") is unsupported or not implemented");
}
}
break;
}
default: {
Expand Down Expand Up @@ -1188,6 +1227,29 @@ protected Map<SshFile.Attribute, Object> readAttrs(Buffer buffer) throws IOExcep
return attrs;
}

protected Object[] checkFileHash(FileHandle fh, String hashalgorithms, long offset, long length, int blocksize) throws IOException,
NoSuchAlgorithmException, UnsupportedOperationException {
// TODO support multiple hash algorithms
// TODO support hashing blocks of the file, not only the whole file
String hashalgorithm = hashalgorithms.split(",")[0];
if (offset != 0 || length != 0 || blocksize != 0) {
throw new UnsupportedOperationException("Only offset=0, length=0, blocksize=0 is supported");
}

MessageDigest digest = MessageDigest.getInstance(hashalgorithm);
byte[] buffer = new byte[Buffer.MAX_LEN];
int readLen = 0;
while (true) {
readLen = fh.read(buffer, offset);
if (readLen <= 0) {
break;
}
offset += readLen;
digest.update(buffer, 0, readLen);
};
return new Object[]{hashalgorithm, digest.digest()};
}

protected void sendStatus(int id, int substatus, String msg) throws IOException {
sendStatus(id, substatus, msg, "");
}
Expand Down

0 comments on commit f6248ee

Please sign in to comment.