Beispiel #1
0
 function testDelAllShareKeysFile()
 {
     $this->view->mkdir('/' . Test_Encryption_Keymanager::TEST_USER . '/files/folder1');
     $this->view->file_put_contents('/' . Test_Encryption_Keymanager::TEST_USER . '/files/folder1/existingFile.txt', 'data');
     // create folder structure for some dummy share key files
     $this->view->mkdir('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1');
     // create some dummy share keys for the existing file
     $this->view->file_put_contents('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/existingFile.txt.user1.shareKey', 'data');
     $this->view->file_put_contents('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/existingFile.txt.user2.shareKey', 'data');
     $this->view->file_put_contents('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/existingFile.txt.user3.shareKey', 'data');
     $this->view->file_put_contents('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/existingFile.txt.' . Test_Encryption_Keymanager::TEST_USER . '.shareKey', 'data');
     // create some dummy share keys for a non-existing file
     $this->view->file_put_contents('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/nonexistingFile.txt.user1.shareKey', 'data');
     $this->view->file_put_contents('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/nonexistingFile.txt.user2.shareKey', 'data');
     $this->view->file_put_contents('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/nonexistingFile.txt.user3.shareKey', 'data');
     $this->view->file_put_contents('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/nonexistingFile.txt.' . Test_Encryption_Keymanager::TEST_USER . '.shareKey', 'data');
     // try to del all share keys from a existing file, should fail because the file still exists
     $result = Encryption\Keymanager::delAllShareKeys($this->view, Test_Encryption_Keymanager::TEST_USER, 'folder1/existingFile.txt');
     $this->assertFalse($result);
     // check if share keys still exists
     $this->assertTrue($this->view->file_exists('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/existingFile.txt.' . Test_Encryption_Keymanager::TEST_USER . '.shareKey'));
     $this->assertTrue($this->view->file_exists('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/existingFile.txt.user1.shareKey'));
     $this->assertTrue($this->view->file_exists('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/existingFile.txt.user2.shareKey'));
     $this->assertTrue($this->view->file_exists('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/existingFile.txt.user3.shareKey'));
     // try to del all share keys froma file, should fail because the file still exists
     $result2 = Encryption\Keymanager::delAllShareKeys($this->view, Test_Encryption_Keymanager::TEST_USER, 'folder1/nonexistingFile.txt');
     $this->assertTrue($result2);
     // check if share keys are really gone
     $this->assertFalse($this->view->file_exists('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/nonexistingFile.txt.' . Test_Encryption_Keymanager::TEST_USER . '.shareKey'));
     $this->assertFalse($this->view->file_exists('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/nonexistingFile.txt.user1.shareKey'));
     $this->assertFalse($this->view->file_exists('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/nonexistingFile.txt.user2.shareKey'));
     $this->assertFalse($this->view->file_exists('/' . Test_Encryption_Keymanager::TEST_USER . '/files_encryption/share-keys/folder1/nonexistingFile.txt.user3.shareKey'));
     // cleanup
     $this->view->deleteAll('/' . Test_Encryption_Keymanager::TEST_USER . '/files/folder1');
 }
