Add isE2eEncrypted column in the metadata table
authorKevin Ottens <kevin.ottens@nextcloud.com>
Thu, 2 Jul 2020 15:22:43 +0000 (17:22 +0200)
committerKevin Ottens (Rebase PR Action) <er-vin@users.noreply.github.com>
Mon, 6 Jul 2020 05:27:14 +0000 (05:27 +0000)
This will allow to exploit the information of a directory being
encrypted or not during the discovery phase.

Signed-off-by: Kevin Ottens <kevin.ottens@nextcloud.com>
src/common/syncjournaldb.cpp
src/common/syncjournalfilerecord.h
src/csync/csync.cpp
src/csync/csync.h
src/csync/csync_update.cpp

index df3360879a4caaaa2b06b1680a8088d419c32cb9..d44f1f98b12f8b9cb59179c575da38e299960616 100644 (file)
@@ -47,7 +47,7 @@ Q_LOGGING_CATEGORY(lcDb, "nextcloud.sync.database", QtInfoMsg)
 
 #define GET_FILE_RECORD_QUERY \
         "SELECT path, inode, modtime, type, md5, fileid, remotePerm, filesize," \
-        "  ignoredChildrenRemote, contentchecksumtype.name || ':' || contentChecksum, e2eMangledName " \
+        "  ignoredChildrenRemote, contentchecksumtype.name || ':' || contentChecksum, e2eMangledName, isE2eEncrypted " \
         " FROM metadata" \
         "  LEFT JOIN checksumtype as contentchecksumtype ON metadata.contentChecksumTypeId == contentchecksumtype.id"
 
@@ -64,6 +64,7 @@ static void fillFileRecordFromGetQuery(SyncJournalFileRecord &rec, SqlQuery &que
     rec._serverHasIgnoredFiles = (query.intValue(8) > 0);
     rec._checksumHeader = query.baValue(9);
     rec._e2eMangledName = query.baValue(10);
+    rec._isE2eEncrypted = query.intValue(11) > 0;
 }
 
 static QByteArray defaultJournalMode(const QString &dbPath)
@@ -724,6 +725,16 @@ bool SyncJournalDb::updateMetadataTableStructure()
         commitInternal("update database structure: add e2eMangledName col");
     }
 
+    if (!columns.contains("isE2eEncrypted")) {
+        SqlQuery query(_db);
+        query.prepare("ALTER TABLE metadata ADD COLUMN isE2eEncrypted INTEGER;");
+        if (!query.exec()) {
+            sqlFail("updateMetadataTableStructure: add e2eMangledName column", query);
+            re = false;
+        }
+        commitInternal("update database structure: add e2eMangledName col");
+    }
+
     if (!tableColumns("uploadinfo").contains("contentChecksum")) {
         SqlQuery query(_db);
         query.prepare("ALTER TABLE uploadinfo ADD COLUMN contentChecksum TEXT;");
@@ -843,7 +854,8 @@ bool SyncJournalDb::setFileRecord(const SyncJournalFileRecord &_record)
     qCInfo(lcDb) << "Updating file record for path:" << record._path << "inode:" << record._inode
                  << "modtime:" << record._modtime << "type:" << record._type
                  << "etag:" << record._etag << "fileId:" << record._fileId << "remotePerm:" << record._remotePerm.toString()
-                 << "fileSize:" << record._fileSize << "checksum:" << record._checksumHeader << "e2eMangledName:" << record._e2eMangledName;
+                 << "fileSize:" << record._fileSize << "checksum:" << record._checksumHeader
+                 << "e2eMangledName:" << record._e2eMangledName << "isE2eEncrypted:" << record._isE2eEncrypted;
 
     qlonglong phash = getPHash(record._path);
     if (checkConnect()) {
@@ -862,8 +874,8 @@ bool SyncJournalDb::setFileRecord(const SyncJournalFileRecord &_record)
 
         if (!_setFileRecordQuery.initOrReset(QByteArrayLiteral(
             "INSERT OR REPLACE INTO metadata "
-            "(phash, pathlen, path, inode, uid, gid, mode, modtime, type, md5, fileid, remotePerm, filesize, ignoredChildrenRemote, contentChecksum, contentChecksumTypeId, e2eMangledName) "
-            "VALUES (?1 , ?2, ?3 , ?4 , ?5 , ?6 , ?7,  ?8 , ?9 , ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17);"), _db)) {
+            "(phash, pathlen, path, inode, uid, gid, mode, modtime, type, md5, fileid, remotePerm, filesize, ignoredChildrenRemote, contentChecksum, contentChecksumTypeId, e2eMangledName, isE2eEncrypted) "
+            "VALUES (?1 , ?2, ?3 , ?4 , ?5 , ?6 , ?7,  ?8 , ?9 , ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18);"), _db)) {
             return false;
         }
 
