diff --git a/src/main/java/org/cryptomator/cli/CryptomatorCli.java b/src/main/java/org/cryptomator/cli/CryptomatorCli.java index f48ea81..cd9887e 100644 --- a/src/main/java/org/cryptomator/cli/CryptomatorCli.java +++ b/src/main/java/org/cryptomator/cli/CryptomatorCli.java @@ -2,6 +2,7 @@ import org.cryptomator.cryptofs.CryptoFileSystemProperties; import org.cryptomator.cryptofs.CryptoFileSystemProvider; +import org.cryptomator.cryptofs.VaultConfig; import org.cryptomator.cryptolib.api.Masterkey; import org.cryptomator.cryptolib.common.MasterkeyFileAccess; import org.cryptomator.integrations.mount.UnmountFailedException; @@ -14,6 +15,8 @@ import java.io.IOException; import java.net.URI; import java.nio.CharBuffer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; import java.security.SecureRandom; import java.util.concurrent.Callable; @@ -26,7 +29,10 @@ public class CryptomatorCli implements Callable { private static final Logger LOG = LoggerFactory.getLogger(CryptomatorCli.class); private static final byte[] PEPPER = new byte[0]; + private static final String CONFIG_FILE_NAME = "vault.cryptomator"; + @CommandLine.Spec + CommandLine.Model.CommandSpec spec; @Parameters(index = "0", paramLabel = "/path/to/vaultDirectory", description = "Path to the vault directory") Path pathToVault; @@ -37,17 +43,33 @@ public class CryptomatorCli implements Callable { @CommandLine.ArgGroup(exclusive = false, multiplicity = "1") MountOptions mountOptions; + @CommandLine.Option(names = {"--maxCleartextNameLength"}, description = "Maximum cleartext filename length limit of created files. Remark: If this limit is greater than the shortening threshold, it does not have any effect.") + void setMaxCleartextNameLength(int input) { + if (input <= 0) { + throw new CommandLine.ParameterException(spec.commandLine(), + String.format("Invalid value '%d' for option '--maxCleartextNameLength': " + + "value must be a positive Number between 1 and %d.", input, Integer.MAX_VALUE)); + } + maxCleartextNameLength = input; + } + + private int maxCleartextNameLength = 0; + private SecureRandom csrpg = null; @Override public Integer call() throws Exception { csrpg = SecureRandom.getInstanceStrong(); - CryptoFileSystemProperties fsProps = CryptoFileSystemProperties.cryptoFileSystemProperties() // + + var unverifiedConfig = readConfigFromStorage(pathToVault); + var fsPropsBuilder = CryptoFileSystemProperties.cryptoFileSystemProperties() // .withKeyLoader(this::loadMasterkey) // - //TODO: shortening Threshold - //TODO: maxCleartextname - .build(); - try (var fs = CryptoFileSystemProvider.newFileSystem(pathToVault, fsProps); + .withShorteningThreshold(unverifiedConfig.allegedShorteningThreshold()); //cryptofs checks, if config is signed with masterkey + if (maxCleartextNameLength > 0) { + fsPropsBuilder.withMaxCleartextNameLength(maxCleartextNameLength); + } + + try (var fs = CryptoFileSystemProvider.newFileSystem(pathToVault, fsPropsBuilder.build()); var mount = mountOptions.mount(fs)) { System.out.println(mount.getMountpoint().uri()); while (true) { @@ -74,6 +96,17 @@ private Masterkey loadMasterkey(URI keyId) { } } + /** + * Attempts to read the vault config file and parse it without verifying its integrity. + * + * @throws IOException if reading the file fails + */ + static VaultConfig.UnverifiedVaultConfig readConfigFromStorage(Path vaultPath) throws IOException { + Path configPath = vaultPath.resolve(CONFIG_FILE_NAME); + String token = Files.readString(configPath, StandardCharsets.US_ASCII); + return VaultConfig.decode(token); + } + public static void main(String... args) { int exitCode = new CommandLine(new CryptomatorCli()) .setPosixClusteredShortOptionsAllowed(false)