Beispiel #2
0
 function testDescryptAllWithBrokenFiles()
 {
     $file1 = "/decryptAll1" . uniqid() . ".txt";
     $file2 = "/decryptAll2" . uniqid() . ".txt";
     $util = new Encryption\Util($this->view, $this->userId);
     $this->view->file_put_contents($this->userId . '/files/' . $file1, $this->dataShort);
     $this->view->file_put_contents($this->userId . '/files/' . $file2, $this->dataShort);
     $fileInfoEncrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1);
     $fileInfoEncrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2);
     $this->assertTrue($fileInfoEncrypted1 instanceof \OC\Files\FileInfo);
     $this->assertTrue($fileInfoEncrypted2 instanceof \OC\Files\FileInfo);
     $this->assertEquals($fileInfoEncrypted1['encrypted'], 1);
     $this->assertEquals($fileInfoEncrypted2['encrypted'], 1);
     // rename keyfile for file1 so that the decryption for file1 fails
     // Expected behaviour: decryptAll() returns false, file2 gets decrypted anyway
     $this->view->rename($this->userId . '/files_encryption/keyfiles/' . $file1 . '.key', $this->userId . '/files_encryption/keyfiles/' . $file1 . '.key.moved');
     // decrypt all encrypted files
     $result = $util->decryptAll();
     $this->assertFalse($result);
     $fileInfoUnencrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1);
     $fileInfoUnencrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2);
     $this->assertTrue($fileInfoUnencrypted1 instanceof \OC\Files\FileInfo);
     $this->assertTrue($fileInfoUnencrypted2 instanceof \OC\Files\FileInfo);
     // file1 should be still encrypted; file2 should be decrypted
     $this->assertEquals(1, $fileInfoUnencrypted1['encrypted']);
     $this->assertEquals(0, $fileInfoUnencrypted2['encrypted']);
     // keyfiles and share keys should still exist
     $this->assertTrue($this->view->is_dir($this->userId . '/files_encryption/keyfiles/'));
     $this->assertTrue($this->view->is_dir($this->userId . '/files_encryption/share-keys/'));
     // rename the keyfile for file1 back
     $this->view->rename($this->userId . '/files_encryption/keyfiles/' . $file1 . '.key.moved', $this->userId . '/files_encryption/keyfiles/' . $file1 . '.key');
     // try again to decrypt all encrypted files
     $result = $util->decryptAll();
     $this->assertTrue($result);
     $fileInfoUnencrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1);
     $fileInfoUnencrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2);
     $this->assertTrue($fileInfoUnencrypted1 instanceof \OC\Files\FileInfo);
     $this->assertTrue($fileInfoUnencrypted2 instanceof \OC\Files\FileInfo);
     // now both files should be decrypted
     $this->assertEquals(0, $fileInfoUnencrypted1['encrypted']);
     $this->assertEquals(0, $fileInfoUnencrypted2['encrypted']);
     // keyfiles and share keys should be deleted
     $this->assertFalse($this->view->is_dir($this->userId . '/files_encryption/keyfiles/'));
     $this->assertFalse($this->view->is_dir($this->userId . '/files_encryption/share-keys/'));
     //cleanup
     $this->view->unlink($this->userId . '/files/' . $file1);
     $this->view->unlink($this->userId . '/files/' . $file2);
     $this->view->deleteAll($this->userId . '/files_encryption/keyfiles.backup');
     $this->view->deleteAll($this->userId . '/files_encryption/share-keys.backup');
 }
Beispiel #3
0
 public function createVideo($title, $files, $theme)
 {
     $title = $this->cleanUpTitle($title);
     $xml = new XML($title, $files, $theme, $this->current_user, $this->app, $this->settings);
     $error = null;
     $result = $xml->setProducers();
     if ($result) {
         exec('cd /home/camila/Projects/Owncloud/owncloud/apps/popcornapp/themes/ && melt6 -producer xml:' . $title . '.xml -consumer avformat:' . $title . '.ogg');
         $xml_view = new \OC\Files\View('/' . $this->current_user . '/files');
         $xml_view->mkdir('popcornapp');
         $content = fopen('/home/camila/Projects/Owncloud/owncloud/apps/popcornapp/themes/' . $title . '.ogg', 'r+');
         $xml_view->file_put_contents('/popcornapp/' . $title . '.ogg', $content);
     } else {
         $error = 'Something went wrong! We all are going to die!';
     }
     return new DataResponse(['src' => $title . '.ogg', 'error' => $error]);
 }
 /**
  * test deletion of a folder which contains share mount points. Share mount
  * points should be unshared before the folder gets deleted so
  * that the mount point doesn't end up at the trash bin
  */
 function testDeleteParentFolder()
 {
     $status = \OC_App::isEnabled('files_trashbin');
     \OC_App::enable('files_trashbin');
     \OCA\Files_Trashbin\Trashbin::registerHooks();
     OC_FileProxy::register(new OCA\Files\Share\Proxy());
     $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder);
     $this->assertTrue($fileinfo instanceof \OC\Files\FileInfo);
     \OCP\Share::shareItem('folder', $fileinfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 31);
     $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
     $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
     // check if user2 can see the shared folder
     $this->assertTrue($view->file_exists($this->folder));
     $foldersShared = \OCP\Share::getItemsSharedWith('folder');
     $this->assertSame(1, count($foldersShared));
     $view->mkdir("localFolder");
     $view->file_put_contents("localFolder/localFile.txt", "local file");
     $view->rename($this->folder, 'localFolder/' . $this->folder);
     // share mount point should now be moved to the subfolder
     $this->assertFalse($view->file_exists($this->folder));
     $this->assertTrue($view->file_exists('localFolder/' . $this->folder));
     $view->unlink('localFolder');
     $this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
     // shared folder should be unshared
     $foldersShared = \OCP\Share::getItemsSharedWith('folder');
     $this->assertTrue(empty($foldersShared));
     // trashbin should contain the local file but not the mount point
     $rootView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
     $trashContent = \OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_FILES_SHARING_API_USER2);
     $this->assertSame(1, count($trashContent));
     $firstElement = reset($trashContent);
     $timestamp = $firstElement['mtime'];
     $this->assertTrue($rootView->file_exists('files_trashbin/files/localFolder.d' . $timestamp . '/localFile.txt'));
     $this->assertFalse($rootView->file_exists('files_trashbin/files/localFolder.d' . $timestamp . '/' . $this->folder));
     //cleanup
     $rootView->deleteAll('files_trashin');
     if ($status === false) {
         \OC_App::disable('files_trashbin');
     }
     \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
 }
