Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DecryptAll.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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\IUserManager;
  34. use Symfony\Component\Console\Helper\ProgressBar;
  35. use Symfony\Component\Console\Input\InputInterface;
  36. use Symfony\Component\Console\Output\OutputInterface;
  37. class DecryptAll {
  38. /** @var OutputInterface */
  39. protected $output;
  40. /** @var InputInterface */
  41. protected $input;
  42. /** @var Manager */
  43. protected $encryptionManager;
  44. /** @var IUserManager */
  45. protected $userManager;
  46. /** @var View */
  47. protected $rootView;
  48. /** @var array files which couldn't be decrypted */
  49. protected $failed;
  50. /**
  51. * @param Manager $encryptionManager
  52. * @param IUserManager $userManager
  53. * @param View $rootView
  54. */
  55. public function __construct(
  56. Manager $encryptionManager,
  57. IUserManager $userManager,
  58. View $rootView
  59. ) {
  60. $this->encryptionManager = $encryptionManager;
  61. $this->userManager = $userManager;
  62. $this->rootView = $rootView;
  63. $this->failed = [];
  64. }
  65. /**
  66. * start to decrypt all files
  67. *
  68. * @param InputInterface $input
  69. * @param OutputInterface $output
  70. * @param string $user which users data folder should be decrypted, default = all users
  71. * @return bool
  72. * @throws \Exception
  73. */
  74. public function decryptAll(InputInterface $input, OutputInterface $output, $user = '') {
  75. $this->input = $input;
  76. $this->output = $output;
  77. if ($user !== '' && $this->userManager->userExists($user) === false) {
  78. $this->output->writeln('User "' . $user . '" does not exist. Please check the username and try again');
  79. return false;
  80. }
  81. $this->output->writeln('prepare encryption modules...');
  82. if ($this->prepareEncryptionModules($user) === false) {
  83. return false;
  84. }
  85. $this->output->writeln(' done.');
  86. $this->decryptAllUsersFiles($user);
  87. if (empty($this->failed)) {
  88. $this->output->writeln('all files could be decrypted successfully!');
  89. } else {
  90. $this->output->writeln('Files for following users couldn\'t be decrypted, ');
  91. $this->output->writeln('maybe the user is not set up in a way that supports this operation: ');
  92. foreach ($this->failed as $uid => $paths) {
  93. $this->output->writeln(' ' . $uid);
  94. foreach ($paths as $path) {
  95. $this->output->writeln(' ' . $path);
  96. }
  97. }
  98. $this->output->writeln('');
  99. }
  100. return true;
  101. }
  102. /**
  103. * prepare encryption modules to perform the decrypt all function
  104. *
  105. * @param $user
  106. * @return bool
  107. */
  108. protected function prepareEncryptionModules($user) {
  109. // prepare all encryption modules for decrypt all
  110. $encryptionModules = $this->encryptionManager->getEncryptionModules();
  111. foreach ($encryptionModules as $moduleDesc) {
  112. /** @var IEncryptionModule $module */
  113. $module = call_user_func($moduleDesc['callback']);
  114. $this->output->writeln('');
  115. $this->output->writeln('Prepare "' . $module->getDisplayName() . '"');
  116. $this->output->writeln('');
  117. if ($module->prepareDecryptAll($this->input, $this->output, $user) === false) {
  118. $this->output->writeln('Module "' . $moduleDesc['displayName'] . '" does not support the functionality to decrypt all files again or the initialization of the module failed!');
  119. return false;
  120. }
  121. }
  122. return true;
  123. }
  124. /**
  125. * iterate over all user and encrypt their files
  126. *
  127. * @param string $user which users files should be decrypted, default = all users
  128. */
  129. protected function decryptAllUsersFiles($user = '') {
  130. $this->output->writeln("\n");
  131. $userList = [];
  132. if ($user === '') {
  133. $fetchUsersProgress = new ProgressBar($this->output);
  134. $fetchUsersProgress->setFormat(" %message% \n [%bar%]");
  135. $fetchUsersProgress->start();
  136. $fetchUsersProgress->setMessage("Fetch list of users...");
  137. $fetchUsersProgress->advance();
  138. foreach ($this->userManager->getBackends() as $backend) {
  139. $limit = 500;
  140. $offset = 0;
  141. do {
  142. $users = $backend->getUsers('', $limit, $offset);
  143. foreach ($users as $user) {
  144. $userList[] = $user;
  145. }
  146. $offset += $limit;
  147. $fetchUsersProgress->advance();
  148. } while (count($users) >= $limit);
  149. $fetchUsersProgress->setMessage("Fetch list of users... finished");
  150. $fetchUsersProgress->finish();
  151. }
  152. } else {
  153. $userList[] = $user;
  154. }
  155. $this->output->writeln("\n\n");
  156. $progress = new ProgressBar($this->output);
  157. $progress->setFormat(" %message% \n [%bar%]");
  158. $progress->start();
  159. $progress->setMessage("starting to decrypt files...");
  160. $progress->advance();
  161. $numberOfUsers = count($userList);
  162. $userNo = 1;
  163. foreach ($userList as $uid) {
  164. $userCount = "$uid ($userNo of $numberOfUsers)";
  165. $this->decryptUsersFiles($uid, $progress, $userCount);
  166. $userNo++;
  167. }
  168. $progress->setMessage("starting to decrypt files... finished");
  169. $progress->finish();
  170. $this->output->writeln("\n\n");
  171. }
  172. /**
  173. * encrypt files from the given user
  174. *
  175. * @param string $uid
  176. * @param ProgressBar $progress
  177. * @param string $userCount
  178. */
  179. protected function decryptUsersFiles($uid, ProgressBar $progress, $userCount) {
  180. $this->setupUserFS($uid);
  181. $directories = [];
  182. $directories[] = '/' . $uid . '/files';
  183. while ($root = array_pop($directories)) {
  184. $content = $this->rootView->getDirectoryContent($root);
  185. foreach ($content as $file) {
  186. // only decrypt files owned by the user
  187. if ($file->getStorage()->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) {
  188. continue;
  189. }
  190. $path = $root . '/' . $file['name'];
  191. if ($this->rootView->is_dir($path)) {
  192. $directories[] = $path;
  193. continue;
  194. } else {
  195. try {
  196. $progress->setMessage("decrypt files for user $userCount: $path");
  197. $progress->advance();
  198. if ($file->isEncrypted() === false) {
  199. $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
  200. $progress->advance();
  201. } else {
  202. if ($this->decryptFile($path) === false) {
  203. $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
  204. $progress->advance();
  205. }
  206. }
  207. } catch (\Exception $e) {
  208. if (isset($this->failed[$uid])) {
  209. $this->failed[$uid][] = $path;
  210. } else {
  211. $this->failed[$uid] = [$path];
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. /**
  219. * encrypt file
  220. *
  221. * @param string $path
  222. * @return bool
  223. */
  224. protected function decryptFile($path) {
  225. // skip already decrypted files
  226. $fileInfo = $this->rootView->getFileInfo($path);
  227. if ($fileInfo !== false && !$fileInfo->isEncrypted()) {
  228. return true;
  229. }
  230. $source = $path;
  231. $target = $path . '.decrypted.' . $this->getTimestamp();
  232. try {
  233. $this->rootView->copy($source, $target);
  234. $this->rootView->touch($target, $fileInfo->getMTime());
  235. $this->rootView->rename($target, $source);
  236. } catch (DecryptionFailedException $e) {
  237. if ($this->rootView->file_exists($target)) {
  238. $this->rootView->unlink($target);
  239. }
  240. return false;
  241. }
  242. return true;
  243. }
  244. /**
  245. * get current timestamp
  246. *
  247. * @return int
  248. */
  249. protected function getTimestamp() {
  250. return time();
  251. }
  252. /**
  253. * setup user file system
  254. *
  255. * @param string $uid
  256. */
  257. protected function setupUserFS($uid) {
  258. \OC_Util::tearDownFS();
  259. \OC_Util::setupFS($uid);
  260. }
  261. }