]> source.dussan.org Git - nextcloud-server.git/commitdiff
improved error messaging, initial commit
authorBjoern Schiessle <schiessle@owncloud.com>
Fri, 30 Aug 2013 08:17:50 +0000 (10:17 +0200)
committerBjoern Schiessle <schiessle@owncloud.com>
Fri, 30 Aug 2013 08:17:50 +0000 (10:17 +0200)
apps/files_encryption/appinfo/database.xml
apps/files_encryption/appinfo/version
apps/files_encryption/files/error.php
apps/files_encryption/hooks/hooks.php
apps/files_encryption/lib/helper.php
apps/files_encryption/lib/stream.php
apps/files_encryption/lib/util.php

index 4587930da0a3c25fb3eecbb4c39afebf9de866ec..cd5434b8c27145d3f0733bc85e70e55f57602d0e 100644 (file)
                                <default>0</default>
                                <comments>Whether encryption migration has been performed</comments>
                        </field>
+                       <field>
+                               <name>initialized</name>
+                               <type>integer</type>
+                               <notnull>true</notnull>
+                               <default>0</default>
+                               <comments>Did the user initialized the encryption app at least once</comments>
+                       </field>
                </declaration>
        </table>
 </database>
\ No newline at end of file
index bd73f47072b1fe4b9914ec14a7f6d47fcc8f816a..2eb3c4fe4eebcdea3da0790cc0ba74cb286ec4f4 100644 (file)
@@ -1 +1 @@
-0.4
+0.5
index 2dd27257abe45381c9cf357ffa51e23da9f1df91..7a2bb1a2811d9b1e345966f3c85c5f4a449d293f 100644 (file)
@@ -4,7 +4,7 @@ if (!isset($_)) { //also provide standalone error page
 
        $l = OC_L10N::get('files_encryption');
 
-       $errorMsg = $l->t('Your private key is not valid! Likely your password was changed outside the ownCloud system (e.g. your corporate directory). You can update your private key password in your personal settings to recover access to your encrypted files.');
+       $errorMsg = $l->t('Your private key is not valid! Maybe the encryption app was re-enabled during your session. Please try to log out and log back in to initialize the encryption app. If this doesn\'t help maybe your password was changed outside the ownCloud system (e.g. your corporate directory). You can update your private key password in your personal settings to recover access to your encrypted files.');
 
        if(isset($_GET['p']) && $_GET['p'] === '1') {
                header('HTTP/1.0 404 ' . $errorMsg);
index de306462d79062a7fcd7ba876a9e8e624936dc06..aefb274e1c471c9630ae66b8a8c5f56109171827 100644 (file)
@@ -70,9 +70,11 @@ class Hooks {
                // If migration not yet done\r
                if ($ready) {\r
 \r
+                       $util->setInitialized(Util::ENCRYPTION_INITIALIZED);\r
+\r
                        $userView = new \OC_FilesystemView('/' . $params['uid']);\r
 \r
-                       // Set legacy encryption key if it exists, to support \r
+                       // Set legacy encryption key if it exists, to support\r
                        // depreciated encryption system\r
                        if (\r
                                $userView->file_exists('encryption.key')\r
@@ -143,6 +145,7 @@ class Hooks {
         * @brief If the password can't be changed within ownCloud, than update the key password in advance.\r
         */\r
        public static function preSetPassphrase($params) {\r
+               return true;\r
                if ( ! \OC_User::canUserChangePassword($params['uid']) ) {\r
                        self::setPassphrase($params);\r
                }\r
@@ -153,7 +156,7 @@ class Hooks {
         * @param array $params keys: uid, password\r
         */\r
        public static function setPassphrase($params) {\r
-\r
+               return true;\r
                // Only attempt to change passphrase if server-side encryption\r
                // is in use (client-side encryption does not have access to\r
                // the necessary keys)\r
@@ -248,7 +251,7 @@ class Hooks {
                        $params['run'] = false;\r
                        $params['error'] = $l->t('Following users are not set up for encryption:') . ' ' . join(', ' , $notConfigured);\r
                }\r
-               \r
+\r
        }\r
 \r
        /**\r
@@ -259,7 +262,7 @@ class Hooks {
                // NOTE: $params has keys:\r
                // [itemType] => file\r
                // itemSource -> int, filecache file ID\r
-               // [parent] => \r
+               // [parent] =>\r
                // [itemTarget] => /13\r
                // shareWith -> string, uid of user being shared to\r
                // fileTarget -> path of file being shared\r
@@ -300,13 +303,13 @@ class Hooks {
                                        // NOTE: parent is folder but shared was a file!\r
                                        // we try to rebuild the missing path\r
                                        // some examples we face here\r
-                                       // user1 share folder1 with user2 folder1 has \r
-                                       // the following structure \r
+                                       // user1 share folder1 with user2 folder1 has\r
+                                       // the following structure\r
                                        // /folder1/subfolder1/subsubfolder1/somefile.txt\r
                                        // user2 re-share subfolder2 with user3\r
                                        // user3 re-share somefile.txt user4\r
-                                       // so our path should be \r
-                                       // /Shared/subfolder1/subsubfolder1/somefile.txt \r
+                                       // so our path should be\r
+                                       // /Shared/subfolder1/subsubfolder1/somefile.txt\r
                                        // while user3 is sharing\r
 \r
                                        if ($params['itemType'] === 'file') {\r
@@ -537,14 +540,18 @@ class Hooks {
        }\r
 \r
        /**\r
-        * set migration status back to '0' so that all new files get encrypted\r
+        * set migration status and the init status back to '0' so that all new files get encrypted\r
         * if the app gets enabled again\r
         * @param array $params contains the app ID\r
         */\r
        public static function preDisable($params) {\r
                if ($params['app'] === 'files_encryption') {\r
-                       $query = \OC_DB::prepare('UPDATE `*PREFIX*encryption` SET `migration_status`=0');\r
-                       $query->execute();\r
+\r
+                       $setMigrationStatus = \OC_DB::prepare('UPDATE `*PREFIX*encryption` SET `migration_status`=0');\r
+                       $setMigrationStatus->execute();\r
+\r
+                       $setInitStatus = \OC_DB::prepare('UPDATE `*PREFIX*encryption` SET `initialized`=0');\r
+                       $setInitStatus->execute();\r
                }\r
        }\r
 \r
index 0209a5d18b78a555bfd17397428aed58cded46e1..105c5357e9612dcae9e4443b5f751e738902bb04 100755 (executable)
@@ -199,12 +199,12 @@ class Helper {
        public static function stripUserFilesPath($path) {
                $trimmed = ltrim($path, '/');
                $split = explode('/', $trimmed);
-               
+
                // it is not a file relative to data/user/files
                if (count($split) < 3 || $split[1] !== 'files') {
                        return false;
                }
-               
+
                $sliced = array_slice($split, 2);
                $relPath = implode('/', $sliced);
 
@@ -219,30 +219,46 @@ class Helper {
        public static function getPathToRealFile($path) {
                $trimmed = ltrim($path, '/');
                $split = explode('/', $trimmed);
-               
+
                if (count($split) < 3 || $split[1] !== "files_versions") {
                        return false;
                }
-               
+
                $sliced = array_slice($split, 2);
                $realPath = implode('/', $sliced);
                //remove the last .v
                $realPath = substr($realPath, 0, strrpos($realPath, '.v'));
 
                return $realPath;
-       }       
-       
+       }
+
        /**
         * @brief redirect to a error page
         */
-       public static function redirectToErrorPage() {
-               $location = \OC_Helper::linkToAbsolute('apps/files_encryption/files', 'error.php');
-               $post = 0;
+       public static function redirectToErrorPage($util) {
+
+               $l = \OC_L10N::get('files_encryption');
+
+               if ($util->getInitialized() === false) {
+                       $errorMsg = $l->t('Encryption app not initialized! Maybe the encryption app was re-enabled during your session. Please try to log out and log back in to initialize the encryption app.');
+               } else {
+                       $errorMsg = $l->t('Your private key is not valid! Likely your password was changed outside the ownCloud system (e.g. your corporate directory). You can update your private key password in your personal settings to recover access to your encrypted files.');
+               }
+
                if(count($_POST) > 0) {
-                       $post = 1;
+                       header('HTTP/1.0 404 ' . $errorMsg);
+               }
+
+               // check if ajax request
+               if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
+                       \OCP\JSON::error(array('data' => array('message' => $errorMsg)));
+               } else {
+                       header('HTTP/1.0 404 ' . $errorMsg);
+                       $tmpl = new OC_Template('files_encryption', 'invalid_private_key', 'guest');
+                       $tmpl->printPage();
                }
-               header('Location: ' . $location . '?p=' . $post);
-               exit();
+
+               exit;
        }
 
        /**
@@ -259,7 +275,7 @@ class Helper {
 
                return (bool) $result;
        }
-       
+
        /**
         * check some common errors if the server isn't configured properly for encryption
         * @return bool true if configuration seems to be OK
index 335ea3733eb2f90565bfe2e2098dbe82876732e3..87b8dc3ee2f8e08d034c5bd7746b1999f7d8aeb5 100644 (file)
@@ -81,7 +81,7 @@ class Stream {
         * @return bool
         */
        public function stream_open($path, $mode, $options, &$opened_path) {
-               
+
                // assume that the file already exist before we decide it finally in getKey()
                $this->newFile = false;
 
@@ -106,12 +106,12 @@ class Stream {
                if ($this->relPath === false) {
                        $this->relPath = Helper::getPathToRealFile($this->rawPath);
                }
-               
+
                if($this->relPath === false) {
                        \OCP\Util::writeLog('Encryption library', 'failed to open file "' . $this->rawPath . '" expecting a path to user/files or to user/files_versions', \OCP\Util::ERROR);
                        return false;
                }
-               
+
                // Disable fileproxies so we can get the file size and open the source file without recursive encryption
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;
@@ -131,7 +131,7 @@ class Stream {
 
                        if($this->privateKey === false) {
                                // if private key is not valid redirect user to a error page
-                               \OCA\Encryption\Helper::redirectToErrorPage();
+                               \OCA\Encryption\Helper::redirectToErrorPage($util);
                        }
 
                        $this->size = $this->rootView->filesize($this->rawPath, $mode);
@@ -272,7 +272,7 @@ class Stream {
                } else {
 
                        $this->newFile = true;
-                       
+
                        return false;
 
                }
@@ -296,9 +296,9 @@ class Stream {
                        return strlen($data);
                }
 
-               // Disable the file proxies so that encryption is not 
-               // automatically attempted when the file is written to disk - 
-               // we are handling that separately here and we don't want to 
+               // Disable the file proxies so that encryption is not
+               // automatically attempted when the file is written to disk -
+               // we are handling that separately here and we don't want to
                // get into an infinite loop
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;
@@ -311,7 +311,7 @@ class Stream {
                $pointer = ftell($this->handle);
 
                // Get / generate the keyfile for the file we're handling
-               // If we're writing a new file (not overwriting an existing 
+               // If we're writing a new file (not overwriting an existing
                // one), save the newly generated keyfile
                if (!$this->getKey()) {
 
@@ -319,7 +319,7 @@ class Stream {
 
                }
 
-               // If extra data is left over from the last round, make sure it 
+               // If extra data is left over from the last round, make sure it
                // is integrated into the next 6126 / 8192 block
                if ($this->writeCache) {
 
@@ -344,12 +344,12 @@ class Stream {
                        if ($remainingLength < 6126) {
 
                                // Set writeCache to contents of $data
-                               // The writeCache will be carried over to the 
-                               // next write round, and added to the start of 
-                               // $data to ensure that written blocks are 
-                               // always the correct length. If there is still 
-                               // data in writeCache after the writing round 
-                               // has finished, then the data will be written 
+                               // The writeCache will be carried over to the
+                               // next write round, and added to the start of
+                               // $data to ensure that written blocks are
+                               // always the correct length. If there is still
+                               // data in writeCache after the writing round
+                               // has finished, then the data will be written
                                // to disk by $this->flush().
                                $this->writeCache = $data;
 
@@ -363,7 +363,7 @@ class Stream {
 
                                $encrypted = $this->preWriteEncrypt($chunk, $this->plainKey);
 
-                               // Write the data chunk to disk. This will be 
+                               // Write the data chunk to disk. This will be
                                // attended to the last data chunk if the file
                                // being handled totals more than 6126 bytes
                                fwrite($this->handle, $encrypted);
index b8d6862349395efa1a02b7039b267cee87faa700..edb9564e73abd33ed8762bbe10274c7b5c3cdd65 100644 (file)
@@ -37,6 +37,8 @@ class Util {
        const MIGRATION_IN_PROGRESS = -1; // migration is running
        const MIGRATION_OPEN = 0;         // user still needs to be migrated
 
+       const ENCRYPTION_INITIALIZED = 1;
+       const ENCRYPTION_NOT_INITIALIZED = 0;
 
        private $view; // OC_FilesystemView object for filesystem operations
        private $userId; // ID of the currently logged-in user
@@ -1216,6 +1218,56 @@ class Util {
                return $return;
        }
 
+       /**
+        * set remember if the encryption app was already initialized or not
+        * @param type $status
+        */
+       public function setInitialized($status) {
+               $sql = 'UPDATE `*PREFIX*encryption` SET `initialized` = ? WHERE `uid` = ?';
+               $args = array($status, $this->userId);
+               $query = \OCP\DB::prepare($sql);
+               $query->execute($args);
+       }
+
+       /**
+        * set remember if the encryption app was already initialized or not
+        */
+       public function getInitialized() {
+               $sql = 'SELECT `initialized` FROM `*PREFIX*encryption` WHERE `uid` = ?';
+               $args = array($this->userId);
+               $query = \OCP\DB::prepare($sql);
+
+               $result = $query->execute($args);
+               $initializedStatus = null;
+
+               if (\OCP\DB::isError($result)) {
+                       \OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
+               } else {
+                       if ($result->numRows() > 0) {
+                               $row = $result->fetchRow();
+                               if (isset($row['initialized'])) {
+                                       $initializedStatus = (int)$row['initialized'];
+                               }
+                       }
+               }
+
+               // If no record is found
+               if (empty($initializedStatus)) {
+                       \OCP\Util::writeLog('Encryption library', "Could not get initialized status for " . $this->userId . ", no record found", \OCP\Util::ERROR);
+                       return false;
+                       // If a record is found
+               } else {
+                       return (bool)$initializedStatus;
+               }
+
+
+
+               $sql = 'UPDATE `*PREFIX*encryption` SET `initialized` = ? WHERE `uid` = ?';
+               $args = array($status, $this->userId);
+               $query = \OCP\DB::prepare($sql);
+               $query->execute($args);
+       }
+
        /**
         * @brief close migration mode after users data has been encrypted successfully
         * @return boolean