nextcloud/apps/files_encryption/lib/proxy.php

402 lines
10 KiB
PHP
Raw Normal View History

2011-10-21 19:02:11 +04:00
<?php
/**
2015-03-26 13:44:34 +03:00
* @author Björn Schießle <schiessle@owncloud.com>
* @author Florin Peter <github@florin-peter.de>
* @author Joas Schilling <nickvergessen@owncloud.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
* @author Sam Tuke <mail@samtuke.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Vincent Petry <pvince81@owncloud.com>
*
2015-03-26 13:44:34 +03:00
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
2013-05-20 03:24:36 +04:00
*
2015-03-26 13:44:34 +03:00
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
2013-05-20 03:24:36 +04:00
*
2015-03-26 13:44:34 +03:00
* This program is distributed in the hope that it will be useful,
2013-05-20 03:24:36 +04:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2015-03-26 13:44:34 +03:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2013-05-20 03:24:36 +04:00
*
2015-03-26 13:44:34 +03:00
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
2013-05-20 03:24:36 +04:00
*
*/
/**
* Encryption proxy which handles filesystem operations before and after
* execution and encrypts, and handles keyfiles accordingly. Used for
* webui.
*/
namespace OCA\Files_Encryption;
2013-05-20 03:24:36 +04:00
/**
* Class Proxy
* @package OCA\Files_Encryption
2013-05-20 03:24:36 +04:00
*/
2013-05-27 19:26:58 +04:00
class Proxy extends \OC_FileProxy {
private static $unencryptedSizes = array(); // remember unencrypted size
private static $fopenMode = array(); // remember the fopen mode
private static $enableEncryption = false; // Enable encryption for the given path
2013-05-20 03:24:36 +04:00
/**
* check if path is excluded from encryption
*
* @param string $path relative to data/
* @return boolean
*/
protected function isExcludedPath($path) {
$view = new \OC\Files\View();
$normalizedPath = \OC\Files\Filesystem::normalizePath($path);
$parts = explode('/', $normalizedPath);
// we only encrypt/decrypt files in the files and files_versions folder
if (sizeof($parts) < 3) {
/**
* Less then 3 parts means, we can't match:
* - /{$uid}/files/* nor
* - /{$uid}/files_versions/*
* So this is not a path we are looking for.
*/
return true;
}
if(
!($parts[2] === 'files' && \OCP\User::userExists($parts[1])) &&
!($parts[2] === 'files_versions' && \OCP\User::userExists($parts[1]))) {
return true;
}
if (!$view->file_exists($normalizedPath)) {
$normalizedPath = dirname($normalizedPath);
}
// we don't encrypt server-to-server shares
list($storage, ) = \OC\Files\Filesystem::resolvePath($normalizedPath);
/**
* @var \OCP\Files\Storage $storage
*/
if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
return true;
}
return false;
}
2011-11-24 04:44:54 +04:00
/**
* Check if a file requires encryption
2011-11-24 04:44:54 +04:00
* @param string $path
* @param string $mode type of access
2011-11-24 04:44:54 +04:00
* @return bool
*
* Tests if server side encryption is enabled, and if we should call the
* crypt stream wrapper for the given file
2011-11-24 04:44:54 +04:00
*/
private function shouldEncrypt($path, $mode = 'w') {
2013-05-20 03:24:36 +04:00
// don't call the crypt stream wrapper, if...
if (
Crypt::mode() !== 'server' // we are not in server-side-encryption mode
|| $this->isExcludedPath($path) // if path is excluded from encryption
|| substr($path, 0, 8) === 'crypt://' // we are already in crypt mode
) {
2012-04-18 18:02:35 +04:00
return false;
}
2013-05-20 03:24:36 +04:00
$userId = Helper::getUser($path);
$view = new \OC\Files\View('');
$util = new Util($view, $userId);
2013-05-20 03:24:36 +04:00
// for write operation we always encrypt the files, for read operations
// we check if the existing file is encrypted or not decide if it needs to
// decrypt it.
if (($mode !== 'r' && $mode !== 'rb') || $util->isEncryptedPath($path)) {
2011-11-24 04:44:54 +04:00
return true;
}
2013-05-20 03:24:36 +04:00
return false;
2011-11-24 04:44:54 +04:00
}
2013-04-23 21:08:52 +04:00
2013-05-20 03:24:36 +04:00
/**
2014-05-13 15:29:25 +04:00
* @param string $path
* @param string $data
2013-05-20 03:24:36 +04:00
* @return bool
*/
2013-05-27 19:26:58 +04:00
public function preFile_put_contents($path, &$data) {
2013-05-20 03:24:36 +04:00
if ($this->shouldEncrypt($path)) {
2013-04-23 21:08:52 +04:00
2013-05-27 19:26:58 +04:00
if (!is_resource($data)) {
2013-04-23 21:08:52 +04:00
// get root view
$view = new \OC\Files\View('/');
2013-05-20 03:24:36 +04:00
// get relative path
$relativePath = Helper::stripUserFilesPath($path);
2013-05-20 03:24:36 +04:00
if (!isset($relativePath)) {
return true;
2013-05-16 02:31:17 +04:00
}
2013-04-23 21:08:52 +04:00
// create random cache folder
$cacheFolder = rand();
$path_slices = explode('/', \OC\Files\Filesystem::normalizePath($path));
$path_slices[2] = "cache/".$cacheFolder;
$tmpPath = implode('/', $path_slices);
$handle = fopen('crypt://' . $tmpPath, 'w');
if (is_resource($handle)) {
// write data to stream
fwrite($handle, $data);
2013-05-20 03:24:36 +04:00
// close stream
fclose($handle);
2013-05-20 03:24:36 +04:00
// disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
2013-05-20 03:24:36 +04:00
// get encrypted content
$data = $view->file_get_contents($tmpPath);
// store new unenecrypted size so that it can be updated
// in the post proxy
2013-12-16 18:24:11 +04:00
$tmpFileInfo = $view->getFileInfo($tmpPath);
2014-07-04 18:44:50 +04:00
if ( isset($tmpFileInfo['unencrypted_size']) ) {
self::$unencryptedSizes[\OC\Files\Filesystem::normalizePath($path)] = $tmpFileInfo['unencrypted_size'];
}
2013-12-16 18:24:11 +04:00
// remove our temp file
$view->deleteAll('/' . \OCP\User::getUser() . '/cache/' . $cacheFolder);
2013-05-20 03:24:36 +04:00
// re-enable proxy - our work is done
\OC_FileProxy::$enabled = $proxyStatus;
} else {
return false;
}
2011-10-21 19:02:11 +04:00
}
}
2013-04-23 21:08:52 +04:00
return true;
2013-05-20 03:24:36 +04:00
}
2013-05-20 03:24:36 +04:00
/**
* update file cache with the new unencrypted size after file was written
* @param string $path
* @param mixed $result
* @return mixed
*/
public function postFile_put_contents($path, $result) {
$normalizedPath = \OC\Files\Filesystem::normalizePath($path);
if ( isset(self::$unencryptedSizes[$normalizedPath]) ) {
$view = new \OC\Files\View('/');
$view->putFileInfo($normalizedPath,
array('encrypted' => true, 'unencrypted_size' => self::$unencryptedSizes[$normalizedPath]));
unset(self::$unencryptedSizes[$normalizedPath]);
}
return $result;
}
/**
* @param string $path Path of file from which has been read
* @param string $data Data that has been read from file
*/
2013-05-27 19:26:58 +04:00
public function postFile_get_contents($path, $data) {
2013-05-20 03:24:36 +04:00
$plainData = null;
2013-05-20 03:24:36 +04:00
// If data is a catfile
if (
Crypt::mode() === 'server'
&& $this->shouldEncrypt($path)
2013-05-27 19:26:58 +04:00
&& Crypt::isCatfileContent($data)
) {
2013-05-20 03:24:36 +04:00
2013-07-30 12:14:17 +04:00
$handle = fopen('crypt://' . $path, 'r');
2013-05-20 03:24:36 +04:00
if (is_resource($handle)) {
while (($plainDataChunk = fgets($handle, 8192)) !== false) {
$plainData .= $plainDataChunk;
}
}
}
2013-05-20 03:24:36 +04:00
2013-05-27 19:26:58 +04:00
if (!isset($plainData)) {
2013-05-20 03:24:36 +04:00
$plainData = $data;
2013-05-20 03:24:36 +04:00
}
2013-05-20 03:24:36 +04:00
return $plainData;
2013-05-20 03:24:36 +04:00
2011-10-21 19:02:11 +04:00
}
2013-05-20 03:24:36 +04:00
/**
* remember initial fopen mode because sometimes it gets changed during the request
* @param string $path path
* @param string $mode type of access
*/
public function preFopen($path, $mode) {
self::$fopenMode[$path] = $mode;
self::$enableEncryption = $this->shouldEncrypt($path, $mode);
}
2013-05-20 03:24:36 +04:00
/**
2014-05-13 15:29:25 +04:00
* @param string $path
2014-05-15 16:19:32 +04:00
* @param resource $result
2013-05-20 03:24:36 +04:00
* @return resource
*/
public function postFopen($path, $result) {
$path = \OC\Files\Filesystem::normalizePath($path);
if (!$result || self::$enableEncryption === false) {
2013-04-23 21:08:52 +04:00
2011-11-24 04:44:54 +04:00
return $result;
2013-05-20 03:24:36 +04:00
2011-11-24 04:44:54 +04:00
}
2013-04-23 21:08:52 +04:00
// if we remember the mode from the pre proxy we re-use it
// otherwise we fall back to stream_get_meta_data()
if (isset(self::$fopenMode[$path])) {
$mode = self::$fopenMode[$path];
unset(self::$fopenMode[$path]);
} else {
$meta = stream_get_meta_data($result);
$mode = $meta['mode'];
}
2013-05-20 03:24:36 +04:00
// Close the original encrypted file
fclose($result);
2013-05-20 03:24:36 +04:00
// Open the file using the crypto stream wrapper
// protocol and let it do the decryption work instead
$result = fopen('crypt://' . $path, $mode);
2013-05-20 03:24:36 +04:00
2011-11-24 04:44:54 +04:00
return $result;
2013-05-20 03:24:36 +04:00
2011-10-21 19:02:11 +04:00
}
2012-02-16 00:44:58 +04:00
2013-05-20 03:24:36 +04:00
/**
2014-05-13 15:29:25 +04:00
* @param string $path
* @param array $data
2013-05-20 03:24:36 +04:00
* @return array
*/
2013-05-27 19:26:58 +04:00
public function postGetFileInfo($path, $data) {
2013-05-20 03:24:36 +04:00
// if path is a folder do nothing
2014-01-17 17:38:14 +04:00
if (\OCP\App::isEnabled('files_encryption') && $data !== false && array_key_exists('size', $data)) {
2013-04-23 21:08:52 +04:00
2013-05-20 03:24:36 +04:00
// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
2013-04-23 21:08:52 +04:00
2013-05-20 03:24:36 +04:00
// get file size
$data['size'] = self::postFileSize($path, $data['size'], $data);
2013-04-23 21:08:52 +04:00
2013-05-20 03:24:36 +04:00
// Re-enable the proxy
\OC_FileProxy::$enabled = $proxyStatus;
}
2013-04-23 21:08:52 +04:00
2013-05-20 03:24:36 +04:00
return $data;
}
2013-04-23 21:08:52 +04:00
2013-05-20 03:24:36 +04:00
/**
2014-05-13 15:29:25 +04:00
* @param string $path
* @param int $size
* @return int|bool
2013-05-20 03:24:36 +04:00
*/
public function postFileSize($path, $size, $fileInfo = null) {
2013-04-23 21:08:52 +04:00
$view = new \OC\Files\View('/');
2013-04-23 21:08:52 +04:00
$userId = Helper::getUser($path);
$util = new Util($view, $userId);
// if encryption is no longer enabled or if the files aren't migrated yet
// we return the default file size
if(!\OCP\App::isEnabled('files_encryption') ||
$util->getMigrationStatus() !== Util::MIGRATION_COMPLETED) {
return $size;
}
2013-05-20 03:24:36 +04:00
// if path is a folder do nothing
2013-05-27 19:26:58 +04:00
if ($view->is_dir($path)) {
$proxyState = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$fileInfo = $view->getFileInfo($path);
\OC_FileProxy::$enabled = $proxyState;
if (isset($fileInfo['unencrypted_size']) && $fileInfo['unencrypted_size'] > 0) {
return $fileInfo['unencrypted_size'];
}
2013-05-20 03:24:36 +04:00
return $size;
}
// get relative path
$relativePath = Helper::stripUserFilesPath($path);
2013-05-07 15:42:08 +04:00
2013-05-20 03:24:36 +04:00
// if path is empty we cannot resolve anything
if (empty($relativePath)) {
2013-05-20 03:24:36 +04:00
return $size;
}
// get file info from database/cache
if (empty($fileInfo)) {
$proxyState = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
2013-05-27 19:26:58 +04:00
$fileInfo = $view->getFileInfo($path);
\OC_FileProxy::$enabled = $proxyState;
2013-05-21 23:09:25 +04:00
}
2013-05-20 03:24:36 +04:00
// if file is encrypted return real file size
if (isset($fileInfo['encrypted']) && $fileInfo['encrypted'] === true) {
// try to fix unencrypted file size if it doesn't look plausible
2013-10-16 13:24:56 +04:00
if ((int)$fileInfo['size'] > 0 && (int)$fileInfo['unencrypted_size'] === 0 ) {
$fixSize = $util->getFileSize($path);
$fileInfo['unencrypted_size'] = $fixSize;
// put file info if not .part file
if (!Helper::isPartialFilePath($relativePath)) {
$view->putFileInfo($path, array('unencrypted_size' => $fixSize));
}
}
2013-05-20 03:24:36 +04:00
$size = $fileInfo['unencrypted_size'];
} else {
$fileInfoUpdates = array();
2013-05-20 03:24:36 +04:00
2013-05-27 19:26:58 +04:00
$fixSize = $util->getFileSize($path);
if ($fixSize > 0) {
2013-05-21 23:09:25 +04:00
$size = $fixSize;
2013-05-20 03:24:36 +04:00
$fileInfoUpdates['encrypted'] = true;
$fileInfoUpdates['unencrypted_size'] = $size;
2013-05-21 23:09:25 +04:00
// put file info if not .part file
if (!Helper::isPartialFilePath($relativePath)) {
$view->putFileInfo($path, $fileInfoUpdates);
2013-05-20 03:24:36 +04:00
}
}
2013-05-21 23:09:25 +04:00
2013-05-20 03:24:36 +04:00
}
return $size;
}
2013-04-30 22:44:42 +04:00
2013-05-20 03:24:36 +04:00
}