/** * copy keys to new location * * @param string $source path relative to data/ * @param string $target path relative to data/ * @return bool */ protected function copyKeys($source, $target) { if (!$this->util->isExcluded($source)) { return $this->keyStorage->copyKeys($source, $target); } return false; }
/** * @dataProvider provideWrapStorage */ public function testWrapStorage($expectedWrapped, $wrappedStorages) { $storage = $this->getMockBuilder('OC\Files\Storage\Storage') ->disableOriginalConstructor() ->getMock(); foreach ($wrappedStorages as $wrapper) { $storage->expects($this->any()) ->method('instanceOfStorage') ->willReturnMap([ [$wrapper, true], ]); } $mount = $this->getMockBuilder('OCP\Files\Mount\IMountPoint') ->disableOriginalConstructor() ->getMock(); $returnedStorage = $this->util->wrapStorage('mountPoint', $storage, $mount); $this->assertEquals( $expectedWrapped, $returnedStorage->instanceOfStorage('OC\Files\Storage\Wrapper\Encryption'), 'Asserted that the storage is (not) wrapped with encryption' ); }
/** * get system wide path and detect mount points * * @param string $path * @return string */ protected function getPathToKeys($path) { list($owner, $relativePath) = $this->util->getUidAndFilename($path); $systemWideMountPoint = $this->util->isSystemWideMountPoint($relativePath, $owner); if ($systemWideMountPoint) { $systemPath = $this->keys_base_dir . $relativePath . '/'; } else { $systemPath = '/' . $owner . $this->keys_base_dir . $relativePath . '/'; } return $systemPath; }
protected function execute(InputInterface $input, OutputInterface $output) { $oldRoot = $this->util->getKeyStorageRoot(); $newRoot = $input->getArgument('newRoot'); if ($newRoot === null) { $question = new ConfirmationQuestion('No storage root given, do you want to reset the key storage root to the default location? (y/n) ', false); if (!$this->questionHelper->ask($input, $output, $question)) { return; } $newRoot = ''; } $oldRootDescription = $oldRoot !== '' ? $oldRoot : 'default storage location'; $newRootDescription = $newRoot !== '' ? $newRoot : 'default storage location'; $output->writeln("Change key storage root from <info>{$oldRootDescription}</info> to <info>{$newRootDescription}</info>"); $success = $this->moveAllKeys($oldRoot, $newRoot, $output); if ($success) { $this->util->setKeyStorageRoot($newRoot); $output->writeln(''); $output->writeln("Key storage root successfully changed to <info>{$newRootDescription}</info>"); } }
/** * get list of users with access to the file * * @param string $path to the file * @return array ['users' => $uniqueUserIds, 'public' => $public] */ public function getAccessList($path) { // Make sure that a share key is generated for the owner too list($owner, $ownerPath) = $this->util->getUidAndFilename($path); // always add owner to the list of users with access to the file $userIds = array($owner); if (!$this->util->isFile($owner . '/' . $ownerPath)) { return array('users' => $userIds, 'public' => false); } $ownerPath = substr($ownerPath, strlen('/files')); $ownerPath = $this->util->stripPartialFileExtension($ownerPath); // first get the shares for the parent and cache the result so that we don't // need to check all parents for every file $parent = dirname($ownerPath); if (isset($this->cache[$parent])) { $resultForParents = $this->cache[$parent]; } else { $resultForParents = \OCP\Share::getUsersSharingFile($parent, $owner); $this->cache[$parent] = $resultForParents; } $userIds = \array_merge($userIds, $resultForParents['users']); $public = $resultForParents['public'] || $resultForParents['remote']; // Find out who, if anyone, is sharing the file $resultForFile = \OCP\Share::getUsersSharingFile($ownerPath, $owner, false, false, false); $userIds = \array_merge($userIds, $resultForFile['users']); $public = $resultForFile['public'] || $resultForFile['remote'] || $public; // check if it is a group mount if (\OCP\App::isEnabled("files_external")) { $mounts = \OC_Mount_Config::getSystemMountPoints(); foreach ($mounts as $mount) { if ($mount['mountpoint'] == substr($ownerPath, 1, strlen($mount['mountpoint']))) { $mountedFor = $this->util->getUserWithAccessToMountPoint($mount['applicable']['users'], $mount['applicable']['groups']); $userIds = array_merge($userIds, $mountedFor); } } } // Remove duplicate UIDs $uniqueUserIds = array_unique($userIds); return array('users' => $uniqueUserIds, 'public' => $public); }
/** * check if key storage is ready * * @return bool */ protected function isKeyStorageReady() { $rootDir = $this->util->getKeyStorageRoot(); // the default root is always valid if ($rootDir === '') { return true; } // check if key storage is mounted correctly if ($this->rootView->file_exists($rootDir . '/' . Storage::KEY_STORAGE_MARKER)) { return true; } return false; }
/** * notify encryption module about added/removed users from a file/folder * * @param string $path relative to data/ * @throws Exceptions\ModuleDoesNotExistsException */ public function update($path) { // if a folder was shared, get a list of all (sub-)folders if ($this->view->is_dir($path)) { $allFiles = $this->util->getAllFiles($path); } else { $allFiles = array($path); } $encryptionModule = $this->encryptionManager->getEncryptionModule(); foreach ($allFiles as $file) { $usersSharing = $this->file->getAccessList($file); $encryptionModule->update($file, $this->uid, $usersSharing); } }
/** * read encryption module needed to read/write the file located at $path * * @param string $path * @return null|\OCP\Encryption\IEncryptionModule * @throws ModuleDoesNotExistsException * @throws \Exception */ protected function getEncryptionModule($path) { $encryptionModule = null; $header = $this->getHeader($path); $encryptionModuleId = $this->util->getEncryptionModuleId($header); if (!empty($encryptionModuleId)) { try { $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); } catch (ModuleDoesNotExistsException $e) { $this->logger->critical('Encryption module defined in "' . $path . '" not loaded!'); throw $e; } } return $encryptionModule; }
/** * copy keys if a file was renamed * * @param string $source * @param string $target * @param string $owner * @param bool $systemWide */ public function copyKeys($source, $target) { list($owner, $source) = $this->util->getUidAndFilename($source); list(, $target) = $this->util->getUidAndFilename($target); $systemWide = $this->util->isSystemWideMountPoint($target); if ($systemWide) { $sourcePath = $this->keys_base_dir . $source . '/'; $targetPath = $this->keys_base_dir . $target . '/'; } else { $sourcePath = '/' . $owner . $this->keys_base_dir . $source . '/'; $targetPath = '/' . $owner . $this->keys_base_dir . $target . '/'; } if ($this->view->file_exists($sourcePath)) { $this->keySetPreparation(dirname($targetPath)); $this->view->copy($sourcePath, $targetPath); } }
/** * update keyfiles and share keys recursively * * @param int $fileSource file source id */ private function update($fileSource) { $path = \OC\Files\Filesystem::getPath($fileSource); $info = \OC\Files\Filesystem::getFileInfo($path); $owner = \OC\Files\Filesystem::getOwner($path); $view = new \OC\Files\View('/' . $owner . '/files'); $ownerPath = $view->getPath($info->getId()); $absPath = '/' . $owner . '/files' . $ownerPath; $mount = $this->mountManager->find($path); $mountPoint = $mount->getMountPoint(); // if a folder was shared, get a list of all (sub-)folders if ($this->view->is_dir($absPath)) { $allFiles = $this->util->getAllFiles($absPath, $mountPoint); } else { $allFiles = array($absPath); } $encryptionModule = $this->encryptionManager->getDefaultEncryptionModule(); foreach ($allFiles as $path) { $usersSharing = $this->file->getAccessList($path); $encryptionModule->update($path, $this->uid, $usersSharing); } }
/** * write header at beginning of encrypted file * * @return integer * @throws EncryptionHeaderKeyExistsException if header key is already in use */ protected function writeHeader() { $header = $this->util->createHeader($this->newHeader, $this->encryptionModule); return parent::stream_write($header); }
/** * @dataProvider providePathsForTestIsExcluded */ public function testIsEcluded($path, $expected) { $this->userManager->expects($this->any())->method('userExists')->will($this->returnCallback(array($this, 'isExcludedCallback'))); $u = new Util($this->view, $this->userManager, $this->config); $this->assertSame($expected, $u->isExcluded($path)); }
/** * @dataProvider dataTestStripPartialFileExtension * * @param string $path * @param string $expected */ public function testStripPartialFileExtension($path, $expected) { $this->assertSame($expected, $this->util->stripPartialFileExtension($path)); }
protected function execute(InputInterface $input, OutputInterface $output) { $currentRoot = $this->util->getKeyStorageRoot(); $rootDescription = $currentRoot !== '' ? $currentRoot : 'default storage location (data/)'; $output->writeln("Current key storage root: <info>{$rootDescription}</info>"); }