Beispiel #5
0
 /**
  * @medium
  */
 function testRenamePartFile()
 {
     // share to user
     $fileinfo = $this->view->getFileInfo($this->folder);
     $result = \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 31);
     $this->assertTrue($result);
     self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
     $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
     $this->assertTrue($user2View->file_exists($this->folder));
     // create part file
     $result = $user2View->file_put_contents($this->folder . '/foo.txt.part', 'some test data');
     $this->assertTrue(is_int($result));
     // rename part file to real file
     $result = $user2View->rename($this->folder . '/foo.txt.part', $this->folder . '/foo.txt');
     $this->assertTrue($result);
     // check if the new file really exists
     $this->assertTrue($user2View->file_exists($this->folder . '/foo.txt'));
     // check if the rename also affected the owner
     self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
     $this->assertTrue($this->view->file_exists($this->folder . '/foo.txt'));
     //cleanup
     \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2);
 }
Beispiel #6
0
 function testStreamSetWriteBuffer()
 {
     $filename = '/tmp-' . time();
     $view = new \OC\Files\View('/' . $this->userId . '/files');
     // Save short data as encrypted file using stream wrapper
     $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
     // Test that data was successfully written
     $this->assertTrue(is_int($cryptedFile));
     $handle = $view->fopen($filename, 'r');
     // set stream options
     $this->assertEquals(0, stream_set_write_buffer($handle, 1024));
     // tear down
     $view->unlink($filename);
 }
Beispiel #7
0
 /**
  * Change a user's encryption passphrase
  * @param array $params keys: uid, password
  */
 public static function setPassphrase($params)
 {
     if (\OCP\App::isEnabled('files_encryption') === false) {
         return true;
     }
     // Only attempt to change passphrase if server-side encryption
     // is in use (client-side encryption does not have access to
     // the necessary keys)
     if (Crypt::mode() === 'server') {
         $view = new \OC\Files\View('/');
         if ($params['uid'] === \OCP\User::getUser()) {
             $session = new \OCA\Encryption\Session($view);
             // Get existing decrypted private key
             $privateKey = $session->getPrivateKey();
             // Encrypt private key with new user pwd as passphrase
             $encryptedPrivateKey = Crypt::symmetricEncryptFileContent($privateKey, $params['password']);
             // Save private key
             Keymanager::setPrivateKey($encryptedPrivateKey);
             // NOTE: Session does not need to be updated as the
             // private key has not changed, only the passphrase
             // used to decrypt it has changed
         } else {
             // admin changed the password for a different user, create new keys and reencrypt file keys
             $user = $params['uid'];
             $util = new Util($view, $user);
             $recoveryPassword = isset($params['recoveryPassword']) ? $params['recoveryPassword'] : null;
             // we generate new keys if...
             // ...we have a recovery password and the user enabled the recovery key
             // ...encryption was activated for the first time (no keys exists)
             // ...the user doesn't have any files
             if ($util->recoveryEnabledForUser() && $recoveryPassword || !$util->userKeysExists() || !$view->file_exists($user . '/files')) {
                 $newUserPassword = $params['password'];
                 // make sure that the users home is mounted
                 \OC\Files\Filesystem::initMountPoints($user);
                 $keypair = Crypt::createKeypair();
                 // Disable encryption proxy to prevent recursive calls
                 $proxyStatus = \OC_FileProxy::$enabled;
                 \OC_FileProxy::$enabled = false;
                 // Save public key
                 $view->file_put_contents('/public-keys/' . $user . '.public.key', $keypair['publicKey']);
                 // Encrypt private key empty passphrase
                 $encryptedPrivateKey = Crypt::symmetricEncryptFileContent($keypair['privateKey'], $newUserPassword);
                 // Save private key
                 $view->file_put_contents('/' . $user . '/files_encryption/' . $user . '.private.key', $encryptedPrivateKey);
                 if ($recoveryPassword) {
                     // if recovery key is set we can re-encrypt the key files
                     $util = new Util($view, $user);
                     $util->recoverUsersFiles($recoveryPassword);
                 }
                 \OC_FileProxy::$enabled = $proxyStatus;
             }
         }
     }
 }
