server/apps/files_encryption/lib/util.php

1793 lines
46 KiB
PHP
Raw Normal View History

<?php
/**
* ownCloud
*
* @author Sam Tuke, Frank Karlitschek
2013-05-19 23:24:36 +00:00
* @copyright 2012 Sam Tuke <samtuke@owncloud.com>,
* Frank Karlitschek <frank@owncloud.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Encryption;
/**
* @brief Class for utilities relating to encrypted file storage system
* @param \OC_FilesystemView $view expected to have OC '/' as root path
* @param string $userId ID of the logged in user
* @param int $client indicating status of client side encryption. Currently
* unused, likely to become obsolete shortly
*/
class Util {
2013-05-19 23:24:36 +00:00
const MIGRATION_COMPLETED = 1; // migration to new encryption completed
const MIGRATION_IN_PROGRESS = -1; // migration is running
const MIGRATION_OPEN = 0; // user still needs to be migrated
private $view; // OC_FilesystemView object for filesystem operations
private $userId; // ID of the user we use to encrypt/decrypt files
private $keyId; // ID of the key we want to manipulate
private $client; // Client side encryption mode flag
private $publicKeyDir; // Dir containing all public user keys
private $encryptionDir; // Dir containing user's files_encryption
private $keyfilesPath; // Dir containing user's keyfiles
private $shareKeysPath; // Dir containing env keys for shared files
private $publicKeyPath; // Path to user's public key
private $privateKeyPath; // Path to user's private key
private $publicShareKeyId;
private $recoveryKeyId;
2013-05-19 23:24:36 +00:00
private $isPublic;
2013-05-19 23:24:36 +00:00
/**
* @param \OC_FilesystemView $view
* @param $userId
* @param bool $client
*/
public function __construct(\OC_FilesystemView $view, $userId, $client = false) {
$this->view = $view;
$this->client = $client;
$this->userId = $userId;
2013-05-19 23:24:36 +00:00
$this->publicShareKeyId = \OC_Appconfig::getValue('files_encryption', 'publicShareKeyId');
$this->recoveryKeyId = \OC_Appconfig::getValue('files_encryption', 'recoveryKeyId');
2013-05-19 23:24:36 +00:00
$this->userDir = '/' . $this->userId;
$this->fileFolderName = 'files';
$this->userFilesDir =
'/' . $userId . '/' . $this->fileFolderName; // TODO: Does this need to be user configurable?
$this->publicKeyDir = '/' . 'public-keys';
$this->encryptionDir = '/' . $this->userId . '/' . 'files_encryption';
$this->keyfilesPath = $this->encryptionDir . '/' . 'keyfiles';
$this->shareKeysPath = $this->encryptionDir . '/' . 'share-keys';
$this->publicKeyPath =
$this->publicKeyDir . '/' . $this->userId . '.public.key'; // e.g. data/public-keys/admin.public.key
$this->privateKeyPath =
$this->encryptionDir . '/' . $this->userId . '.private.key'; // e.g. data/admin/admin.private.key
// make sure that the owners home is mounted
\OC\Files\Filesystem::initMountPoints($userId);
if (\OCA\Encryption\Helper::isPublicAccess()) {
$this->keyId = $this->publicShareKeyId;
$this->isPublic = true;
2013-05-19 23:24:36 +00:00
} else {
$this->keyId = $this->userId;
$this->isPublic = false;
2013-05-19 23:24:36 +00:00
}
}
2013-05-19 23:24:36 +00:00
/**
* @return bool
*/
2013-05-23 21:56:31 +00:00
public function ready() {
2013-05-19 23:24:36 +00:00
if (
!$this->view->file_exists($this->encryptionDir)
or !$this->view->file_exists($this->keyfilesPath)
or !$this->view->file_exists($this->shareKeysPath)
or !$this->view->file_exists($this->publicKeyPath)
or !$this->view->file_exists($this->privateKeyPath)
) {
return false;
} else {
return true;
}
}
2013-05-19 23:24:36 +00:00
/**
2013-12-05 19:02:41 +00:00
* @brief check if the users private & public key exists
* @return boolean
*/
public function userKeysExists() {
if (
$this->view->file_exists($this->privateKeyPath) &&
$this->view->file_exists($this->publicKeyPath)) {
return true;
} else {
return false;
}
}
2013-05-19 23:24:36 +00:00
/**
* @brief Sets up user folders and keys for serverside encryption
*
* @param string $passphrase to encrypt server-stored private key with
* @return bool
2013-05-19 23:24:36 +00:00
*/
public function setupServerSide($passphrase = null) {
2013-05-19 23:24:36 +00:00
// Set directories to check / create
2013-05-19 23:24:36 +00:00
$setUpDirs = array(
$this->userDir,
$this->userFilesDir,
$this->publicKeyDir,
$this->encryptionDir,
$this->keyfilesPath,
$this->shareKeysPath
);
2013-05-19 23:24:36 +00:00
// Check / create all necessary dirs
foreach ($setUpDirs as $dirPath) {
2013-05-19 23:24:36 +00:00
if (!$this->view->file_exists($dirPath)) {
2013-05-19 23:24:36 +00:00
$this->view->mkdir($dirPath);
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
// Create user keypair
// we should never override a keyfile
2013-05-19 23:24:36 +00:00
if (
!$this->view->file_exists($this->publicKeyPath)
&& !$this->view->file_exists($this->privateKeyPath)
) {
2013-05-19 23:24:36 +00:00
// Generate keypair
$keypair = Crypt::createKeypair();
2013-05-19 23:24:36 +00:00
if ($keypair) {
2013-05-19 23:24:36 +00:00
\OC_FileProxy::$enabled = false;
2013-05-19 23:24:36 +00:00
// Encrypt private key with user pwd as passphrase
$encryptedPrivateKey = Crypt::symmetricEncryptFileContent($keypair['privateKey'], $passphrase);
2013-05-19 23:24:36 +00:00
// Save key-pair
if ($encryptedPrivateKey) {
$this->view->file_put_contents($this->privateKeyPath, $encryptedPrivateKey);
$this->view->file_put_contents($this->publicKeyPath, $keypair['publicKey']);
}
2013-05-19 23:24:36 +00:00
\OC_FileProxy::$enabled = true;
}
2013-05-19 23:24:36 +00:00
} else {
// check if public-key exists but private-key is missing
if ($this->view->file_exists($this->publicKeyPath) && !$this->view->file_exists($this->privateKeyPath)) {
\OCP\Util::writeLog('Encryption library',
'public key exists but private key is missing for "' . $this->keyId . '"', \OCP\Util::FATAL);
return false;
} else {
if (!$this->view->file_exists($this->publicKeyPath) && $this->view->file_exists($this->privateKeyPath)
) {
\OCP\Util::writeLog('Encryption library',
'private key exists but public key is missing for "' . $this->keyId . '"', \OCP\Util::FATAL);
return false;
}
}
}
2013-05-19 23:24:36 +00:00
// If there's no record for this user's encryption preferences
if (false === $this->recoveryEnabledForUser()) {
2013-05-19 23:24:36 +00:00
// create database configuration
$sql = 'INSERT INTO `*PREFIX*encryption` (`uid`,`mode`,`recovery_enabled`,`migration_status`) VALUES (?,?,?,?)';
$args = array(
$this->userId,
'server-side',
0,
self::MIGRATION_OPEN
);
$query = \OCP\DB::prepare($sql);
$query->execute($args);
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
return true;
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
/**
* @return string
*/
2013-05-23 21:56:31 +00:00
public function getPublicShareKeyId() {
return $this->publicShareKeyId;
}
2013-05-19 23:24:36 +00:00
/**
* @brief Check whether pwd recovery is enabled for a given user
2013-05-19 23:24:36 +00:00
* @return bool 1 = yes, 0 = no, false = no record
*
* @note If records are not being returned, check for a hidden space
* at the start of the uid in db
*/
2013-05-23 21:56:31 +00:00
public function recoveryEnabledForUser() {
2013-05-19 23:24:36 +00:00
$sql = 'SELECT `recovery_enabled` FROM `*PREFIX*encryption` WHERE `uid` = ?';
$args = array($this->userId);
2013-05-19 23:24:36 +00:00
$query = \OCP\DB::prepare($sql);
2013-05-19 23:24:36 +00:00
$result = $query->execute($args);
2013-05-19 23:24:36 +00:00
$recoveryEnabled = array();
2013-05-19 23:24:36 +00:00
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
2013-05-25 21:07:19 +00:00
} else {
2013-06-03 16:42:13 +00:00
if ($result->numRows() > 0) {
$row = $result->fetchRow();
if (isset($row['recovery_enabled'])) {
$recoveryEnabled[] = $row['recovery_enabled'];
}
2013-05-25 21:07:19 +00:00
}
}
2013-05-19 23:24:36 +00:00
// If no record is found
if (empty($recoveryEnabled)) {
2013-05-19 23:24:36 +00:00
return false;
2013-05-19 23:24:36 +00:00
// If a record is found
} else {
2013-05-19 23:24:36 +00:00
return $recoveryEnabled[0];
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
/**
* @brief Enable / disable pwd recovery for a given user
* @param bool $enabled Whether to enable or disable recovery
* @return bool
*/
public function setRecoveryForUser($enabled) {
2013-05-19 23:24:36 +00:00
$recoveryStatus = $this->recoveryEnabledForUser();
2013-05-19 23:24:36 +00:00
// If a record for this user already exists, update it
if (false === $recoveryStatus) {
2013-05-19 23:24:36 +00:00
$sql = 'INSERT INTO `*PREFIX*encryption` (`uid`,`mode`,`recovery_enabled`) VALUES (?,?,?)';
2013-05-19 23:24:36 +00:00
$args = array(
$this->userId,
'server-side',
$enabled
);
2013-05-19 23:24:36 +00:00
// Create a new record instead
} else {
2013-05-19 23:24:36 +00:00
$sql = 'UPDATE `*PREFIX*encryption` SET `recovery_enabled` = ? WHERE `uid` = ?';
2013-05-19 23:24:36 +00:00
$args = array(
$enabled,
$this->userId
);
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
2013-06-20 12:49:10 +00:00
return is_numeric(\OC_DB::executeAudited($sql, $args));
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
/**
* @brief Find all files and their encryption status within a directory
* @param string $directory The path of the parent directory to search
* @param bool $found the founded files if called again
* @return mixed false if 0 found, array on success. Keys: name, path
* @note $directory needs to be a path relative to OC data dir. e.g.
* /admin/files NOT /backup OR /home/www/oc/data/admin/files
*/
public function findEncFiles($directory, &$found = false) {
2013-05-19 23:24:36 +00:00
// Disable proxy - we don't want files to be decrypted before
// we handle them
\OC_FileProxy::$enabled = false;
2013-05-19 23:24:36 +00:00
if ($found === false) {
$found = array(
'plain' => array(),
'encrypted' => array(),
'legacy' => array()
);
}
2013-05-19 23:24:36 +00:00
if (
$this->view->is_dir($directory)
&& $handle = $this->view->opendir($directory)
) {
2013-09-05 09:58:57 +00:00
if(is_resource($handle)) {
while (false !== ($file = readdir($handle))) {
if (
$file !== "."
&& $file !== ".."
) {
$filePath = $directory . '/' . $this->view->getRelativePath('/' . $file);
$relPath = \OCA\Encryption\Helper::stripUserFilesPath($filePath);
// If the path is a directory, search
// its contents
if ($this->view->is_dir($filePath)) {
$this->findEncFiles($filePath, $found);
// If the path is a file, determine
// its encryption status
} elseif ($this->view->is_file($filePath)) {
// Disable proxies again, some-
// where they got re-enabled :/
\OC_FileProxy::$enabled = false;
$isEncryptedPath = $this->isEncryptedPath($filePath);
// If the file is encrypted
// NOTE: If the userId is
// empty or not set, file will
// detected as plain
// NOTE: This is inefficient;
// scanning every file like this
// will eat server resources :(
if (
Keymanager::getFileKey($this->view, $this, $relPath)
2013-09-05 09:58:57 +00:00
&& $isEncryptedPath
) {
$found['encrypted'][] = array(
'name' => $file,
'path' => $filePath
);
// If the file uses old
// encryption system
} elseif (Crypt::isLegacyEncryptedContent($isEncryptedPath, $relPath)) {
$found['legacy'][] = array(
'name' => $file,
'path' => $filePath
);
// If the file is not encrypted
} else {
$found['plain'][] = array(
'name' => $file,
'path' => $relPath
);
}
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
}
}
2013-05-19 23:24:36 +00:00
\OC_FileProxy::$enabled = true;
2013-05-19 23:24:36 +00:00
if (empty($found)) {
2013-05-19 23:24:36 +00:00
return false;
2013-05-19 23:24:36 +00:00
} else {
2013-05-19 23:24:36 +00:00
return $found;
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
\OC_FileProxy::$enabled = true;
2013-05-19 23:24:36 +00:00
return false;
}
2013-05-19 23:24:36 +00:00
/**
* @brief Fetch the last lines of a file efficiently
* @note Safe to use on large files; does not read entire file to memory
* @note Derivative of http://tekkie.flashbit.net/php/tail-functionality-in-php
*/
public function tail($filename, $numLines) {
2013-05-19 23:24:36 +00:00
\OC_FileProxy::$enabled = false;
2013-05-19 23:24:36 +00:00
$text = '';
$pos = -1;
$handle = $this->view->fopen($filename, 'r');
2013-05-19 23:24:36 +00:00
while ($numLines > 0) {
--$pos;
if (fseek($handle, $pos, SEEK_END) !== 0) {
2013-05-19 23:24:36 +00:00
rewind($handle);
$numLines = 0;
2013-05-19 23:24:36 +00:00
} elseif (fgetc($handle) === "\n") {
2013-05-19 23:24:36 +00:00
--$numLines;
2013-05-19 23:24:36 +00:00
}
$block_size = (-$pos) % 8192;
if ($block_size === 0 || $numLines === 0) {
2013-05-19 23:24:36 +00:00
$text = fread($handle, ($block_size === 0 ? 8192 : $block_size)) . $text;
2013-05-19 23:24:36 +00:00
}
}
fclose($handle);
2013-05-19 23:24:36 +00:00
\OC_FileProxy::$enabled = true;
2013-05-19 23:24:36 +00:00
return $text;
}
2013-05-19 23:24:36 +00:00
/**
2013-05-19 23:24:36 +00:00
* @brief Check if a given path identifies an encrypted file
* @param string $path
2013-05-19 23:24:36 +00:00
* @return boolean
*/
public function isEncryptedPath($path) {
2013-05-19 23:24:36 +00:00
$relPath = Helper::getPathToRealFile($path);
if ($relPath === false) {
$relPath = Helper::stripUserFilesPath($path);
}
2013-11-21 10:11:15 +00:00
$fileKey = Keymanager::getFileKey($this->view, $this, $relPath);
if ($fileKey === false) {
return false;
}
2013-05-19 23:24:36 +00:00
return true;
2013-05-19 23:24:36 +00:00
}
/**
2013-05-19 23:24:36 +00:00
* @brief get the file size of the unencrypted file
* @param string $path absolute path
* @return bool
*/
public function getFileSize($path) {
$result = 0;
// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
// split the path parts
$pathParts = explode('/', $path);
2013-06-03 16:42:13 +00:00
if (isset($pathParts[2]) && $pathParts[2] === 'files' && $this->view->file_exists($path)
&& $this->isEncryptedPath($path)
) {
// get the size from filesystem
$size = $this->view->filesize($path);
// fast path, else the calculation for $lastChunkNr is bogus
if ($size === 0) {
\OC_FileProxy::$enabled = $proxyStatus;
return 0;
}
// calculate last chunk nr
// next highest is end of chunks, one subtracted is last one
// we have to read the last chunk, we can't just calculate it (because of padding etc)
$lastChunkNr = ceil($size/ 8192) - 1;
$lastChunkSize = $size - ($lastChunkNr * 8192);
// open stream
$stream = fopen('crypt://' . $path, "r");
if (is_resource($stream)) {
// calculate last chunk position
$lastChunckPos = ($lastChunkNr * 8192);
// seek to end
fseek($stream, $lastChunckPos);
// get the content of the last chunk
$lastChunkContent = fread($stream, $lastChunkSize);
// calc the real file size with the size of the last chunk
$realSize = (($lastChunkNr * 6126) + strlen($lastChunkContent));
// store file size
$result = $realSize;
}
}
2013-04-27 21:34:25 +00:00
\OC_FileProxy::$enabled = $proxyStatus;
2013-04-27 21:34:25 +00:00
return $result;
}
2013-05-19 23:24:36 +00:00
/**
* @brief fix the file size of the encrypted file
* @param string $path absolute path
* @return boolean true / false if file is encrypted
*/
public function fixFileSize($path) {
2013-04-27 21:34:25 +00:00
$result = false;
2013-04-27 21:34:25 +00:00
// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
2013-04-27 21:34:25 +00:00
$realSize = $this->getFileSize($path);
2013-05-19 23:24:36 +00:00
if ($realSize > 0) {
2013-05-19 23:24:36 +00:00
$cached = $this->view->getFileInfo($path);
2013-05-10 22:23:30 +00:00
$cached['encrypted'] = true;
2013-04-27 21:34:25 +00:00
// set the size
$cached['unencrypted_size'] = $realSize;
// put file info
$this->view->putFileInfo($path, $cached);
$result = true;
2013-05-19 23:24:36 +00:00
}
\OC_FileProxy::$enabled = $proxyStatus;
return $result;
}
2013-06-03 16:42:13 +00:00
2013-05-19 23:24:36 +00:00
/**
* @param $path
* @return bool
*/
public function isSharedPath($path) {
2013-05-19 23:24:36 +00:00
$trimmed = ltrim($path, '/');
$split = explode('/', $trimmed);
2013-05-19 23:24:36 +00:00
if (isset($split[2]) && $split[2] === 'Shared') {
2013-05-19 23:24:36 +00:00
return true;
2013-05-19 23:24:36 +00:00
} else {
2013-05-19 23:24:36 +00:00
return false;
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
}
2013-07-30 07:48:30 +00:00
2013-07-30 10:19:04 +00:00
/**
* @brief encrypt versions from given file
* @param array $filelist list of encrypted files, relative to data/user/files
* @return boolean
*/
private function encryptVersions($filelist) {
$successful = true;
if (\OCP\App::isEnabled('files_versions')) {
foreach ($filelist as $filename) {
$versions = \OCA\Files_Versions\Storage::getVersions($this->userId, $filename);
foreach ($versions as $version) {
$path = '/' . $this->userId . '/files_versions/' . $version['path'] . '.v' . $version['version'];
$encHandle = fopen('crypt://' . $path . '.part', 'wb');
if ($encHandle === false) {
\OCP\Util::writeLog('Encryption library', 'couldn\'t open "' . $path . '", decryption failed!', \OCP\Util::FATAL);
$successful = false;
continue;
}
$plainHandle = $this->view->fopen($path, 'rb');
if ($plainHandle === false) {
\OCP\Util::writeLog('Encryption library', 'couldn\'t open "' . $path . '.part", decryption failed!', \OCP\Util::FATAL);
$successful = false;
continue;
}
stream_copy_to_stream($plainHandle, $encHandle);
fclose($encHandle);
fclose($plainHandle);
$this->view->rename($path . '.part', $path);
}
}
}
return $successful;
}
/**
* @brief decrypt versions from given file
* @param string $filelist list of decrypted files, relative to data/user/files
* @return boolean
*/
private function decryptVersions($filelist) {
$successful = true;
if (\OCP\App::isEnabled('files_versions')) {
foreach ($filelist as $filename) {
$versions = \OCA\Files_Versions\Storage::getVersions($this->userId, $filename);
foreach ($versions as $version) {
$path = '/' . $this->userId . '/files_versions/' . $version['path'] . '.v' . $version['version'];
$encHandle = fopen('crypt://' . $path, 'rb');
if ($encHandle === false) {
\OCP\Util::writeLog('Encryption library', 'couldn\'t open "' . $path . '", decryption failed!', \OCP\Util::FATAL);
$successful = false;
continue;
}
$plainHandle = $this->view->fopen($path . '.part', 'wb');
if ($plainHandle === false) {
\OCP\Util::writeLog('Encryption library', 'couldn\'t open "' . $path . '.part", decryption failed!', \OCP\Util::FATAL);
$successful = false;
continue;
}
stream_copy_to_stream($encHandle, $plainHandle);
fclose($encHandle);
fclose($plainHandle);
$this->view->rename($path . '.part', $path);
}
}
}
return $successful;
}
2013-07-29 15:06:05 +00:00
/**
* @brief Decrypt all files
* @return bool
*/
public function decryptAll() {
$found = $this->findEncFiles($this->userId . '/files');
2013-07-30 07:48:30 +00:00
$successful = true;
2013-07-29 15:06:05 +00:00
if ($found) {
2013-07-30 10:19:04 +00:00
$versionStatus = \OCP\App::isEnabled('files_versions');
\OC_App::disable('files_versions');
2013-08-12 11:59:49 +00:00
$decryptedFiles = array();
2013-07-30 10:19:04 +00:00
2013-07-29 15:06:05 +00:00
// Encrypt unencrypted files
foreach ($found['encrypted'] as $encryptedFile) {
//relative to data/<user>/file
$relPath = Helper::stripUserFilesPath($encryptedFile['path']);
//get file info
$fileInfo = \OC\Files\Filesystem::getFileInfo($relPath);
2013-07-29 15:06:05 +00:00
//relative to /data
$rawPath = $encryptedFile['path'];
//get timestamp
$timestamp = $fileInfo['mtime'];
//enable proxy to use OC\Files\View to access the original file
\OC_FileProxy::$enabled = true;
2013-07-29 15:06:05 +00:00
// Open enc file handle for binary reading
$encHandle = $this->view->fopen($rawPath, 'rb');
// Disable proxy to prevent file being encrypted again
\OC_FileProxy::$enabled = false;
2013-07-29 15:06:05 +00:00
2013-07-30 07:48:30 +00:00
if ($encHandle === false) {
\OCP\Util::writeLog('Encryption library', 'couldn\'t open "' . $rawPath . '", decryption failed!', \OCP\Util::FATAL);
$successful = false;
continue;
}
2013-07-29 15:06:05 +00:00
// Open plain file handle for binary writing, with same filename as original plain file
$plainHandle = $this->view->fopen($rawPath . '.part', 'wb');
2013-07-30 07:48:30 +00:00
if ($plainHandle === false) {
\OCP\Util::writeLog('Encryption library', 'couldn\'t open "' . $rawPath . '.part", decryption failed!', \OCP\Util::FATAL);
$successful = false;
continue;
}
2013-07-29 15:06:05 +00:00
// Move plain file to a temporary location
$size = stream_copy_to_stream($encHandle, $plainHandle);
2013-07-30 07:48:30 +00:00
if ($size === 0) {
\OCP\Util::writeLog('Encryption library', 'Zero bytes copied of "' . $rawPath . '", decryption failed!', \OCP\Util::FATAL);
$successful = false;
continue;
}
2013-07-29 15:06:05 +00:00
fclose($encHandle);
fclose($plainHandle);
$fakeRoot = $this->view->getRoot();
$this->view->chroot('/' . $this->userId . '/files');
$this->view->rename($relPath . '.part', $relPath);
//set timestamp
$this->view->touch($relPath, $timestamp);
$this->view->chroot($fakeRoot);
2013-07-29 15:06:05 +00:00
// Add the file to the cache
\OC\Files\Filesystem::putFileInfo($relPath, array(
'encrypted' => false,
'size' => $size,
'unencrypted_size' => $size,
'etag' => $fileInfo['etag']
2013-07-29 15:06:05 +00:00
));
2013-07-30 10:19:04 +00:00
$decryptedFiles[] = $relPath;
2013-07-29 15:06:05 +00:00
}
2013-07-30 10:19:04 +00:00
if ($versionStatus) {
\OC_App::enable('files_versions');
}
2013-07-30 10:19:04 +00:00
if (!$this->decryptVersions($decryptedFiles)) {
$successful = false;
}
2013-07-30 07:48:30 +00:00
if ($successful) {
$this->view->deleteAll($this->keyfilesPath);
$this->view->deleteAll($this->shareKeysPath);
}
2013-07-29 15:06:05 +00:00
\OC_FileProxy::$enabled = true;
}
2013-07-30 07:48:30 +00:00
return $successful;
2013-07-29 15:06:05 +00:00
}
2013-05-19 23:24:36 +00:00
/**
* @brief Encrypt all files in a directory
* @param string $dirPath the directory whose files will be encrypted
2013-05-19 23:24:36 +00:00
* @param null $legacyPassphrase
* @param null $newPassphrase
* @return bool
* @note Encryption is recursive
*/
public function encryptAll($dirPath, $legacyPassphrase = null, $newPassphrase = null) {
2013-05-17 12:13:05 +00:00
2013-07-29 15:06:05 +00:00
$found = $this->findEncFiles($dirPath);
2013-07-30 10:19:04 +00:00
2013-07-29 15:06:05 +00:00
if ($found) {
2013-05-17 12:13:05 +00:00
// Disable proxy to prevent file being encrypted twice
\OC_FileProxy::$enabled = false;
2013-07-30 10:19:04 +00:00
$versionStatus = \OCP\App::isEnabled('files_versions');
\OC_App::disable('files_versions');
2013-07-30 10:19:04 +00:00
$encryptedFiles = array();
2013-05-17 12:13:05 +00:00
// Encrypt unencrypted files
foreach ($found['plain'] as $plainFile) {
2013-05-17 12:13:05 +00:00
//get file info
$fileInfo = \OC\Files\Filesystem::getFileInfo($plainFile['path']);
2013-04-12 13:18:19 +00:00
//relative to data/<user>/file
$relPath = $plainFile['path'];
2013-04-12 13:18:19 +00:00
//relative to /data
2013-06-03 16:42:13 +00:00
$rawPath = '/' . $this->userId . '/files/' . $plainFile['path'];
2013-05-17 12:13:05 +00:00
// keep timestamp
$timestamp = $fileInfo['mtime'];
// Open plain file handle for binary reading
2013-06-03 16:42:13 +00:00
$plainHandle = $this->view->fopen($rawPath, 'rb');
2013-05-17 12:13:05 +00:00
// Open enc file handle for binary writing, with same filename as original plain file
2013-07-29 15:06:05 +00:00
$encHandle = fopen('crypt://' . $rawPath . '.part', 'wb');
2013-05-17 12:13:05 +00:00
// Move plain file to a temporary location
2013-06-03 16:42:13 +00:00
$size = stream_copy_to_stream($plainHandle, $encHandle);
2013-05-17 12:13:05 +00:00
fclose($encHandle);
2013-07-29 15:06:05 +00:00
fclose($plainHandle);
2013-05-17 12:13:05 +00:00
$fakeRoot = $this->view->getRoot();
2013-06-03 16:42:13 +00:00
$this->view->chroot('/' . $this->userId . '/files');
2013-05-22 00:02:42 +00:00
$this->view->rename($relPath . '.part', $relPath);
2013-05-17 12:13:05 +00:00
// set timestamp
$this->view->touch($relPath, $timestamp);
$this->view->chroot($fakeRoot);
2013-05-17 12:13:05 +00:00
// Add the file to the cache
2013-06-03 16:42:13 +00:00
\OC\Files\Filesystem::putFileInfo($relPath, array(
2013-07-29 15:06:05 +00:00
'encrypted' => true,
'size' => $size,
'unencrypted_size' => $size,
'etag' => $fileInfo['etag']
2013-07-29 15:06:05 +00:00
));
2013-07-30 10:19:04 +00:00
$encryptedFiles[] = $relPath;
}
2013-05-17 12:13:05 +00:00
// Encrypt legacy encrypted files
2013-05-17 12:13:05 +00:00
if (
!empty($legacyPassphrase)
&& !empty($newPassphrase)
) {
2013-05-17 12:13:05 +00:00
foreach ($found['legacy'] as $legacyFile) {
2013-05-17 12:13:05 +00:00
// Fetch data from file
$legacyData = $this->view->file_get_contents($legacyFile['path']);
2013-05-17 12:13:05 +00:00
2013-05-30 23:36:49 +00:00
// decrypt data, generate catfile
$decrypted = Crypt::legacyBlockDecrypt($legacyData, $legacyPassphrase);
2013-05-17 12:13:05 +00:00
$rawPath = $legacyFile['path'];
2013-05-17 12:13:05 +00:00
2013-05-30 23:36:49 +00:00
// enable proxy the ensure encryption is handled
\OC_FileProxy::$enabled = true;
// Open enc file handle for binary writing, with same filename as original plain file
$encHandle = $this->view->fopen( $rawPath, 'wb' );
2013-05-30 23:36:49 +00:00
if (is_resource($encHandle)) {
2013-05-30 23:36:49 +00:00
// write data to stream
fwrite($encHandle, $decrypted);
2013-05-17 12:13:05 +00:00
2013-05-30 23:36:49 +00:00
// close stream
fclose($encHandle);
}
2013-05-17 12:13:05 +00:00
2013-05-30 23:36:49 +00:00
// disable proxy to prevent file being encrypted twice
\OC_FileProxy::$enabled = false;
}
}
2013-05-17 12:13:05 +00:00
\OC_FileProxy::$enabled = true;
2013-05-17 12:13:05 +00:00
2013-07-30 10:19:04 +00:00
if ($versionStatus) {
\OC_App::enable('files_versions');
}
2013-07-30 10:19:04 +00:00
$this->encryptVersions($encryptedFiles);
// If files were found, return true
return true;
} else {
2013-05-17 12:13:05 +00:00
// If no files were found, return false
return false;
}
}
2013-05-17 12:13:05 +00:00
/**
* @brief Return important encryption related paths
* @param string $pathName Name of the directory to return the path of
* @return string path
*/
public function getPath($pathName) {
2013-05-19 23:24:36 +00:00
switch ($pathName) {
2013-05-19 23:24:36 +00:00
case 'publicKeyDir':
2013-05-19 23:24:36 +00:00
return $this->publicKeyDir;
2013-05-19 23:24:36 +00:00
break;
2013-05-19 23:24:36 +00:00
case 'encryptionDir':
2013-05-19 23:24:36 +00:00
return $this->encryptionDir;
2013-05-19 23:24:36 +00:00
break;
2013-05-19 23:24:36 +00:00
case 'keyfilesPath':
2013-05-19 23:24:36 +00:00
return $this->keyfilesPath;
2013-05-19 23:24:36 +00:00
break;
2013-05-19 23:24:36 +00:00
case 'publicKeyPath':
2013-05-19 23:24:36 +00:00
return $this->publicKeyPath;
2013-05-19 23:24:36 +00:00
break;
2013-05-19 23:24:36 +00:00
case 'privateKeyPath':
2013-05-19 23:24:36 +00:00
return $this->privateKeyPath;
2013-05-19 23:24:36 +00:00
break;
}
2013-05-19 23:24:36 +00:00
2013-05-23 21:56:31 +00:00
return false;
}
2013-05-19 23:24:36 +00:00
/**
* @brief get path of a file.
2013-05-19 23:24:36 +00:00
* @param int $fileId id of the file
* @return string path of the file
*/
public static function fileIdToPath($fileId) {
2013-05-19 23:24:36 +00:00
$sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
2013-05-19 23:24:36 +00:00
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($fileId));
2013-05-19 23:24:36 +00:00
2013-05-25 21:07:19 +00:00
$path = false;
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
2013-05-25 21:07:19 +00:00
} else {
2013-06-03 16:42:13 +00:00
if ($result->numRows() > 0) {
$row = $result->fetchRow();
$path = substr($row['path'], strlen('files'));
}
2013-05-25 19:33:05 +00:00
}
2013-05-25 21:07:19 +00:00
return $path;
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
/**
* @brief Filter an array of UIDs to return only ones ready for sharing
* @param array $unfilteredUsers users to be checked for sharing readiness
* @return array as multi-dimensional array. keys: ready, unready
*/
public function filterShareReadyUsers($unfilteredUsers) {
2013-05-19 23:24:36 +00:00
// This array will collect the filtered IDs
$readyIds = $unreadyIds = array();
2013-05-19 23:24:36 +00:00
// Loop through users and create array of UIDs that need new keyfiles
foreach ($unfilteredUsers as $user) {
2013-05-19 23:24:36 +00:00
$util = new Util($this->view, $user);
2013-05-19 23:24:36 +00:00
// Check that the user is encryption capable, or is the
// public system user 'ownCloud' (for public shares)
2013-05-19 23:24:36 +00:00
if (
$user === $this->publicShareKeyId
or $user === $this->recoveryKeyId
2013-05-19 23:24:36 +00:00
or $util->ready()
) {
2013-05-19 23:24:36 +00:00
// Construct array of ready UIDs for Keymanager{}
$readyIds[] = $user;
2013-05-19 23:24:36 +00:00
} else {
2013-05-19 23:24:36 +00:00
// Construct array of unready UIDs for Keymanager{}
$unreadyIds[] = $user;
2013-05-19 23:24:36 +00:00
// Log warning; we can't do necessary setup here
// because we don't have the user passphrase
\OCP\Util::writeLog('Encryption library',
'"' . $user . '" is not setup for encryption', \OCP\Util::WARN);
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
return array(
2013-05-23 21:56:31 +00:00
'ready' => $readyIds,
'unready' => $unreadyIds
);
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
/**
* @brief Decrypt a keyfile
* @param string $filePath
* @param string $privateKey
* @return bool|string
*/
private function decryptKeyfile($filePath, $privateKey) {
// Get the encrypted keyfile
$encKeyfile = Keymanager::getFileKey($this->view, $this, $filePath);
2013-05-19 23:24:36 +00:00
// The file has a shareKey and must use it for decryption
$shareKey = Keymanager::getShareKey($this->view, $this->keyId, $this, $filePath);
2013-05-19 23:24:36 +00:00
$plainKeyfile = Crypt::multiKeyDecrypt($encKeyfile, $shareKey, $privateKey);
2013-05-19 23:24:36 +00:00
return $plainKeyfile;
}
2013-05-19 23:24:36 +00:00
/**
* @brief Encrypt keyfile to multiple users
2013-05-19 23:24:36 +00:00
* @param Session $session
* @param array $users list of users which should be able to access the file
* @param string $filePath path of the file to be shared
2013-05-19 23:24:36 +00:00
* @return bool
*/
public function setSharedFileKeyfiles(Session $session, array $users, $filePath) {
2013-05-19 23:24:36 +00:00
// Make sure users are capable of sharing
$filteredUids = $this->filterShareReadyUsers($users);
2013-05-19 23:24:36 +00:00
// If we're attempting to share to unready users
if (!empty($filteredUids['unready'])) {
2013-05-19 23:24:36 +00:00
\OCP\Util::writeLog('Encryption library',
'Sharing to these user(s) failed as they are unready for encryption:"'
. print_r($filteredUids['unready'], 1), \OCP\Util::WARN);
2013-05-19 23:24:36 +00:00
return false;
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
// Get public keys for each user, ready for generating sharekeys
$userPubKeys = Keymanager::getPublicKeys($this->view, $filteredUids['ready']);
2013-05-19 23:24:36 +00:00
// Note proxy status then disable it
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
// Get the current users's private key for decrypting existing keyfile
$privateKey = $session->getPrivateKey();
$fileOwner = \OC\Files\Filesystem::getOwner($filePath);
2013-05-19 23:24:36 +00:00
// Decrypt keyfile
$plainKeyfile = $this->decryptKeyfile($filePath, $privateKey);
2013-05-19 23:24:36 +00:00
// Re-enc keyfile to (additional) sharekeys
$multiEncKey = Crypt::multiKeyEncrypt($plainKeyfile, $userPubKeys);
2013-05-19 23:24:36 +00:00
// Save the recrypted key to it's owner's keyfiles directory
// Save new sharekeys to all necessary user directory
2013-05-19 23:24:36 +00:00
if (
!Keymanager::setFileKey($this->view, $this, $filePath, $multiEncKey['data'])
|| !Keymanager::setShareKeys($this->view, $this, $filePath, $multiEncKey['keys'])
) {
\OCP\Util::writeLog('Encryption library',
'Keyfiles could not be saved for users sharing ' . $filePath, \OCP\Util::ERROR);
2013-05-19 23:24:36 +00:00
return false;
}
2013-05-19 23:24:36 +00:00
// Return proxy to original status
\OC_FileProxy::$enabled = $proxyStatus;
return true;
}
2013-05-19 23:24:36 +00:00
/**
* @brief Find, sanitise and format users sharing a file
* @note This wraps other methods into a portable bundle
*/
public function getSharingUsersArray($sharingEnabled, $filePath, $currentUserId = false) {
// Check if key recovery is enabled
if (
\OC_Appconfig::getValue('files_encryption', 'recoveryAdminEnabled')
&& $this->recoveryEnabledForUser()
) {
$recoveryEnabled = true;
} else {
$recoveryEnabled = false;
}
2013-05-19 23:24:36 +00:00
// Make sure that a share key is generated for the owner too
list($owner, $ownerPath) = $this->getUidAndFilename($filePath);
2013-05-19 23:24:36 +00:00
$ownerPath = \OCA\Encryption\Helper::stripPartialFileExtension($ownerPath);
2013-05-23 21:56:31 +00:00
$userIds = array();
if ($sharingEnabled) {
// Find out who, if anyone, is sharing the file
$result = \OCP\Share::getUsersSharingFile($ownerPath, $owner, true);
$userIds = $result['users'];
if ($result['public']) {
$userIds[] = $this->publicShareKeyId;
}
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
// If recovery is enabled, add the
// Admin UID to list of users to share to
if ($recoveryEnabled) {
// Find recoveryAdmin user ID
$recoveryKeyId = \OC_Appconfig::getValue('files_encryption', 'recoveryKeyId');
// Add recoveryAdmin to list of users sharing
$userIds[] = $recoveryKeyId;
}
// add current user if given
if ($currentUserId !== false) {
$userIds[] = $currentUserId;
2013-06-25 10:21:54 +00:00
}
2013-05-19 23:24:36 +00:00
2013-06-25 10:21:54 +00:00
// check if it is a group mount
if (\OCP\App::isEnabled("files_external")) {
$mount = \OC_Mount_Config::getSystemMountPoints();
foreach ($mount as $mountPoint => $data) {
if ($mountPoint == substr($ownerPath, 1, strlen($mountPoint))) {
$userIds = array_merge($userIds, $this->getUserWithAccessToMountPoint($data['applicable']['users'], $data['applicable']['groups']));
}
2013-06-25 10:21:54 +00:00
}
}
// Remove duplicate UIDs
$uniqueUserIds = array_unique($userIds);
2013-05-19 23:24:36 +00:00
return $uniqueUserIds;
}
2013-05-19 23:24:36 +00:00
2013-06-25 10:21:54 +00:00
private function getUserWithAccessToMountPoint($users, $groups) {
$result = array();
if (in_array('all', $users)) {
$result = \OCP\User::getUsers();
} else {
$result = array_merge($result, $users);
foreach ($groups as $group) {
$result = array_merge($result, \OC_Group::usersInGroup($group));
}
}
return $result;
}
/**
* @brief start migration mode to initially encrypt users data
* @return boolean
*/
public function beginMigration() {
2013-05-19 23:24:36 +00:00
$return = false;
2013-05-19 23:24:36 +00:00
$sql = 'UPDATE `*PREFIX*encryption` SET `migration_status` = ? WHERE `uid` = ? and `migration_status` = ?';
$args = array(self::MIGRATION_IN_PROGRESS, $this->userId, self::MIGRATION_OPEN);
$query = \OCP\DB::prepare($sql);
2013-06-20 12:49:10 +00:00
$manipulatedRows = $query->execute($args);
2013-05-19 23:24:36 +00:00
if ($manipulatedRows === 1) {
$return = true;
\OCP\Util::writeLog('Encryption library', "Start migration to encryption mode for " . $this->userId, \OCP\Util::INFO);
} else {
\OCP\Util::writeLog('Encryption library', "Could not activate migration mode for " . $this->userId . ". Probably another process already started the initial encryption", \OCP\Util::WARN);
}
2013-05-19 23:24:36 +00:00
return $return;
}
2013-05-19 23:24:36 +00:00
/**
* @brief close migration mode after users data has been encrypted successfully
* @return boolean
*/
public function finishMigration() {
2013-05-19 23:24:36 +00:00
$return = false;
2013-05-19 23:24:36 +00:00
$sql = 'UPDATE `*PREFIX*encryption` SET `migration_status` = ? WHERE `uid` = ? and `migration_status` = ?';
$args = array(self::MIGRATION_COMPLETED, $this->userId, self::MIGRATION_IN_PROGRESS);
$query = \OCP\DB::prepare($sql);
2013-06-20 12:49:10 +00:00
$manipulatedRows = $query->execute($args);
2013-05-19 23:24:36 +00:00
if ($manipulatedRows === 1) {
2013-06-13 08:11:23 +00:00
$return = true;
\OCP\Util::writeLog('Encryption library', "Finish migration successfully for " . $this->userId, \OCP\Util::INFO);
} else {
\OCP\Util::writeLog('Encryption library', "Could not deactivate migration mode for " . $this->userId, \OCP\Util::WARN);
}
2013-05-19 23:24:36 +00:00
return $return;
}
2013-05-19 23:24:36 +00:00
/**
* @brief check if files are already migrated to the encryption system
* @return migration status, false = in case of no record
2013-05-19 23:24:36 +00:00
* @note If records are not being returned, check for a hidden space
* at the start of the uid in db
*/
public function getMigrationStatus() {
$sql = 'SELECT `migration_status` FROM `*PREFIX*encryption` WHERE `uid` = ?';
2013-05-19 23:24:36 +00:00
$args = array($this->userId);
$query = \OCP\DB::prepare($sql);
2013-05-19 23:24:36 +00:00
$result = $query->execute($args);
2013-05-19 23:24:36 +00:00
$migrationStatus = array();
2013-05-19 23:24:36 +00:00
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
2013-05-25 21:07:19 +00:00
} else {
2013-06-03 16:42:13 +00:00
if ($result->numRows() > 0) {
$row = $result->fetchRow();
if (isset($row['migration_status'])) {
$migrationStatus[] = $row['migration_status'];
}
2013-05-25 21:07:19 +00:00
}
}
2013-05-19 23:24:36 +00:00
// If no record is found
if (empty($migrationStatus)) {
\OCP\Util::writeLog('Encryption library', "Could not get migration status for " . $this->userId . ", no record found", \OCP\Util::ERROR);
// insert missing entry in DB with status open if the user exists
if (\OCP\User::userExists($this->userId)) {
$sql = 'INSERT INTO `*PREFIX*encryption` (`uid`,`mode`,`recovery_enabled`,`migration_status`) VALUES (?,?,?,?)';
$args = array(
$this->userId,
'server-side',
0,
self::MIGRATION_OPEN
);
$query = \OCP\DB::prepare($sql);
$query->execute($args);
return self::MIGRATION_OPEN;
} else {
return false;
}
} else { // If a record is found
return (int)$migrationStatus[0];
}
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
/**
2013-03-26 11:39:55 +00:00
* @brief get uid of the owners of the file and the path to the file
2013-05-19 23:24:36 +00:00
* @param string $path Path of the file to check
* @throws \Exception
2013-05-19 23:24:36 +00:00
* @note $shareFilePath must be relative to data/UID/files. Files
* relative to /Shared are also acceptable
2013-03-26 11:39:55 +00:00
* @return array
*/
public function getUidAndFilename($path) {
2013-05-19 23:24:36 +00:00
2013-08-22 15:55:10 +00:00
$pathinfo = pathinfo($path);
$partfile = false;
$parentFolder = false;
if (array_key_exists('extension', $pathinfo) && $pathinfo['extension'] === 'part') {
2013-08-22 15:55:10 +00:00
// if the real file exists we check this file
2013-09-05 08:11:09 +00:00
$filePath = $this->userFilesDir . '/' .$pathinfo['dirname'] . '/' . $pathinfo['filename'];
if ($this->view->file_exists($filePath)) {
2013-08-22 15:55:10 +00:00
$pathToCheck = $pathinfo['dirname'] . '/' . $pathinfo['filename'];
} else { // otherwise we look for the parent
$pathToCheck = $pathinfo['dirname'];
$parentFolder = true;
}
$partfile = true;
} else {
$pathToCheck = $path;
}
$view = new \OC\Files\View($this->userFilesDir);
2013-08-22 15:55:10 +00:00
$fileOwnerUid = $view->getOwner($pathToCheck);
2013-05-19 23:24:36 +00:00
// handle public access
if ($this->isPublic) {
2013-05-19 23:24:36 +00:00
$filename = $path;
$fileOwnerUid = $this->userId;
return array(
$fileOwnerUid,
$filename
);
2013-05-19 23:24:36 +00:00
} else {
2013-05-19 23:24:36 +00:00
// Check that UID is valid
if (!\OCP\User::userExists($fileOwnerUid)) {
throw new \Exception(
'Could not find owner (UID = "' . var_export($fileOwnerUid, 1) . '") of file "' . $path . '"');
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
// NOTE: Bah, this dependency should be elsewhere
\OC\Files\Filesystem::initMountPoints($fileOwnerUid);
2013-05-19 23:24:36 +00:00
// If the file owner is the currently logged in user
if ($fileOwnerUid === $this->userId) {
2013-05-19 23:24:36 +00:00
// Assume the path supplied is correct
$filename = $path;
2013-05-19 23:24:36 +00:00
} else {
2013-08-22 15:55:10 +00:00
$info = $view->getFileInfo($pathToCheck);
$ownerView = new \OC\Files\View('/' . $fileOwnerUid . '/files');
2013-05-19 23:24:36 +00:00
// Fetch real file path from DB
2013-08-22 15:55:10 +00:00
$filename = $ownerView->getPath($info['fileid']);
if ($parentFolder) {
$filename = $filename . '/'. $pathinfo['filename'];
}
if ($partfile) {
$filename = $filename . '.' . $pathinfo['extension'];
}
2013-05-19 23:24:36 +00:00
}
return array(
$fileOwnerUid,
2013-06-25 14:41:24 +00:00
\OC_Filesystem::normalizePath($filename)
);
2013-05-19 23:24:36 +00:00
}
2013-03-26 11:39:55 +00:00
}
2013-08-22 15:55:10 +00:00
/**
2013-05-23 21:56:31 +00:00
* @brief go recursively through a dir and collect all files and sub files.
2013-05-19 23:24:36 +00:00
* @param string $dir relative to the users files folder
* @return array with list of files relative to the users files folder
*/
public function getAllFiles($dir) {
2013-05-19 23:24:36 +00:00
$result = array();
2013-05-01 09:04:40 +00:00
2013-06-03 16:42:13 +00:00
$content = $this->view->getDirectoryContent(\OC\Files\Filesystem::normalizePath(
$this->userFilesDir . '/' . $dir));
2013-05-01 09:04:40 +00:00
// handling for re shared folders
$pathSplit = explode('/', $dir);
2013-05-19 23:24:36 +00:00
foreach ($content as $c) {
2013-05-19 23:24:36 +00:00
$sharedPart = $pathSplit[sizeof($pathSplit) - 1];
$targetPathSplit = array_reverse(explode('/', $c['path']));
$path = '';
// rebuild path
foreach ($targetPathSplit as $pathPart) {
2013-05-19 23:24:36 +00:00
if ($pathPart !== $sharedPart) {
2013-05-19 23:24:36 +00:00
$path = '/' . $pathPart . $path;
2013-05-19 23:24:36 +00:00
} else {
2013-05-19 23:24:36 +00:00
break;
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
$path = $dir . $path;
if ($c['type'] === 'dir') {
2013-05-19 23:24:36 +00:00
$result = array_merge($result, $this->getAllFiles($path));
} else {
2013-05-19 23:24:36 +00:00
$result[] = $path;
2013-05-19 23:24:36 +00:00
}
}
2013-05-19 23:24:36 +00:00
return $result;
2013-05-19 23:24:36 +00:00
}
/**
* @brief get shares parent.
* @param int $id of the current share
* @return array of the parent
*/
public static function getShareParent($id) {
2013-05-04 01:37:22 +00:00
$sql = 'SELECT `file_target`, `item_type` FROM `*PREFIX*share` WHERE `id` = ?';
2013-05-04 01:37:22 +00:00
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($id));
2013-05-04 01:37:22 +00:00
2013-05-25 21:07:19 +00:00
$row = array();
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
2013-05-25 21:07:19 +00:00
} else {
2013-06-03 16:42:13 +00:00
if ($result->numRows() > 0) {
$row = $result->fetchRow();
}
2013-05-25 19:33:05 +00:00
}
return $row;
2013-05-04 01:37:22 +00:00
}
/**
* @brief get shares parent.
* @param int $id of the current share
* @return array of the parent
*/
public static function getParentFromShare($id) {
$sql = 'SELECT `parent` FROM `*PREFIX*share` WHERE `id` = ?';
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($id));
2013-05-25 21:07:19 +00:00
$row = array();
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
2013-05-25 21:07:19 +00:00
} else {
2013-06-03 16:42:13 +00:00
if ($result->numRows() > 0) {
$row = $result->fetchRow();
}
2013-05-25 19:33:05 +00:00
}
return $row;
}
/**
* @brief get owner of the shared files.
2013-05-19 23:24:36 +00:00
* @param $id
* @internal param int $Id of a share
* @return string owner
*/
public function getOwnerFromSharedFile($id) {
2013-05-23 21:56:31 +00:00
$query = \OCP\DB::prepare('SELECT `parent`, `uid_owner` FROM `*PREFIX*share` WHERE `id` = ?', 1);
2013-05-25 19:33:05 +00:00
$result = $query->execute(array($id));
2013-05-25 19:33:05 +00:00
2013-05-25 21:07:19 +00:00
$source = array();
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
2013-05-25 21:07:19 +00:00
} else {
2013-06-03 16:42:13 +00:00
if ($result->numRows() > 0) {
$source = $result->fetchRow();
}
2013-05-25 19:33:05 +00:00
}
2013-05-23 21:56:31 +00:00
$fileOwner = false;
2013-05-19 23:24:36 +00:00
if (isset($source['parent'])) {
$parent = $source['parent'];
2013-05-19 23:24:36 +00:00
while (isset($parent)) {
2013-05-19 23:24:36 +00:00
$query = \OCP\DB::prepare('SELECT `parent`, `uid_owner` FROM `*PREFIX*share` WHERE `id` = ?', 1);
2013-05-25 19:33:05 +00:00
$result = $query->execute(array($parent));
2013-05-25 19:33:05 +00:00
2013-05-25 21:07:19 +00:00
$item = array();
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
2013-05-25 21:07:19 +00:00
} else {
2013-06-03 16:42:13 +00:00
if ($result->numRows() > 0) {
$item = $result->fetchRow();
}
2013-05-25 19:33:05 +00:00
}
if (isset($item['parent'])) {
2013-05-19 23:24:36 +00:00
$parent = $item['parent'];
2013-05-19 23:24:36 +00:00
} else {
2013-05-19 23:24:36 +00:00
$fileOwner = $item['uid_owner'];
2013-05-19 23:24:36 +00:00
break;
2013-05-19 23:24:36 +00:00
}
}
2013-05-19 23:24:36 +00:00
} else {
2013-05-19 23:24:36 +00:00
$fileOwner = $source['uid_owner'];
2013-05-19 23:24:36 +00:00
}
return $fileOwner;
2013-05-19 23:24:36 +00:00
}
2013-05-19 23:24:36 +00:00
/**
* @return string
*/
2013-05-23 21:56:31 +00:00
public function getUserId() {
2013-05-19 23:24:36 +00:00
return $this->userId;
}
/**
* @return string
*/
public function getKeyId() {
return $this->keyId;
}
2013-05-19 23:24:36 +00:00
/**
* @return string
*/
2013-05-23 21:56:31 +00:00
public function getUserFilesDir() {
2013-05-19 23:24:36 +00:00
return $this->userFilesDir;
}
2013-05-19 23:24:36 +00:00
/**
* @param $password
* @return bool
*/
public function checkRecoveryPassword($password) {
$result = false;
$pathKey = '/owncloud_private_key/' . $this->recoveryKeyId . ".private.key";
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$recoveryKey = $this->view->file_get_contents($pathKey);
$decryptedRecoveryKey = Crypt::decryptPrivateKey($recoveryKey, $password);
if ($decryptedRecoveryKey) {
$result = true;
}
\OC_FileProxy::$enabled = $proxyStatus;
2013-05-19 23:24:36 +00:00
return $result;
}
2013-05-19 23:24:36 +00:00
/**
* @return string
*/
2013-05-23 21:56:31 +00:00
public function getRecoveryKeyId() {
return $this->recoveryKeyId;
}
/**
* @brief add recovery key to all encrypted files
*/
public function addRecoveryKeys($path = '/') {
$dirContent = $this->view->getDirectoryContent($this->keyfilesPath . $path);
foreach ($dirContent as $item) {
2013-05-23 21:56:31 +00:00
// get relative path from files_encryption/keyfiles/
$filePath = substr($item['path'], strlen('files_encryption/keyfiles'));
if ($item['type'] === 'dir') {
$this->addRecoveryKeys($filePath . '/');
} else {
$session = new \OCA\Encryption\Session(new \OC_FilesystemView('/'));
$sharingEnabled = \OCP\Share::isEnabled();
2013-05-29 10:23:33 +00:00
// remove '.key' extension from path e.g. 'file.txt.key' to 'file.txt'
$file = substr($filePath, 0, -4);
$usersSharing = $this->getSharingUsersArray($sharingEnabled, $file);
$this->setSharedFileKeyfiles($session, $usersSharing, $file);
}
}
}
/**
* @brief remove recovery key to all encrypted files
*/
public function removeRecoveryKeys($path = '/') {
$dirContent = $this->view->getDirectoryContent($this->keyfilesPath . $path);
foreach ($dirContent as $item) {
2013-05-23 21:56:31 +00:00
// get relative path from files_encryption/keyfiles
$filePath = substr($item['path'], strlen('files_encryption/keyfiles'));
if ($item['type'] === 'dir') {
$this->removeRecoveryKeys($filePath . '/');
} else {
2013-05-29 10:23:33 +00:00
// remove '.key' extension from path e.g. 'file.txt.key' to 'file.txt'
$file = substr($filePath, 0, -4);
$this->view->unlink($this->shareKeysPath . '/' . $file . '.' . $this->recoveryKeyId . '.shareKey');
}
}
}
/**
* @brief decrypt given file with recovery key and encrypt it again to the owner and his new key
2013-05-19 23:24:36 +00:00
* @param string $file
* @param string $privateKey recovery key to decrypt the file
*/
private function recoverFile($file, $privateKey) {
$sharingEnabled = \OCP\Share::isEnabled();
// Find out who, if anyone, is sharing the file
if ($sharingEnabled) {
$result = \OCP\Share::getUsersSharingFile($file, $this->userId, true);
$userIds = $result['users'];
$userIds[] = $this->recoveryKeyId;
if ($result['public']) {
$userIds[] = $this->publicShareKeyId;
}
} else {
$userIds = array(
$this->userId,
$this->recoveryKeyId
);
}
$filteredUids = $this->filterShareReadyUsers($userIds);
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
//decrypt file key
$encKeyfile = $this->view->file_get_contents($this->keyfilesPath . $file . ".key");
$shareKey = $this->view->file_get_contents(
$this->shareKeysPath . $file . "." . $this->recoveryKeyId . ".shareKey");
$plainKeyfile = Crypt::multiKeyDecrypt($encKeyfile, $shareKey, $privateKey);
// encrypt file key again to all users, this time with the new public key for the recovered use
$userPubKeys = Keymanager::getPublicKeys($this->view, $filteredUids['ready']);
$multiEncKey = Crypt::multiKeyEncrypt($plainKeyfile, $userPubKeys);
// write new keys to filesystem TDOO!
$this->view->file_put_contents($this->keyfilesPath . $file . '.key', $multiEncKey['data']);
foreach ($multiEncKey['keys'] as $userId => $shareKey) {
2013-05-19 23:24:36 +00:00
$shareKeyPath = $this->shareKeysPath . $file . '.' . $userId . '.shareKey';
$this->view->file_put_contents($shareKeyPath, $shareKey);
}
// Return proxy to original status
\OC_FileProxy::$enabled = $proxyStatus;
}
/**
* @brief collect all files and recover them one by one
2013-05-19 23:24:36 +00:00
* @param string $path to look for files keys
* @param string $privateKey private recovery key which is used to decrypt the files
*/
private function recoverAllFiles($path, $privateKey) {
$dirContent = $this->view->getDirectoryContent($this->keyfilesPath . $path);
foreach ($dirContent as $item) {
2013-05-29 10:23:33 +00:00
// get relative path from files_encryption/keyfiles
$filePath = substr($item['path'], strlen('files_encryption/keyfiles'));
if ($item['type'] === 'dir') {
$this->recoverAllFiles($filePath . '/', $privateKey);
} else {
2013-05-29 10:23:33 +00:00
// remove '.key' extension from path e.g. 'file.txt.key' to 'file.txt'
$file = substr($filePath, 0, -4);
$this->recoverFile($file, $privateKey);
}
}
}
/**
* @brief recover users files in case of password lost
2013-05-19 23:24:36 +00:00
* @param string $recoveryPassword
*/
public function recoverUsersFiles($recoveryPassword) {
// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$encryptedKey = $this->view->file_get_contents(
'/owncloud_private_key/' . $this->recoveryKeyId . '.private.key');
$privateKey = Crypt::decryptPrivateKey($encryptedKey, $recoveryPassword);
\OC_FileProxy::$enabled = $proxyStatus;
$this->recoverAllFiles('/', $privateKey);
}
/**
* Get the path including the storage mount point
* @param int $id
* @return string the path including the mount point like AmazonS3/folder/file.txt
*/
public function getPathWithMountPoint($id) {
list($storage, $internalPath) = \OC\Files\Cache\Cache::getById($id);
$mount = \OC\Files\Filesystem::getMountByStorageId($storage);
$mountPoint = $mount[0]->getMountPoint();
2013-06-03 16:42:13 +00:00
$path = \OC\Files\Filesystem::normalizePath($mountPoint . '/' . $internalPath);
// reformat the path to be relative e.g. /user/files/folder becomes /folder/
$relativePath = \OCA\Encryption\Helper::stripUserFilesPath($path);
return $relativePath;
}
/**
* @brief check if the file is stored on a system wide mount point
* @param $path relative to /data/user with leading '/'
* @return boolean
*/
public function isSystemWideMountPoint($path) {
if (\OCP\App::isEnabled("files_external")) {
$mount = \OC_Mount_Config::getSystemMountPoints();
foreach ($mount as $mountPoint => $data) {
if ($mountPoint == substr($path, 1, strlen($mountPoint))) {
return true;
}
}
}
return false;
}
2013-07-29 15:06:05 +00:00
/**
* @brief decrypt private key and add it to the current session
* @param array $params with 'uid' and 'password'
* @return mixed session or false
*/
public function initEncryption($params) {
$session = new \OCA\Encryption\Session($this->view);
// we tried to initialize the encryption app for this session
$session->setInitialized(\OCA\Encryption\Session::INIT_EXECUTED);
2013-07-29 15:06:05 +00:00
$encryptedKey = Keymanager::getPrivateKey($this->view, $params['uid']);
$privateKey = Crypt::decryptPrivateKey($encryptedKey, $params['password']);
if ($privateKey === false) {
\OCP\Util::writeLog('Encryption library', 'Private key for user "' . $params['uid']
. '" is not valid! Maybe the user password was changed from outside if so please change it back to gain access', \OCP\Util::ERROR);
return false;
}
$session->setPrivateKey($privateKey);
$session->setInitialized(\OCA\Encryption\Session::INIT_SUCCESSFUL);
2013-07-29 15:06:05 +00:00
return $session;
}
}