diff --git a/src/cryfs/config/CryConfig.cpp b/src/cryfs/config/CryConfig.cpp index 7ec0eebc..3558e693 100644 --- a/src/cryfs/config/CryConfig.cpp +++ b/src/cryfs/config/CryConfig.cpp @@ -18,12 +18,15 @@ using cpputils::Random; namespace cryfs { +constexpr const char* CryConfig::FilesystemFormatVersion; + CryConfig::CryConfig() : _rootBlob("") , _encKey("") , _cipher("") , _version("") , _createdWithVersion("") +, _lastOpenedWithVersion("") , _blocksizeBytes(0) , _filesystemId(FilesystemID::Null()) , _exclusiveClientId(none) @@ -45,6 +48,7 @@ CryConfig CryConfig::load(const Data &data) { cfg._cipher = pt.get("cryfs.cipher"); cfg._version = pt.get("cryfs.version", "0.8"); // CryFS 0.8 didn't specify this field, so if the field doesn't exist, it's 0.8. cfg._createdWithVersion = pt.get("cryfs.createdWithVersion", cfg._version); // In CryFS <= 0.9.2, we didn't have this field, but also didn't update cryfs.version, so we can use this field instead. + cfg._lastOpenedWithVersion = pt.get("cryfs.lastOpenedWithVersion", cfg._version); // In CryFS <= 0.9.8, we didn't have this field, but used the cryfs.version field for this purpose. cfg._blocksizeBytes = pt.get("cryfs.blocksizeBytes", 32832); // CryFS <= 0.9.2 used a 32KB block size which was this physical block size. cfg._exclusiveClientId = pt.get_optional("cryfs.exclusiveClientId"); #ifndef CRYFS_NO_COMPATIBILITY @@ -69,6 +73,7 @@ Data CryConfig::save() const { pt.put("cryfs.cipher", _cipher); pt.put("cryfs.version", _version); pt.put("cryfs.createdWithVersion", _createdWithVersion); + pt.put("cryfs.lastOpenedWithVersion", _lastOpenedWithVersion); pt.put("cryfs.blocksizeBytes", _blocksizeBytes); pt.put("cryfs.filesystemId", _filesystemId.ToString()); if (_exclusiveClientId != none) { @@ -115,6 +120,10 @@ void CryConfig::SetVersion(std::string value) { _version = std::move(value); } +const std::string &CryConfig::LastOpenedWithVersion() const { + return _lastOpenedWithVersion; +} + const std::string &CryConfig::CreatedWithVersion() const { return _createdWithVersion; } @@ -123,6 +132,10 @@ void CryConfig::SetCreatedWithVersion(std::string value) { _createdWithVersion = std::move(value); } +void CryConfig::SetLastOpenedWithVersion(const std::string &value) { + _lastOpenedWithVersion = value; +} + uint64_t CryConfig::BlocksizeBytes() const { return _blocksizeBytes; } diff --git a/src/cryfs/config/CryConfig.h b/src/cryfs/config/CryConfig.h index 645db7c7..6d0bf2d9 100644 --- a/src/cryfs/config/CryConfig.h +++ b/src/cryfs/config/CryConfig.h @@ -12,6 +12,8 @@ namespace cryfs { class CryConfig final { public: + static constexpr const char* FilesystemFormatVersion = "0.9.6"; + //TODO No default constructor, pass in config values instead! CryConfig(); CryConfig(CryConfig &&rhs) = default; @@ -32,6 +34,9 @@ public: const std::string &CreatedWithVersion() const; void SetCreatedWithVersion(std::string value); + const std::string &LastOpenedWithVersion() const; + void SetLastOpenedWithVersion(const std::string &value); + uint64_t BlocksizeBytes() const; void SetBlocksizeBytes(uint64_t value); @@ -62,6 +67,7 @@ private: std::string _cipher; std::string _version; std::string _createdWithVersion; + std::string _lastOpenedWithVersion; uint64_t _blocksizeBytes; FilesystemID _filesystemId; boost::optional _exclusiveClientId; diff --git a/src/cryfs/config/CryConfigCreator.cpp b/src/cryfs/config/CryConfigCreator.cpp index 2c22729f..f300486d 100644 --- a/src/cryfs/config/CryConfigCreator.cpp +++ b/src/cryfs/config/CryConfigCreator.cpp @@ -22,8 +22,9 @@ namespace cryfs { CryConfigCreator::ConfigCreateResult CryConfigCreator::create(const optional &cipherFromCommandLine, const optional &blocksizeBytesFromCommandLine, const optional &missingBlockIsIntegrityViolationFromCommandLine, bool allowReplacedFilesystem) { CryConfig config; config.SetCipher(_generateCipher(cipherFromCommandLine)); - config.SetVersion(gitversion::VersionString()); + config.SetVersion(CryConfig::FilesystemFormatVersion); config.SetCreatedWithVersion(gitversion::VersionString()); + config.SetLastOpenedWithVersion(gitversion::VersionString()); config.SetBlocksizeBytes(_generateBlocksizeBytes(blocksizeBytesFromCommandLine)); config.SetRootBlob(_generateRootBlobId()); config.SetFilesystemId(_generateFilesystemID()); diff --git a/src/cryfs/config/CryConfigLoader.cpp b/src/cryfs/config/CryConfigLoader.cpp index b759de59..8d751066 100644 --- a/src/cryfs/config/CryConfigLoader.cpp +++ b/src/cryfs/config/CryConfigLoader.cpp @@ -40,6 +40,12 @@ optional CryConfigLoader::_loadConfig(bf::pat return none; } std::cout << "done" << std::endl; +#ifndef CRYFS_NO_COMPATIBILITY + //Since 0.9.7 and 0.9.8 set their own version to cryfs.version instead of the filesystem format version (which is 0.9.6), overwrite it + if (config->config()->Version() == "0.9.7" || config->config()->Version() == "0.9.8") { + config->config()->SetVersion("0.9.6"); + } +#endif _checkVersion(*config->config(), allowFilesystemUpgrade); #ifndef CRYFS_NO_COMPATIBILITY //Since 0.9.3-alpha set the config value cryfs.blocksizeBytes wrongly to 32768 (but didn't use the value), we have to fix this here. @@ -47,8 +53,12 @@ optional CryConfigLoader::_loadConfig(bf::pat config->config()->SetBlocksizeBytes(32832); } #endif - if (config->config()->Version() != gitversion::VersionString()) { - config->config()->SetVersion(gitversion::VersionString()); + if (config->config()->Version() != CryConfig::FilesystemFormatVersion) { + config->config()->SetVersion(CryConfig::FilesystemFormatVersion); + config->save(); + } + if (config->config()->LastOpenedWithVersion() != gitversion::VersionString()) { + config->config()->SetLastOpenedWithVersion(gitversion::VersionString()); config->save(); } _checkCipher(*config->config()); @@ -59,14 +69,14 @@ optional CryConfigLoader::_loadConfig(bf::pat } void CryConfigLoader::_checkVersion(const CryConfig &config, bool allowFilesystemUpgrade) { - if (gitversion::VersionCompare::isOlderThan(gitversion::VersionString(), config.Version())) { - if (!_console->askYesNo("This filesystem is for CryFS " + config.Version() + " and should not be opened with older versions. It is strongly recommended to update your CryFS version. However, if you have backed up your base directory and know what you're doing, you can continue trying to load it. Do you want to continue?", false)) { - throw CryfsException("This filesystem is for CryFS " + config.Version() + ". Please update your CryFS version.", ErrorCode::TooNewFilesystemFormat); + if (gitversion::VersionCompare::isOlderThan(CryConfig::FilesystemFormatVersion, config.Version())) { + if (!_console->askYesNo("This filesystem is for CryFS " + config.Version() + " or later and should not be opened with older versions. It is strongly recommended to update your CryFS version. However, if you have backed up your base directory and know what you're doing, you can continue trying to load it. Do you want to continue?", false)) { + throw CryfsException("This filesystem is for CryFS " + config.Version() + " or later. Please update your CryFS version.", ErrorCode::TooNewFilesystemFormat); } } - if (!allowFilesystemUpgrade && gitversion::VersionCompare::isOlderThan(config.Version(), gitversion::VersionString())) { - if (!_console->askYesNo("This filesystem is for CryFS " + config.Version() + ". It can be migrated to CryFS " + gitversion::VersionString() + ", but afterwards couldn't be opened anymore with older versions. Do you want to migrate it?", false)) { - throw CryfsException("This filesystem is for CryFS " + config.Version() + ". It has to be migrated.", ErrorCode::TooOldFilesystemFormat); + if (!allowFilesystemUpgrade && gitversion::VersionCompare::isOlderThan(config.Version(), CryConfig::FilesystemFormatVersion)) { + if (!_console->askYesNo("This filesystem is for CryFS " + config.Version() + " (or a later version with the same storage format). You're running a CryFS version using storage format " + CryConfig::FilesystemFormatVersion + ". It can be migrated, but afterwards couldn't be opened anymore with older versions. Do you want to migrate it?", false)) { + throw CryfsException("This filesystem is for CryFS " + config.Version() + " (or a later version with the same storage format). It has to be migrated.", ErrorCode::TooOldFilesystemFormat); } } } diff --git a/test/cryfs/config/CryConfigCreatorTest.cpp b/test/cryfs/config/CryConfigCreatorTest.cpp index 8298903a..f04c3ce7 100644 --- a/test/cryfs/config/CryConfigCreatorTest.cpp +++ b/test/cryfs/config/CryConfigCreatorTest.cpp @@ -187,9 +187,14 @@ TEST_F(CryConfigCreatorTest, SetsCorrectCreatedWithVersion) { EXPECT_EQ(gitversion::VersionString(), config.CreatedWithVersion()); } +TEST_F(CryConfigCreatorTest, SetsCorrectLastOpenedWithVersion) { + CryConfig config = noninteractiveCreator.create(none, none); + EXPECT_EQ(gitversion::VersionString(), config.CreatedWithVersion()); +} + TEST_F(CryConfigCreatorTest, SetsCorrectVersion) { CryConfig config = noninteractiveCreator.create(none, none, none).config; - EXPECT_EQ(gitversion::VersionString(), config.Version()); + EXPECT_EQ(CryConfig::FilesystemFormatVersion, config.Version()); } //TODO Add test cases ensuring that the values entered are correctly taken diff --git a/test/cryfs/config/CryConfigLoaderTest.cpp b/test/cryfs/config/CryConfigLoaderTest.cpp index fb87478a..9fa83283 100644 --- a/test/cryfs/config/CryConfigLoaderTest.cpp +++ b/test/cryfs/config/CryConfigLoaderTest.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include using cpputils::TempFile; @@ -113,6 +114,7 @@ public: void CreateWithVersion(const string &version, const string &password = "mypassword") { auto cfg = loader(password, false).loadOrCreate(file.path(), false).value().configFile; cfg.config()->SetVersion(version); + cfg.config()->SetLastOpenedWithVersion(version); cfg.config()->SetCreatedWithVersion(version); cfg.save(); } @@ -130,19 +132,20 @@ public: } string olderVersion() { + auto versionInfo = gitversion::Parser::parse(CryConfig::FilesystemFormatVersion); string olderVersion; - if (std::stol(gitversion::MinorVersion()) > 0) { - olderVersion = gitversion::MajorVersion() + "." + std::to_string(std::stol(gitversion::MinorVersion()) - 1); + if (std::stol(versionInfo.minorVersion) > 0) { + olderVersion = versionInfo.majorVersion + "." + std::to_string(std::stol(versionInfo.minorVersion) - 1); } else { - olderVersion = std::to_string(std::stol(gitversion::MajorVersion()) - 1) + "." + gitversion::MinorVersion(); + olderVersion = std::to_string(std::stol(versionInfo.majorVersion) - 1) + "." + versionInfo.minorVersion; } - assert(gitversion::VersionCompare::isOlderThan(olderVersion, gitversion::VersionString())); + assert(gitversion::VersionCompare::isOlderThan(olderVersion, CryConfig::FilesystemFormatVersion)); return olderVersion; } string newerVersion() { string newerVersion = gitversion::MajorVersion()+"."+std::to_string(std::stol(gitversion::MinorVersion())+1); - assert(gitversion::VersionCompare::isOlderThan(gitversion::VersionString(), newerVersion)); + assert(gitversion::VersionCompare::isOlderThan(CryConfig::FilesystemFormatVersion, newerVersion)); return newerVersion; } @@ -244,7 +247,8 @@ TEST_F(CryConfigLoaderTest, Cipher_Create) { TEST_F(CryConfigLoaderTest, Version_Load) { CreateWithVersion("0.9.2"); auto loaded = Load().value(); - EXPECT_EQ(gitversion::VersionString(), loaded.config()->Version()); + EXPECT_EQ(CryConfig::FilesystemFormatVersion, loaded.config()->Version()); + EXPECT_EQ(gitversion::VersionString(), loaded.config()->LastOpenedWithVersion()); EXPECT_EQ("0.9.2", loaded.config()->CreatedWithVersion()); } @@ -252,13 +256,15 @@ TEST_F(CryConfigLoaderTest, Version_Load_IsStoredAndNotOnlyOverwrittenInMemoryOn CreateWithVersion("0.9.2", "mypassword"); Load().value(); auto configFile = CryConfigFile::load(file.path(), "mypassword").value(); - EXPECT_EQ(gitversion::VersionString(), configFile.config()->Version()); + EXPECT_EQ(CryConfig::FilesystemFormatVersion, configFile.config()->Version()); + EXPECT_EQ(gitversion::VersionString(), configFile.config()->LastOpenedWithVersion()); EXPECT_EQ("0.9.2", configFile.config()->CreatedWithVersion()); } TEST_F(CryConfigLoaderTest, Version_Create) { auto created = Create(); - EXPECT_EQ(gitversion::VersionString(), created.config()->Version()); + EXPECT_EQ(CryConfig::FilesystemFormatVersion, created.config()->Version()); + EXPECT_EQ(gitversion::VersionString(), created.config()->LastOpenedWithVersion()); EXPECT_EQ(gitversion::VersionString(), created.config()->CreatedWithVersion()); }