Beispiel #8
0
 /**
  * store private key from the user
  * @param string $key
  * @return bool
  * @note Encryption of the private key must be performed by client code
  * as no encryption takes place here
  */
 public static function setPrivateKey($key)
 {
     $user = \OCP\User::getUser();
     $view = new \OC\Files\View('/' . $user . '/files_encryption');
     $proxyStatus = \OC_FileProxy::$enabled;
     \OC_FileProxy::$enabled = false;
     if (!$view->file_exists('')) {
         $view->mkdir('');
     }
     $result = $view->file_put_contents($user . '.private.key', $key);
     \OC_FileProxy::$enabled = $proxyStatus;
     return $result;
 }
Beispiel #9
0
 /**
  * @brief enable recovery
  *
  * @param $recoveryKeyId
  * @param $recoveryPassword
  * @internal param \OCA\Encryption\Util $util
  * @internal param string $password
  * @return bool
  */
 public static function adminEnableRecovery($recoveryKeyId, $recoveryPassword)
 {
     $view = new \OC\Files\View('/');
     if ($recoveryKeyId === null) {
         $recoveryKeyId = 'recovery_' . substr(md5(time()), 0, 8);
         \OC_Appconfig::setValue('files_encryption', 'recoveryKeyId', $recoveryKeyId);
     }
     if (!$view->is_dir('/owncloud_private_key')) {
         $view->mkdir('/owncloud_private_key');
     }
     if (!$view->file_exists("/public-keys/" . $recoveryKeyId . ".public.key") || !$view->file_exists("/owncloud_private_key/" . $recoveryKeyId . ".private.key")) {
         $keypair = \OCA\Encryption\Crypt::createKeypair();
         \OC_FileProxy::$enabled = false;
         // Save public key
         if (!$view->is_dir('/public-keys')) {
             $view->mkdir('/public-keys');
         }
         $view->file_put_contents('/public-keys/' . $recoveryKeyId . '.public.key', $keypair['publicKey']);
         // Encrypt private key empty passphrase
         $encryptedPrivateKey = \OCA\Encryption\Crypt::symmetricEncryptFileContent($keypair['privateKey'], $recoveryPassword);
         // Save private key
         $view->file_put_contents('/owncloud_private_key/' . $recoveryKeyId . '.private.key', $encryptedPrivateKey);
         \OC_FileProxy::$enabled = true;
         // Set recoveryAdmin as enabled
         \OC_Appconfig::setValue('files_encryption', 'recoveryAdminEnabled', 1);
         $return = true;
     } else {
         // get recovery key and check the password
         $util = new \OCA\Encryption\Util(new \OC_FilesystemView('/'), \OCP\User::getUser());
         $return = $util->checkRecoveryPassword($recoveryPassword);
         if ($return) {
             \OC_Appconfig::setValue('files_encryption', 'recoveryAdminEnabled', 1);
         }
     }
     return $return;
 }
Beispiel #10
0
 /**
  * Tests whether share keys can be found
  *
  * @dataProvider userNamesProvider
  */
 function testFindShareKeys($userName)
 {
     self::setUpUsers();
     // note: not using dataProvider as we want to make
     // sure that the correct keys are match and not any
     // other ones that might happen to have similar names
     self::setupHooks();
     self::loginHelper($userName, true);
     $testDir = 'testFindShareKeys' . $this->getUniqueID() . '/';
     $baseDir = $userName . '/files/' . $testDir;
     $fileList = array('t est.txt', 't est_.txt', 't est.doc.txt', 't est(.*).txt', 'multiple.dots.can.happen.too.txt', 't est.' . $userName . '.txt', 't est_.' . $userName . '.shareKey.txt', 'who would upload their.shareKey', 'user ones file.txt', 'user ones file.txt.backup', '.t est.txt');
     $rootView = new \OC\Files\View('/');
     $rootView->mkdir($baseDir);
     foreach ($fileList as $fileName) {
         $rootView->file_put_contents($baseDir . $fileName, 'dummy');
     }
     $shareKeysDir = $userName . '/files_encryption/share-keys/' . $testDir;
     foreach ($fileList as $fileName) {
         // make sure that every file only gets its correct respective keys
         $result = Encryption\Helper::findShareKeys($baseDir . $fileName, $shareKeysDir . $fileName, $rootView);
         $this->assertEquals(array($shareKeysDir . $fileName . '.' . $userName . '.shareKey'), $result);
     }
     self::cleanUpUsers();
 }
