DecryptAll.php 7.7 KB

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