@@ -884,6 +896,7 @@ bool SyncJournalDb::setFileRecord(const SyncJournalFileRecord &_record)
         _setFileRecordQuery.bindValue(15, checksum);
         _setFileRecordQuery.bindValue(16, contentChecksumTypeId);
         _setFileRecordQuery.bindValue(17, record._e2eMangledName);
+        _setFileRecordQuery.bindValue(18, record._isE2eEncrypted);
 
         if (!_setFileRecordQuery.exec()) {
             return false;
@@ -1272,6 +1285,7 @@ bool SyncJournalDb::setFileRecordMetadata(const SyncJournalFileRecord &record)
     existing._fileSize = record._fileSize;
     existing._serverHasIgnoredFiles = record._serverHasIgnoredFiles;
     existing._e2eMangledName = record._e2eMangledName;
+    existing._isE2eEncrypted = record._isE2eEncrypted;
     return setFileRecord(existing);
 }
 
index 3124f78206ce9db7e4701c93e04fa9ed700a2cf5..2bff920220e1a85a3cf44750bcfe20194de15dce 100644 (file)
@@ -65,6 +65,7 @@ public:
     bool _serverHasIgnoredFiles = false;
     QByteArray _checksumHeader;
     QByteArray _e2eMangledName;
+    bool _isE2eEncrypted = false;
 };
 
 bool OCSYNC_EXPORT
index 48cdcc7d9ea32333f8a1987b13976add67c411bd..b07a3f4fe81a83558f257b89955482e2f57ff84e 100644 (file)
@@ -340,5 +340,6 @@ std::unique_ptr<csync_file_stat_t> csync_file_stat_s::fromSyncJournalFileRecord(
     st->has_ignored_files = rec._serverHasIgnoredFiles;
     st->checksumHeader = rec._checksumHeader;
     st->e2eMangledName = rec._e2eMangledName;
+    st->isE2eEncrypted = rec._isE2eEncrypted;
     return st;
 }
index f23e9d21c1d71714ff37c136edd6f2a35c88b778..97895b2c2e87bca4a50f8cd5801c678bd0d08646 100644 (file)
@@ -173,6 +173,7 @@ struct OCSYNC_EXPORT csync_file_stat_s {
   // In both cases, the format is "SHA1:baff".
   QByteArray checksumHeader;
   QByteArray e2eMangledName;
+  bool isE2eEncrypted;
 
   CSYNC_STATUS error_status = CSYNC_STATUS_OK;
 
@@ -183,6 +184,7 @@ struct OCSYNC_EXPORT csync_file_stat_s {
     , child_modified(false)
     , has_ignored_files(false)
     , is_hidden(false)
+    , isE2eEncrypted(false)
   { }
 
   static std::unique_ptr<csync_file_stat_t> fromSyncJournalFileRecord(const OCC::SyncJournalFileRecord &rec);
index 94351a3f84b9a6a6cb1e317114e8780ff644c8f2..f6f029ca6d4e844292f353116a73891ab2a1b239 100644 (file)
@@ -203,7 +203,8 @@ static int _csync_detect_update(CSYNC *ctx, std::unique_ptr<csync_file_stat_t> f
 
   if(base.isValid()) { /* there is an entry in the database */
       // When the file is loaded from the file system it misses
-      // the e2e mangled name
+      // the e2e mangled name and e2e encryption status
+      fs->isE2eEncrypted = base._isE2eEncrypted;
       if (fs->e2eMangledName.isEmpty() && !base._e2eMangledName.isEmpty()) {
           fs->e2eMangledName = base._e2eMangledName;
           fs->path = base._path;