summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Reschke <lukas@owncloud.com>2016-02-08 20:35:33 +0100
committerLukas Reschke <lukas@owncloud.com>2016-02-09 23:43:27 +0100
commit5ccb9dfa7e35d78d61d7a973ee2a5fddfda7d766 (patch)
treec0d45ac7af479ba32dc2fdbd492999489fa62a94
parent3badf5caf579f8ff10c9917f62cb41cd9b0c68f8 (diff)
downloadnextcloud-server-5ccb9dfa7e35d78d61d7a973ee2a5fddfda7d766.tar.gz
nextcloud-server-5ccb9dfa7e35d78d61d7a973ee2a5fddfda7d766.zip
Use database for keeping track of the version
-rw-r--r--apps/encryption/lib/crypto/encryption.php11
-rw-r--r--apps/encryption/lib/keymanager.php25
-rw-r--r--apps/files_versions/lib/storage.php10
-rw-r--r--lib/private/files/cache/cache.php9
-rw-r--r--lib/private/files/fileinfo.php9
-rw-r--r--lib/private/files/storage/wrapper/encryption.php3
6 files changed, 58 insertions, 9 deletions
diff --git a/apps/encryption/lib/crypto/encryption.php b/apps/encryption/lib/crypto/encryption.php
index d4e8087c4b0..b640f9a7a03 100644
--- a/apps/encryption/lib/crypto/encryption.php
+++ b/apps/encryption/lib/crypto/encryption.php
@@ -56,6 +56,9 @@ class Encryption implements IEncryptionModule {
private $path;
/** @var string */
+ private $realPath;
+
+ /** @var string */
private $user;
/** @var string */
@@ -167,6 +170,7 @@ class Encryption implements IEncryptionModule {
*/
public function begin($path, $user, $mode, array $header, array $accessList) {
$this->path = $this->getPathToRealFile($path);
+ $this->realPath = $this->path;
$this->accessList = $accessList;
$this->user = $user;
$this->isWriteOperation = false;
@@ -182,7 +186,7 @@ class Encryption implements IEncryptionModule {
$this->fileKey = $this->keyManager->getFileKey($this->path, $this->user);
}
- $this->version = (int)$this->keyManager->getVersion($this->path);
+ $this->version = (int)$this->keyManager->getVersion($this->realPath);
if (
$mode === 'w'
@@ -360,7 +364,10 @@ class Encryption implements IEncryptionModule {
*/
public function update($path, $uid, array $accessList) {
$fileKey = $this->keyManager->getFileKey($path, $uid);
- $version = $this->keyManager->getVersion($path);
+ if(empty($this->realPath)) {
+ $this->realPath = $path;
+ }
+ $version = $this->keyManager->getVersion($this->realPath);
if (!empty($fileKey)) {
diff --git a/apps/encryption/lib/keymanager.php b/apps/encryption/lib/keymanager.php
index 4cbb377a43c..7d8bd8485e6 100644
--- a/apps/encryption/lib/keymanager.php
+++ b/apps/encryption/lib/keymanager.php
@@ -25,12 +25,14 @@
namespace OCA\Encryption;
use OC\Encryption\Exceptions\DecryptionFailedException;
+use OC\Files\View;
use OCA\Encryption\Crypto\Encryption;
use OCA\Encryption\Exceptions\PrivateKeyMissingException;
use OCA\Encryption\Exceptions\PublicKeyMissingException;
use OCA\Encryption\Crypto\Crypt;
use OCP\Encryption\Keys\IStorage;
use OCP\IConfig;
+use OCP\IDBConnection;
use OCP\ILogger;
use OCP\IUserSession;
@@ -416,18 +418,35 @@ class KeyManager {
* Get the current version of a file
*
* @param string $path
- * @return mixed
+ * @return int
*/
public function getVersion($path) {
- return $this->keyStorage->getFileKey($path, 'version', Encryption::ID);
+ $view = new \OC\Files\View();
+ $fileInfo = $view->getFileInfo($path);
+ if($fileInfo === false) {
+ return 0;
+ }
+ return $fileInfo->getEncryptedVersion();
}
/**
+ * Set the current version of a file
+ *
* @param string $path
* @param string $version
*/
public function setVersion($path, $version) {
- $this->keyStorage->setFileKey($path, 'version', $version, Encryption::ID);
+ $view = new \OC\Files\View();
+ $fileInfo= $view->getFileInfo($path);
+
+ if($fileInfo !== false) {
+ $fileId = $fileInfo->getId();
+ $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
+ $qb->update('filecache')
+ ->set('encrypted', $qb->createNamedParameter($version))
+ ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($fileId)))
+ ->execute();
+ }
}
/**
diff --git a/apps/files_versions/lib/storage.php b/apps/files_versions/lib/storage.php
index 47acec1d763..0b121c344f9 100644
--- a/apps/files_versions/lib/storage.php
+++ b/apps/files_versions/lib/storage.php
@@ -165,7 +165,15 @@ class Storage {
$mtime = $users_view->filemtime('files/' . $filename);
$users_view->copy('files/' . $filename, 'files_versions/' . $filename . '.v' . $mtime);
// call getFileInfo to enforce a file cache entry for the new version
- $users_view->getFileInfo('files_versions/' . $filename . '.v' . $mtime);
+ $newFileInfo = $users_view->getFileInfo('files_versions/' . $filename . '.v' . $mtime);
+
+ // Keep the "encrypted" value of the original file
+ $oldVersion = $files_view->getFileInfo($filename)->getEncryptedVersion();
+ $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
+ $qb->update('filecache')
+ ->set('encrypted', $qb->createNamedParameter($oldVersion))
+ ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($newFileInfo->getId())))
+ ->execute();
}
}
diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php
index 22b9f49e528..b30666d48d2 100644
--- a/lib/private/files/cache/cache.php
+++ b/lib/private/files/cache/cache.php
@@ -145,6 +145,7 @@ class Cache implements ICache {
$data['size'] = 0 + $data['size'];
$data['mtime'] = (int)$data['mtime'];
$data['storage_mtime'] = (int)$data['storage_mtime'];
+ $data['encryptedVersion'] = (int)$data['encrypted'];
$data['encrypted'] = (bool)$data['encrypted'];
$data['storage'] = $this->storageId;
$data['mimetype'] = $this->mimetypeLoader->getMimetypeById($data['mimetype']);
@@ -345,8 +346,12 @@ class Cache implements ICache {
$queryParts[] = '`mtime`';
}
} elseif ($name === 'encrypted') {
- // Boolean to integer conversion
- $value = $value ? 1 : 0;
+ if(isset($data['encryptedVersion'])) {
+ $value = $data['encryptedVersion'];
+ } else {
+ // Boolean to integer conversion
+ $value = $value ? 1 : 0;
+ }
}
$params[] = $value;
$queryParts[] = '`' . $name . '`';
diff --git a/lib/private/files/fileinfo.php b/lib/private/files/fileinfo.php
index f22e1099e26..1d722a46735 100644
--- a/lib/private/files/fileinfo.php
+++ b/lib/private/files/fileinfo.php
@@ -194,6 +194,15 @@ class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
}
/**
+ * Return the currently version used for the HMAC in the encryption app
+ *
+ * @return int
+ */
+ public function getEncryptedVersion() {
+ return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1;
+ }
+
+ /**
* @return int
*/
public function getPermissions() {
diff --git a/lib/private/files/storage/wrapper/encryption.php b/lib/private/files/storage/wrapper/encryption.php
index 96d642e7780..3307599aa52 100644
--- a/lib/private/files/storage/wrapper/encryption.php
+++ b/lib/private/files/storage/wrapper/encryption.php
@@ -131,11 +131,12 @@ class Encryption extends Wrapper {
// update file cache
if ($info) {
$info = $info->getData();
+ $info['encrypted'] = $info['encryptedVersion'];
} else {
$info = [];
+ $info['encrypted'] = true;
}
- $info['encrypted'] = true;
$info['size'] = $size;
$this->getCache()->put($path, $info);