Beispiel #11
0
 public function testLongPath()
 {
     $storage = new \OC\Files\Storage\Temporary(array());
     \OC\Files\Filesystem::mount($storage, array(), '/');
     $rootView = new \OC\Files\View('');
     $longPath = '';
     // 4000 is the maximum path length in file_cache.path
     $folderName = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789';
     $depth = 4000 / 57;
     foreach (range(0, $depth - 1) as $i) {
         $longPath .= '/' . $folderName;
         $result = $rootView->mkdir($longPath);
         $this->assertTrue($result, "mkdir failed on {$i} - path length: " . strlen($longPath));
         $result = $rootView->file_put_contents($longPath . '/test.txt', 'lorem');
         $this->assertEquals(5, $result, "file_put_contents failed on {$i}");
         $this->assertTrue($rootView->file_exists($longPath));
         $this->assertTrue($rootView->file_exists($longPath . '/test.txt'));
     }
     $cache = $storage->getCache();
     $scanner = $storage->getScanner();
     $scanner->scan('');
     $longPath = $folderName;
     foreach (range(0, $depth - 1) as $i) {
         $cachedFolder = $cache->get($longPath);
         $this->assertTrue(is_array($cachedFolder), "No cache entry for folder at {$i}");
         $this->assertEquals($folderName, $cachedFolder['name'], "Wrong cache entry for folder at {$i}");
         $cachedFile = $cache->get($longPath . '/test.txt');
         $this->assertTrue(is_array($cachedFile), "No cache entry for file at {$i}");
         $this->assertEquals('test.txt', $cachedFile['name'], "Wrong cache entry for file at {$i}");
         $longPath .= '/' . $folderName;
     }
 }
 */
use OCA\Encryption;
\OCP\JSON::checkLoggedIn();
\OCP\JSON::checkAppEnabled('files_encryption');
\OCP\JSON::callCheck();
$l = OC_L10N::get('core');
$return = false;
$oldPassword = $_POST['oldPassword'];
$newPassword = $_POST['newPassword'];
$view = new \OC\Files\View('/');
$session = new \OCA\Encryption\Session($view);
$user = \OCP\User::getUser();
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$keyPath = '/' . $user . '/files_encryption/' . $user . '.private.key';
$encryptedKey = $view->file_get_contents($keyPath);
$decryptedKey = \OCA\Encryption\Crypt::decryptPrivateKey($encryptedKey, $oldPassword);
if ($decryptedKey) {
    $encryptedKey = \OCA\Encryption\Crypt::symmetricEncryptFileContent($decryptedKey, $newPassword);
    $view->file_put_contents($keyPath, $encryptedKey);
    $session->setPrivateKey($decryptedKey);
    $return = true;
}
\OC_FileProxy::$enabled = $proxyStatus;
// success or failure
if ($return) {
    $session->setInitialized(\OCA\Encryption\Session::INIT_SUCCESSFUL);
    \OCP\JSON::success(array('data' => array('message' => $l->t('Private key password successfully updated.'))));
} else {
    \OCP\JSON::error(array('data' => array('message' => $l->t('Could not update the private key password. Maybe the old password was not correct.'))));
}
Beispiel #13
0
 public function testFilePutContentsClearsChecksum()
 {
     $storage = new Temporary(array());
     $scanner = $storage->getScanner();
     $storage->file_put_contents('foo.txt', 'bar');
     \OC\Files\Filesystem::mount($storage, array(), '/test/');
     $scanner->scan('');
     $view = new \OC\Files\View('/test/foo.txt');
     $view->putFileInfo('.', ['checksum' => '42']);
     $this->assertEquals('bar', $view->file_get_contents(''));
     $fh = tmpfile();
     fwrite($fh, 'fooo');
     rewind($fh);
     $view->file_put_contents('', $fh);
     $this->assertEquals('fooo', $view->file_get_contents(''));
     $data = $view->getFileInfo('.');
     $this->assertEquals('', $data->getChecksum());
 }
 /**
  * @medium
  */
 public function testRenamePartFile()
 {
     // share to user
     $share = $this->share(\OCP\Share::SHARE_TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);
     self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
     $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
     $this->assertTrue($user2View->file_exists($this->folder));
     // create part file
     $result = $user2View->file_put_contents($this->folder . '/foo.txt.part', 'some test data');
     $this->assertTrue(is_int($result));
     // rename part file to real file
     $result = $user2View->rename($this->folder . '/foo.txt.part', $this->folder . '/foo.txt');
     $this->assertTrue($result);
     // check if the new file really exists
     $this->assertTrue($user2View->file_exists($this->folder . '/foo.txt'));
     // check if the rename also affected the owner
     self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
     $this->assertTrue($this->view->file_exists($this->folder . '/foo.txt'));
     //cleanup
     $this->shareManager->deleteShare($share);
 }
