DecryptAll.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. foreach ($paths as $path) {
  92. $this->output->writeln(' ' . $path);
  93. }
  94. }
  95. $this->output->writeln('');
  96. }
  97. return true;
  98. }
  99. /**
  100. * prepare encryption modules to perform the decrypt all function
  101. *
  102. * @param $user
  103. * @return bool
  104. */
  105. protected function prepareEncryptionModules($user) {
  106. // prepare all encryption modules for decrypt all
  107. $encryptionModules = $this->encryptionManager->getEncryptionModules();
  108. foreach ($encryptionModules as $moduleDesc) {
  109. /** @var IEncryptionModule $module */
  110. $module = call_user_func($moduleDesc['callback']);
  111. $this->output->writeln('');
  112. $this->output->writeln('Prepare "' . $module->getDisplayName() . '"');
  113. $this->output->writeln('');
  114. if ($module->prepareDecryptAll($this->input, $this->output, $user) === false) {
  115. $this->output->writeln('Module "' . $moduleDesc['displayName'] . '" does not support the functionality to decrypt all files again or the initialization of the module failed!');
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. /**
  122. * iterate over all user and encrypt their files
  123. *
  124. * @param string $user which users files should be decrypted, default = all users
  125. */
  126. protected function decryptAllUsersFiles($user = '') {
  127. $this->output->writeln("\n");
  128. $userList = [];
  129. if ($user === '') {
  130. $fetchUsersProgress = new ProgressBar($this->output);
  131. $fetchUsersProgress->setFormat(" %message% \n [%bar%]");
  132. $fetchUsersProgress->start();
  133. $fetchUsersProgress->setMessage("Fetch list of users...");
  134. $fetchUsersProgress->advance();
  135. foreach ($this->userManager->getBackends() as $backend) {
  136. $limit = 500;
  137. $offset = 0;
  138. do {
  139. $users = $backend->getUsers('', $limit, $offset);
  140. foreach ($users as $user) {
  141. $userList[] = $user;
  142. }
  143. $offset += $limit;
  144. $fetchUsersProgress->advance();
  145. } while (count($users) >= $limit);
  146. $fetchUsersProgress->setMessage("Fetch list of users... finished");
  147. $fetchUsersProgress->finish();
  148. }
  149. } else {
  150. $userList[] = $user;
  151. }
  152. $this->output->writeln("\n\n");
  153. $progress = new ProgressBar($this->output);
  154. $progress->setFormat(" %message% \n [%bar%]");
  155. $progress->start();
  156. $progress->setMessage("starting to decrypt files...");
  157. $progress->advance();
  158. $numberOfUsers = count($userList);
  159. $userNo = 1;
  160. foreach ($userList as $uid) {
  161. $userCount = "$uid ($userNo of $numberOfUsers)";
  162. $this->decryptUsersFiles($uid, $progress, $userCount);
  163. $userNo++;
  164. }
  165. $progress->setMessage("starting to decrypt files... finished");
  166. $progress->finish();
  167. $this->output->writeln("\n\n");
  168. }
  169. /**
  170. * encrypt files from the given user
  171. *
  172. * @param string $uid
  173. * @param ProgressBar $progress
  174. * @param string $userCount
  175. */
  176. protected function decryptUsersFiles($uid, ProgressBar $progress, $userCount) {
  177. $this->setupUserFS($uid);
  178. $directories = array();
  179. $directories[] = '/' . $uid . '/files';
  180. while ($root = array_pop($directories)) {
  181. $content = $this->rootView->getDirectoryContent($root);
  182. foreach ($content as $file) {
  183. // only decrypt files owned by the user
  184. if($file->getStorage()->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) {
  185. continue;
  186. }
  187. $path = $root . '/' . $file['name'];
  188. if ($this->rootView->is_dir($path)) {
  189. $directories[] = $path;
  190. continue;
  191. } else {
  192. try {
  193. $progress->setMessage("decrypt files for user $userCount: $path");
  194. $progress->advance();
  195. if ($file->isEncrypted() === false) {
  196. $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
  197. $progress->advance();
  198. } else {
  199. if ($this->decryptFile($path) === false) {
  200. $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
  201. $progress->advance();
  202. }
  203. }
  204. } catch (\Exception $e) {
  205. if (isset($this->failed[$uid])) {
  206. $this->failed[$uid][] = $path;
  207. } else {
  208. $this->failed[$uid] = [$path];
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. /**
  216. * encrypt file
  217. *
  218. * @param string $path
  219. * @return bool
  220. */
  221. protected function decryptFile($path) {
  222. // skip already decrypted files
  223. $fileInfo = $this->rootView->getFileInfo($path);
  224. if ($fileInfo !== false && !$fileInfo->isEncrypted()) {
  225. return true;
  226. }
  227. $source = $path;
  228. $target = $path . '.decrypted.' . $this->getTimestamp();
  229. try {
  230. $this->rootView->copy($source, $target);
  231. $this->rootView->rename($target, $source);
  232. } catch (DecryptionFailedException $e) {
  233. if ($this->rootView->file_exists($target)) {
  234. $this->rootView->unlink($target);
  235. }
  236. return false;
  237. }
  238. return true;
  239. }
  240. /**
  241. * get current timestamp
  242. *
  243. * @return int
  244. */
  245. protected function getTimestamp() {
  246. return time();
  247. }
  248. /**
  249. * setup user file system
  250. *
  251. * @param string $uid
  252. */
  253. protected function setupUserFS($uid) {
  254. \OC_Util::tearDownFS();
  255. \OC_Util::setupFS($uid);
  256. }
  257. }