DecryptAll.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christian Jürges <christian@eqipe.ch>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author sammo2828 <sammo2828@gmail.com>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Encryption;
  30. use OC\Encryption\Exceptions\DecryptionFailedException;
  31. use OC\Files\View;
  32. use OCP\Encryption\IEncryptionModule;
  33. use OCP\Encryption\IManager;
  34. use OCP\IUserManager;
  35. use Symfony\Component\Console\Helper\ProgressBar;
  36. use Symfony\Component\Console\Input\InputInterface;
  37. use Symfony\Component\Console\Output\OutputInterface;
  38. class DecryptAll {
  39. /** @var OutputInterface */
  40. protected $output;
  41. /** @var InputInterface */
  42. protected $input;
  43. /** @var array files which couldn't be decrypted */
  44. protected $failed;
  45. public function __construct(
  46. protected IManager $encryptionManager,
  47. protected IUserManager $userManager,
  48. protected View $rootView
  49. ) {
  50. $this->failed = [];
  51. }
  52. /**
  53. * start to decrypt all files
  54. *
  55. * @param InputInterface $input
  56. * @param OutputInterface $output
  57. * @param string $user which users data folder should be decrypted, default = all users
  58. * @return bool
  59. * @throws \Exception
  60. */
  61. public function decryptAll(InputInterface $input, OutputInterface $output, $user = '') {
  62. $this->input = $input;
  63. $this->output = $output;
  64. if ($user !== '' && $this->userManager->userExists($user) === false) {
  65. $this->output->writeln('User "' . $user . '" does not exist. Please check the username and try again');
  66. return false;
  67. }
  68. $this->output->writeln('prepare encryption modules...');
  69. if ($this->prepareEncryptionModules($user) === false) {
  70. return false;
  71. }
  72. $this->output->writeln(' done.');
  73. $this->decryptAllUsersFiles($user);
  74. if (empty($this->failed)) {
  75. $this->output->writeln('all files could be decrypted successfully!');
  76. } else {
  77. $this->output->writeln('Files for following users couldn\'t be decrypted, ');
  78. $this->output->writeln('maybe the user is not set up in a way that supports this operation: ');
  79. foreach ($this->failed as $uid => $paths) {
  80. $this->output->writeln(' ' . $uid);
  81. foreach ($paths as $path) {
  82. $this->output->writeln(' ' . $path);
  83. }
  84. }
  85. $this->output->writeln('');
  86. }
  87. return true;
  88. }
  89. /**
  90. * prepare encryption modules to perform the decrypt all function
  91. *
  92. * @param $user
  93. * @return bool
  94. */
  95. protected function prepareEncryptionModules($user) {
  96. // prepare all encryption modules for decrypt all
  97. $encryptionModules = $this->encryptionManager->getEncryptionModules();
  98. foreach ($encryptionModules as $moduleDesc) {
  99. /** @var IEncryptionModule $module */
  100. $module = call_user_func($moduleDesc['callback']);
  101. $this->output->writeln('');
  102. $this->output->writeln('Prepare "' . $module->getDisplayName() . '"');
  103. $this->output->writeln('');
  104. if ($module->prepareDecryptAll($this->input, $this->output, $user) === false) {
  105. $this->output->writeln('Module "' . $moduleDesc['displayName'] . '" does not support the functionality to decrypt all files again or the initialization of the module failed!');
  106. return false;
  107. }
  108. }
  109. return true;
  110. }
  111. /**
  112. * iterate over all user and encrypt their files
  113. *
  114. * @param string $user which users files should be decrypted, default = all users
  115. */
  116. protected function decryptAllUsersFiles($user = '') {
  117. $this->output->writeln("\n");
  118. $userList = [];
  119. if ($user === '') {
  120. $fetchUsersProgress = new ProgressBar($this->output);
  121. $fetchUsersProgress->setFormat(" %message% \n [%bar%]");
  122. $fetchUsersProgress->start();
  123. $fetchUsersProgress->setMessage("Fetch list of users...");
  124. $fetchUsersProgress->advance();
  125. foreach ($this->userManager->getBackends() as $backend) {
  126. $limit = 500;
  127. $offset = 0;
  128. do {
  129. $users = $backend->getUsers('', $limit, $offset);
  130. foreach ($users as $user) {
  131. $userList[] = $user;
  132. }
  133. $offset += $limit;
  134. $fetchUsersProgress->advance();
  135. } while (count($users) >= $limit);
  136. $fetchUsersProgress->setMessage("Fetch list of users... finished");
  137. $fetchUsersProgress->finish();
  138. }
  139. } else {
  140. $userList[] = $user;
  141. }
  142. $this->output->writeln("\n\n");
  143. $progress = new ProgressBar($this->output);
  144. $progress->setFormat(" %message% \n [%bar%]");
  145. $progress->start();
  146. $progress->setMessage("starting to decrypt files...");
  147. $progress->advance();
  148. $numberOfUsers = count($userList);
  149. $userNo = 1;
  150. foreach ($userList as $uid) {
  151. $userCount = "$uid ($userNo of $numberOfUsers)";
  152. $this->decryptUsersFiles($uid, $progress, $userCount);
  153. $userNo++;
  154. }
  155. $progress->setMessage("starting to decrypt files... finished");
  156. $progress->finish();
  157. $this->output->writeln("\n\n");
  158. }
  159. /**
  160. * encrypt files from the given user
  161. *
  162. * @param string $uid
  163. * @param ProgressBar $progress
  164. * @param string $userCount
  165. */
  166. protected function decryptUsersFiles($uid, ProgressBar $progress, $userCount) {
  167. $this->setupUserFS($uid);
  168. $directories = [];
  169. $directories[] = '/' . $uid . '/files';
  170. while ($root = array_pop($directories)) {
  171. $content = $this->rootView->getDirectoryContent($root);
  172. foreach ($content as $file) {
  173. // only decrypt files owned by the user
  174. if ($file->getStorage()->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) {
  175. continue;
  176. }
  177. $path = $root . '/' . $file['name'];
  178. if ($this->rootView->is_dir($path)) {
  179. $directories[] = $path;
  180. continue;
  181. } else {
  182. try {
  183. $progress->setMessage("decrypt files for user $userCount: $path");
  184. $progress->advance();
  185. if ($file->isEncrypted() === false) {
  186. $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
  187. $progress->advance();
  188. } else {
  189. if ($this->decryptFile($path) === false) {
  190. $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
  191. $progress->advance();
  192. }
  193. }
  194. } catch (\Exception $e) {
  195. if (isset($this->failed[$uid])) {
  196. $this->failed[$uid][] = $path;
  197. } else {
  198. $this->failed[$uid] = [$path];
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. /**
  206. * encrypt file
  207. *
  208. * @param string $path
  209. * @return bool
  210. */
  211. protected function decryptFile($path) {
  212. // skip already decrypted files
  213. $fileInfo = $this->rootView->getFileInfo($path);
  214. if ($fileInfo !== false && !$fileInfo->isEncrypted()) {
  215. return true;
  216. }
  217. $source = $path;
  218. $target = $path . '.decrypted.' . $this->getTimestamp();
  219. try {
  220. $this->rootView->copy($source, $target);
  221. $this->rootView->touch($target, $fileInfo->getMTime());
  222. $this->rootView->rename($target, $source);
  223. } catch (DecryptionFailedException $e) {
  224. if ($this->rootView->file_exists($target)) {
  225. $this->rootView->unlink($target);
  226. }
  227. return false;
  228. }
  229. return true;
  230. }
  231. /**
  232. * get current timestamp
  233. *
  234. * @return int
  235. */
  236. protected function getTimestamp() {
  237. return time();
  238. }
  239. /**
  240. * setup user file system
  241. *
  242. * @param string $uid
  243. */
  244. protected function setupUserFS($uid) {
  245. \OC_Util::tearDownFS();
  246. \OC_Util::setupFS($uid);
  247. }
  248. }