Beispiel #15
0
 /**
  * Test locks for rename or copy operation cross-storage
  *
  * @dataProvider lockFileRenameOrCopyCrossStorageDataProvider
  *
  * @param string $viewOperation operation to be done on the view
  * @param string $storageOperation operation to be mocked on the storage
  * @param int $expectedLockTypeSourceDuring expected lock type on source file during
  * the operation
  */
 public function testLockFileRenameCrossStorage($viewOperation, $storageOperation, $expectedLockTypeSourceDuring)
 {
     $view = new \OC\Files\View('/' . $this->user . '/files/');
     $storage = $this->getMockBuilder('\\OC\\Files\\Storage\\Temporary')->setMethods([$storageOperation])->getMock();
     $storage2 = $this->getMockBuilder('\\OC\\Files\\Storage\\Temporary')->setMethods([$storageOperation, 'filemtime'])->getMock();
     $storage2->expects($this->any())->method('filemtime')->will($this->returnValue(123456789));
     $sourcePath = 'original.txt';
     $targetPath = 'substorage/target.txt';
     \OC\Files\Filesystem::mount($storage, array(), $this->user . '/');
     \OC\Files\Filesystem::mount($storage2, array(), $this->user . '/files/substorage');
     $storage->mkdir('files');
     $view->file_put_contents($sourcePath, 'meh');
     $storage->expects($this->never())->method($storageOperation);
     $storage2->expects($this->once())->method($storageOperation)->will($this->returnCallback(function () use($view, $sourcePath, $targetPath, &$lockTypeSourceDuring, &$lockTypeTargetDuring) {
         $lockTypeSourceDuring = $this->getFileLockType($view, $sourcePath);
         $lockTypeTargetDuring = $this->getFileLockType($view, $targetPath);
         return true;
     }));
     $this->connectMockHooks($viewOperation, $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost);
     $this->connectMockHooks($viewOperation, $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost);
     $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation');
     $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked before operation');
     $view->{$viewOperation}($sourcePath, $targetPath);
     $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePre, 'Source file locked properly during pre-hook');
     $this->assertEquals($expectedLockTypeSourceDuring, $lockTypeSourceDuring, 'Source file locked properly during operation');
     $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePost, 'Source file locked properly during post-hook');
     $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPre, 'Target file locked properly during pre-hook');
     $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeTargetDuring, 'Target file locked properly during operation');
     $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPost, 'Target file locked properly during post-hook');
     $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation');
     $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked after operation');
 }
    header("Location: settings/personal.php");
    exit;
}
$fh = fopen($_FILES['rootcert_import']['tmp_name'], 'r');
$data = fread($fh, filesize($_FILES['rootcert_import']['tmp_name']));
fclose($fh);
$filename = $_FILES['rootcert_import']['name'];
$view = new \OC\Files\View('/' . \OCP\User::getUser() . '/files_external/uploads');
if (!$view->file_exists('')) {
    $view->mkdir('');
}
$isValid = openssl_pkey_get_public($data);
//maybe it was just the wrong file format, try to convert it...
if ($isValid == false) {
    $data = chunk_split(base64_encode($data), 64, "\n");
    $data = "-----BEGIN CERTIFICATE-----\n" . $data . "-----END CERTIFICATE-----\n";
    $isValid = openssl_pkey_get_public($data);
}
// add the certificate if it could be verified
if ($isValid) {
    // disable proxy to prevent multiple fopen calls
    $proxyStatus = \OC_FileProxy::$enabled;
    \OC_FileProxy::$enabled = false;
    $view->file_put_contents($filename, $data);
    OC_Mount_Config::createCertificateBundle();
    \OC_FileProxy::$enabled = $proxyStatus;
} else {
    OCP\Util::writeLog('files_external', 'Couldn\'t import SSL root certificate (' . $filename . '), allowed formats: PEM and DER', OCP\Util::WARN);
}
header('Location:' . OCP\Util::linkToRoute("settings_personal"));
exit;
Beispiel #17
0
 /**
  * @param string $filter
  * @param string[] $expected
  * @dataProvider mimeFilterProvider
  */
 public function testGetDirectoryContentMimeFilter($filter, $expected)
 {
     $storage1 = new Temporary();
     $root = $this->getUniqueID('/');
     \OC\Files\Filesystem::mount($storage1, array(), $root . '/');
     $view = new \OC\Files\View($root);
     $view->file_put_contents('test1.txt', 'asd');
     $view->file_put_contents('test2.txt', 'asd');
     $view->file_put_contents('test3.md', 'asd');
     $view->file_put_contents('test4.png', '');
     $content = $view->getDirectoryContent('', $filter);
     $files = array_map(function (FileInfo $info) {
         return $info->getName();
     }, $content);
     sort($files);
     $this->assertEquals($expected, $files);
 }
