123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace OC\Encryption;
- use OC\Encryption\Exceptions\DecryptionFailedException;
- use OC\Files\View;
- use OCP\Encryption\IEncryptionModule;
- use OCP\IUserManager;
- use Symfony\Component\Console\Helper\ProgressBar;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- class DecryptAll {
-
- protected $output;
-
- protected $input;
-
- protected $encryptionManager;
-
- protected $userManager;
-
- protected $rootView;
-
- protected $failed;
-
- public function __construct(
- Manager $encryptionManager,
- IUserManager $userManager,
- View $rootView
- ) {
- $this->encryptionManager = $encryptionManager;
- $this->userManager = $userManager;
- $this->rootView = $rootView;
- $this->failed = [];
- }
-
- public function decryptAll(InputInterface $input, OutputInterface $output, $user = '') {
- $this->input = $input;
- $this->output = $output;
- if ($user !== '' && $this->userManager->userExists($user) === false) {
- $this->output->writeln('User "' . $user . '" does not exist. Please check the username and try again');
- return false;
- }
- $this->output->writeln('prepare encryption modules...');
- if ($this->prepareEncryptionModules($user) === false) {
- return false;
- }
- $this->output->writeln(' done.');
- $this->decryptAllUsersFiles($user);
- if (empty($this->failed)) {
- $this->output->writeln('all files could be decrypted successfully!');
- } else {
- $this->output->writeln('Files for following users couldn\'t be decrypted, ');
- $this->output->writeln('maybe the user is not set up in a way that supports this operation: ');
- foreach ($this->failed as $uid => $paths) {
- $this->output->writeln(' ' . $uid);
- foreach ($paths as $path) {
- $this->output->writeln(' ' . $path);
- }
- }
- $this->output->writeln('');
- }
- return true;
- }
-
- protected function prepareEncryptionModules($user) {
-
- $encryptionModules = $this->encryptionManager->getEncryptionModules();
- foreach ($encryptionModules as $moduleDesc) {
-
- $module = call_user_func($moduleDesc['callback']);
- $this->output->writeln('');
- $this->output->writeln('Prepare "' . $module->getDisplayName() . '"');
- $this->output->writeln('');
- if ($module->prepareDecryptAll($this->input, $this->output, $user) === false) {
- $this->output->writeln('Module "' . $moduleDesc['displayName'] . '" does not support the functionality to decrypt all files again or the initialization of the module failed!');
- return false;
- }
- }
- return true;
- }
-
- protected function decryptAllUsersFiles($user = '') {
- $this->output->writeln("\n");
- $userList = [];
- if ($user === '') {
- $fetchUsersProgress = new ProgressBar($this->output);
- $fetchUsersProgress->setFormat(" %message% \n [%bar%]");
- $fetchUsersProgress->start();
- $fetchUsersProgress->setMessage("Fetch list of users...");
- $fetchUsersProgress->advance();
- foreach ($this->userManager->getBackends() as $backend) {
- $limit = 500;
- $offset = 0;
- do {
- $users = $backend->getUsers('', $limit, $offset);
- foreach ($users as $user) {
- $userList[] = $user;
- }
- $offset += $limit;
- $fetchUsersProgress->advance();
- } while (count($users) >= $limit);
- $fetchUsersProgress->setMessage("Fetch list of users... finished");
- $fetchUsersProgress->finish();
- }
- } else {
- $userList[] = $user;
- }
- $this->output->writeln("\n\n");
- $progress = new ProgressBar($this->output);
- $progress->setFormat(" %message% \n [%bar%]");
- $progress->start();
- $progress->setMessage("starting to decrypt files...");
- $progress->advance();
- $numberOfUsers = count($userList);
- $userNo = 1;
- foreach ($userList as $uid) {
- $userCount = "$uid ($userNo of $numberOfUsers)";
- $this->decryptUsersFiles($uid, $progress, $userCount);
- $userNo++;
- }
- $progress->setMessage("starting to decrypt files... finished");
- $progress->finish();
- $this->output->writeln("\n\n");
- }
-
- protected function decryptUsersFiles($uid, ProgressBar $progress, $userCount) {
- $this->setupUserFS($uid);
- $directories = [];
- $directories[] = '/' . $uid . '/files';
- while ($root = array_pop($directories)) {
- $content = $this->rootView->getDirectoryContent($root);
- foreach ($content as $file) {
-
- if ($file->getStorage()->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) {
- continue;
- }
- $path = $root . '/' . $file['name'];
- if ($this->rootView->is_dir($path)) {
- $directories[] = $path;
- continue;
- } else {
- try {
- $progress->setMessage("decrypt files for user $userCount: $path");
- $progress->advance();
- if ($file->isEncrypted() === false) {
- $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
- $progress->advance();
- } else {
- if ($this->decryptFile($path) === false) {
- $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
- $progress->advance();
- }
- }
- } catch (\Exception $e) {
- if (isset($this->failed[$uid])) {
- $this->failed[$uid][] = $path;
- } else {
- $this->failed[$uid] = [$path];
- }
- }
- }
- }
- }
- }
-
- protected function decryptFile($path) {
-
- $fileInfo = $this->rootView->getFileInfo($path);
- if ($fileInfo !== false && !$fileInfo->isEncrypted()) {
- return true;
- }
- $source = $path;
- $target = $path . '.decrypted.' . $this->getTimestamp();
- try {
- $this->rootView->copy($source, $target);
- $this->rootView->touch($target, $fileInfo->getMTime());
- $this->rootView->rename($target, $source);
- } catch (DecryptionFailedException $e) {
- if ($this->rootView->file_exists($target)) {
- $this->rootView->unlink($target);
- }
- return false;
- }
- return true;
- }
-
- protected function getTimestamp() {
- return time();
- }
-
- protected function setupUserFS($uid) {
- \OC_Util::tearDownFS();
- \OC_Util::setupFS($uid);
- }
- }
|