MigrateKeyStorage.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Command\Encryption;
  25. use OC\Encryption\Keys\Storage;
  26. use OC\Encryption\Util;
  27. use OC\Files\View;
  28. use OCP\IConfig;
  29. use OCP\IUserManager;
  30. use OCP\Security\ICrypto;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Helper\ProgressBar;
  33. use Symfony\Component\Console\Helper\QuestionHelper;
  34. use Symfony\Component\Console\Input\InputInterface;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. class MigrateKeyStorage extends Command {
  37. /** @var View */
  38. protected $rootView;
  39. /** @var IUserManager */
  40. protected $userManager;
  41. /** @var IConfig */
  42. protected $config;
  43. /** @var Util */
  44. protected $util;
  45. /** @var QuestionHelper */
  46. protected $questionHelper;
  47. /**
  48. * @var ICrypto
  49. */
  50. private $crypto;
  51. public function __construct(View $view, IUserManager $userManager, IConfig $config, Util $util, ICrypto $crypto) {
  52. parent::__construct();
  53. $this->rootView = $view;
  54. $this->userManager = $userManager;
  55. $this->config = $config;
  56. $this->util = $util;
  57. $this->crypto = $crypto;
  58. }
  59. protected function configure() {
  60. parent::configure();
  61. $this
  62. ->setName('encryption:migrate-key-storage-format')
  63. ->setDescription('Migrate the format of the keystorage to a newer format');
  64. }
  65. protected function execute(InputInterface $input, OutputInterface $output): int {
  66. $root = $this->util->getKeyStorageRoot();
  67. $output->writeln("Updating key storage format");
  68. $this->updateKeys($root, $output);
  69. $output->writeln("Key storage format successfully updated");
  70. return 0;
  71. }
  72. /**
  73. * move keys to new key storage root
  74. *
  75. * @param string $root
  76. * @param OutputInterface $output
  77. * @return bool
  78. * @throws \Exception
  79. */
  80. protected function updateKeys(string $root, OutputInterface $output) {
  81. $output->writeln("Start to update the keys:");
  82. $this->updateSystemKeys($root);
  83. $this->updateUsersKeys($root, $output);
  84. $this->config->deleteSystemValue('encryption.key_storage_migrated');
  85. return true;
  86. }
  87. /**
  88. * move system key folder
  89. *
  90. * @param string $root
  91. */
  92. protected function updateSystemKeys($root) {
  93. if (!$this->rootView->is_dir($root . '/files_encryption')) {
  94. return;
  95. }
  96. $this->traverseKeys($root . '/files_encryption', null);
  97. }
  98. private function traverseKeys(string $folder, ?string $uid) {
  99. $listing = $this->rootView->getDirectoryContent($folder);
  100. foreach ($listing as $node) {
  101. if ($node['mimetype'] === 'httpd/unix-directory') {
  102. //ignore
  103. } else {
  104. $endsWith = function ($haystack, $needle) {
  105. $length = strlen($needle);
  106. if ($length === 0) {
  107. return true;
  108. }
  109. return (substr($haystack, -$length) === $needle);
  110. };
  111. if ($node['name'] === 'fileKey' ||
  112. $endsWith($node['name'], '.privateKey') ||
  113. $endsWith($node['name'], '.publicKey') ||
  114. $endsWith($node['name'], '.shareKey')) {
  115. $path = $folder . '/' . $node['name'];
  116. $content = $this->rootView->file_get_contents($path);
  117. try {
  118. $this->crypto->decrypt($content);
  119. continue;
  120. } catch (\Exception $e) {
  121. // Ignore we now update the data.
  122. }
  123. $data = [
  124. 'key' => base64_encode($content),
  125. 'uid' => $uid,
  126. ];
  127. $enc = $this->crypto->encrypt(json_encode($data));
  128. $this->rootView->file_put_contents($path, $enc);
  129. }
  130. }
  131. }
  132. }
  133. private function traverseFileKeys(string $folder) {
  134. $listing = $this->rootView->getDirectoryContent($folder);
  135. foreach ($listing as $node) {
  136. if ($node['mimetype'] === 'httpd/unix-directory') {
  137. $this->traverseFileKeys($folder . '/' . $node['name']);
  138. } else {
  139. $endsWith = function ($haystack, $needle) {
  140. $length = strlen($needle);
  141. if ($length === 0) {
  142. return true;
  143. }
  144. return (substr($haystack, -$length) === $needle);
  145. };
  146. if ($node['name'] === 'fileKey' ||
  147. $endsWith($node['name'], '.privateKey') ||
  148. $endsWith($node['name'], '.publicKey') ||
  149. $endsWith($node['name'], '.shareKey')) {
  150. $path = $folder . '/' . $node['name'];
  151. $content = $this->rootView->file_get_contents($path);
  152. try {
  153. $this->crypto->decrypt($content);
  154. continue;
  155. } catch (\Exception $e) {
  156. // Ignore we now update the data.
  157. }
  158. $data = [
  159. 'key' => base64_encode($content)
  160. ];
  161. $enc = $this->crypto->encrypt(json_encode($data));
  162. $this->rootView->file_put_contents($path, $enc);
  163. }
  164. }
  165. }
  166. }
  167. /**
  168. * setup file system for the given user
  169. *
  170. * @param string $uid
  171. */
  172. protected function setupUserFS($uid) {
  173. \OC_Util::tearDownFS();
  174. \OC_Util::setupFS($uid);
  175. }
  176. /**
  177. * iterate over each user and move the keys to the new storage
  178. *
  179. * @param string $root
  180. * @param OutputInterface $output
  181. */
  182. protected function updateUsersKeys(string $root, OutputInterface $output) {
  183. $progress = new ProgressBar($output);
  184. $progress->start();
  185. foreach ($this->userManager->getBackends() as $backend) {
  186. $limit = 500;
  187. $offset = 0;
  188. do {
  189. $users = $backend->getUsers('', $limit, $offset);
  190. foreach ($users as $user) {
  191. $progress->advance();
  192. $this->setupUserFS($user);
  193. $this->updateUserKeys($root, $user);
  194. }
  195. $offset += $limit;
  196. } while (count($users) >= $limit);
  197. }
  198. $progress->finish();
  199. }
  200. /**
  201. * move user encryption folder to new root folder
  202. *
  203. * @param string $root
  204. * @param string $user
  205. * @throws \Exception
  206. */
  207. protected function updateUserKeys(string $root, string $user) {
  208. if ($this->userManager->userExists($user)) {
  209. $source = $root . '/' . $user . '/files_encryption/OC_DEFAULT_MODULE';
  210. if ($this->rootView->is_dir($source)) {
  211. $this->traverseKeys($source, $user);
  212. }
  213. $source = $root . '/' . $user . '/files_encryption/keys';
  214. if ($this->rootView->is_dir($source)) {
  215. $this->traverseFileKeys($source);
  216. }
  217. }
  218. }
  219. }