Beispiel #18
0
 /**
  * @large
  */
 function testEncryptLegacyFiles()
 {
     \Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
     $userView = new \OC\Files\View('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
     $view = new \OC\Files\View('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER . '/files');
     // Disable encryption proxy to prevent recursive calls
     $proxyStatus = \OC_FileProxy::$enabled;
     \OC_FileProxy::$enabled = false;
     $encryptionKeyContent = file_get_contents($this->legacyEncryptedDataKey);
     $userView->file_put_contents('/encryption.key', $encryptionKeyContent);
     $legacyEncryptedData = file_get_contents($this->legacyEncryptedData);
     $view->mkdir('/test/');
     $view->mkdir('/test/subtest/');
     $view->file_put_contents('/test/subtest/legacy-encrypted-text.txt', $legacyEncryptedData);
     $fileInfo = $view->getFileInfo('/test/subtest/legacy-encrypted-text.txt');
     $fileInfo['encrypted'] = true;
     $view->putFileInfo('/test/subtest/legacy-encrypted-text.txt', $fileInfo);
     \OC_FileProxy::$enabled = $proxyStatus;
     $params['uid'] = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER;
     $params['password'] = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER;
     $util = new Encryption\Util($this->view, \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
     $this->setMigrationStatus(0, \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
     $this->assertTrue(OCA\Encryption\Hooks::login($params));
     $this->assertEquals($this->legacyKey, \OC::$session->get('legacyKey'));
     $files = $util->findEncFiles('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER . '/files/');
     $this->assertTrue(is_array($files));
     $found = false;
     foreach ($files['encrypted'] as $encryptedFile) {
         if ($encryptedFile['name'] === 'legacy-encrypted-text.txt') {
             $found = true;
             break;
         }
     }
     $this->assertTrue($found);
 }
Beispiel #19
0
 public function testSharingAFileInsideAFolderThatIsAlreadyShared()
 {
     \OC_User::setUserId($this->user1);
     $view = new \OC\Files\View('/' . $this->user1 . '/');
     $view->mkdir('files/test');
     $view->mkdir('files/test/sub1');
     $view->file_put_contents('files/test/sub1/file.txt', 'abc');
     $folderInfo = $view->getFileInfo('files/test/sub1');
     $this->assertInstanceOf('\\OC\\Files\\FileInfo', $folderInfo);
     $folderId = $folderInfo->getId();
     $fileInfo = $view->getFileInfo('files/test/sub1/file.txt');
     $this->assertInstanceOf('\\OC\\Files\\FileInfo', $fileInfo);
     $fileId = $fileInfo->getId();
     $this->assertTrue(\OCP\Share::shareItem('folder', $folderId, \OCP\Share::SHARE_TYPE_GROUP, $this->group2, \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_UPDATE), 'Failed asserting that user 1 successfully shared "test/sub1" with group 2.');
     $this->assertTrue(\OCP\Share::shareItem('file', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ), 'Failed asserting that user 1 successfully shared "test/sub1/file.txt" with user 2.');
     $result = \OCP\Share::getItemsSharedWithUser('file', $this->user2);
     $this->assertCount(2, $result);
     foreach ($result as $share) {
         $itemName = substr($share['path'], strrpos($share['path'], '/'));
         $this->assertSame($itemName, $share['file_target'], 'Asserting that the file_target is the last segment of the path');
         $this->assertSame($share['item_target'], '/' . $share['item_source'], 'Asserting that the item is the item that was shared');
     }
 }
Beispiel #20
0
 /**
  * write private system key (recovery and public share key) to disk
  *
  * @param string $key encrypted key
  * @param string $keyName name of the key file
  * @return boolean
  */
 public static function setPrivateSystemKey($key, $keyName)
 {
     $header = Crypt::generateHeader();
     $view = new \OC\Files\View('/owncloud_private_key');
     $proxyStatus = \OC_FileProxy::$enabled;
     \OC_FileProxy::$enabled = false;
     if (!$view->file_exists('')) {
         $view->mkdir('');
     }
     $result = $view->file_put_contents($keyName, $header . $key);
     \OC_FileProxy::$enabled = $proxyStatus;
     return $result;
 }
Beispiel #21
0
 /**
  * @medium
  */
 function testFopenFile()
 {
     $filename = '/tmp-' . uniqid();
     $view = new \OC\Files\View('/' . $this->userId . '/files');
     // Save short data as encrypted file using stream wrapper
     $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
     // Test that data was successfully written
     $this->assertTrue(is_int($cryptedFile));
     $handle = $view->fopen($filename, 'r');
     // Get file decrypted contents
     $decrypt = fgets($handle);
     $this->assertEquals($this->dataShort, $decrypt);
     // tear down
     $view->unlink($filename);
 }
 * @brief Script to change recovery key password
 *
 */
use OCA\Encryption;
\OCP\JSON::checkAdminUser();
\OCP\JSON::checkAppEnabled('files_encryption');
\OCP\JSON::callCheck();
$l = OC_L10N::get('core');
$return = false;
$oldPassword = $_POST['oldPassword'];
$newPassword = $_POST['newPassword'];
$view = new \OC\Files\View('/');
$util = new \OCA\Encryption\Util(new \OC_FilesystemView('/'), \OCP\User::getUser());
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$keyId = $util->getRecoveryKeyId();
$keyPath = '/owncloud_private_key/' . $keyId . '.private.key';
$encryptedRecoveryKey = $view->file_get_contents($keyPath);
$decryptedRecoveryKey = \OCA\Encryption\Crypt::decryptPrivateKey($encryptedRecoveryKey, $oldPassword);
if ($decryptedRecoveryKey) {
    $encryptedRecoveryKey = \OCA\Encryption\Crypt::symmetricEncryptFileContent($decryptedRecoveryKey, $newPassword);
    $view->file_put_contents($keyPath, $encryptedRecoveryKey);
    $return = true;
}
\OC_FileProxy::$enabled = $proxyStatus;
// success or failure
if ($return) {
    \OCP\JSON::success(array('data' => array('message' => $l->t('Password successfully changed.'))));
} else {
    \OCP\JSON::error(array('data' => array('message' => $l->t('Could not change the password. Maybe the old password was not correct.'))));
}
Beispiel #23
0
 public function xtestLongPath()
 {
     $storage = new \OC\Files\Storage\Temporary(array());
     \OC\Files\Filesystem::mount($storage, array(), '/');
     $rootView = new \OC\Files\View('');
     $longPath = '';
     $ds = DIRECTORY_SEPARATOR;
     /*
      * 4096 is the maximum path length in file_cache.path in *nix
      * 1024 is the max path length in mac
      * 228 is the max path length in windows
      */
     $folderName = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789';
     $tmpdirLength = strlen(\OC_Helper::tmpFolder());
     if (\OC_Util::runningOnWindows()) {
         $this->markTestSkipped('[Windows] ');
         $depth = (260 - $tmpdirLength) / 57;
     } elseif (\OC_Util::runningOnMac()) {
         $depth = (1024 - $tmpdirLength) / 57;
     } else {
         $depth = (4000 - $tmpdirLength) / 57;
     }
     foreach (range(0, $depth - 1) as $i) {
         $longPath .= $ds . $folderName;
         $result = $rootView->mkdir($longPath);
         $this->assertTrue($result, "mkdir failed on {$i} - path length: " . strlen($longPath));
         $result = $rootView->file_put_contents($longPath . "{$ds}test.txt", 'lorem');
         $this->assertEquals(5, $result, "file_put_contents failed on {$i}");
         $this->assertTrue($rootView->file_exists($longPath));
         $this->assertTrue($rootView->file_exists($longPath . "{$ds}test.txt"));
     }
     $cache = $storage->getCache();
     $scanner = $storage->getScanner();
     $scanner->scan('');
     $longPath = $folderName;
     foreach (range(0, $depth - 1) as $i) {
         $cachedFolder = $cache->get($longPath);
         $this->assertTrue(is_array($cachedFolder), "No cache entry for folder at {$i}");
         $this->assertEquals($folderName, $cachedFolder['name'], "Wrong cache entry for folder at {$i}");
         $cachedFile = $cache->get($longPath . '/test.txt');
         $this->assertTrue(is_array($cachedFile), "No cache entry for file at {$i}");
         $this->assertEquals('test.txt', $cachedFile['name'], "Wrong cache entry for file at {$i}");
         $longPath .= $ds . $folderName;
     }
 }
Beispiel #24
0
 public function testFileView()
 {
     $storage = new Temporary(array());
     $scanner = $storage->getScanner();
     $storage->file_put_contents('foo.txt', 'bar');
     \OC\Files\Filesystem::mount($storage, array(), '/test/');
     $scanner->scan('');
     $view = new \OC\Files\View('/test/foo.txt');
     $this->assertEquals('bar', $view->file_get_contents(''));
     $fh = tmpfile();
     fwrite($fh, 'foo');
     rewind($fh);
     $view->file_put_contents('', $fh);
     $this->assertEquals('foo', $view->file_get_contents(''));
 }