@@ -39,233 +39,233 @@ |
||
| 39 | 39 | |
| 40 | 40 | class ChangeKeyStorageRoot extends Command { |
| 41 | 41 | |
| 42 | - /** @var View */ |
|
| 43 | - protected $rootView; |
|
| 44 | - |
|
| 45 | - /** @var IUserManager */ |
|
| 46 | - protected $userManager; |
|
| 47 | - |
|
| 48 | - /** @var IConfig */ |
|
| 49 | - protected $config; |
|
| 50 | - |
|
| 51 | - /** @var Util */ |
|
| 52 | - protected $util; |
|
| 53 | - |
|
| 54 | - /** @var QuestionHelper */ |
|
| 55 | - protected $questionHelper; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param View $view |
|
| 59 | - * @param IUserManager $userManager |
|
| 60 | - * @param IConfig $config |
|
| 61 | - * @param Util $util |
|
| 62 | - * @param QuestionHelper $questionHelper |
|
| 63 | - */ |
|
| 64 | - public function __construct(View $view, IUserManager $userManager, IConfig $config, Util $util, QuestionHelper $questionHelper) { |
|
| 65 | - parent::__construct(); |
|
| 66 | - $this->rootView = $view; |
|
| 67 | - $this->userManager = $userManager; |
|
| 68 | - $this->config = $config; |
|
| 69 | - $this->util = $util; |
|
| 70 | - $this->questionHelper = $questionHelper; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - protected function configure() { |
|
| 74 | - parent::configure(); |
|
| 75 | - $this |
|
| 76 | - ->setName('encryption:change-key-storage-root') |
|
| 77 | - ->setDescription('Change key storage root') |
|
| 78 | - ->addArgument( |
|
| 79 | - 'newRoot', |
|
| 80 | - InputArgument::OPTIONAL, |
|
| 81 | - 'new root of the key storage relative to the data folder' |
|
| 82 | - ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 86 | - $oldRoot = $this->util->getKeyStorageRoot(); |
|
| 87 | - $newRoot = $input->getArgument('newRoot'); |
|
| 88 | - |
|
| 89 | - if ($newRoot === null) { |
|
| 90 | - $question = new ConfirmationQuestion('No storage root given, do you want to reset the key storage root to the default location? (y/n) ', false); |
|
| 91 | - if (!$this->questionHelper->ask($input, $output, $question)) { |
|
| 92 | - return; |
|
| 93 | - } |
|
| 94 | - $newRoot = ''; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $oldRootDescription = $oldRoot !== '' ? $oldRoot : 'default storage location'; |
|
| 98 | - $newRootDescription = $newRoot !== '' ? $newRoot : 'default storage location'; |
|
| 99 | - $output->writeln("Change key storage root from <info>$oldRootDescription</info> to <info>$newRootDescription</info>"); |
|
| 100 | - $success = $this->moveAllKeys($oldRoot, $newRoot, $output); |
|
| 101 | - if ($success) { |
|
| 102 | - $this->util->setKeyStorageRoot($newRoot); |
|
| 103 | - $output->writeln(''); |
|
| 104 | - $output->writeln("Key storage root successfully changed to <info>$newRootDescription</info>"); |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * move keys to new key storage root |
|
| 110 | - * |
|
| 111 | - * @param string $oldRoot |
|
| 112 | - * @param string $newRoot |
|
| 113 | - * @param OutputInterface $output |
|
| 114 | - * @return bool |
|
| 115 | - * @throws \Exception |
|
| 116 | - */ |
|
| 117 | - protected function moveAllKeys($oldRoot, $newRoot, OutputInterface $output) { |
|
| 118 | - |
|
| 119 | - $output->writeln("Start to move keys:"); |
|
| 120 | - |
|
| 121 | - if ($this->rootView->is_dir($oldRoot) === false) { |
|
| 122 | - $output->writeln("No old keys found: Nothing needs to be moved"); |
|
| 123 | - return false; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $this->prepareNewRoot($newRoot); |
|
| 127 | - $this->moveSystemKeys($oldRoot, $newRoot); |
|
| 128 | - $this->moveUserKeys($oldRoot, $newRoot, $output); |
|
| 129 | - |
|
| 130 | - return true; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * prepare new key storage |
|
| 135 | - * |
|
| 136 | - * @param string $newRoot |
|
| 137 | - * @throws \Exception |
|
| 138 | - */ |
|
| 139 | - protected function prepareNewRoot($newRoot) { |
|
| 140 | - if ($this->rootView->is_dir($newRoot) === false) { |
|
| 141 | - throw new \Exception("New root folder doesn't exist. Please create the folder or check the permissions and try again."); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $result = $this->rootView->file_put_contents( |
|
| 145 | - $newRoot . '/' . Storage::KEY_STORAGE_MARKER, |
|
| 146 | - 'Nextcloud will detect this folder as key storage root only if this file exists' |
|
| 147 | - ); |
|
| 148 | - |
|
| 149 | - if (!$result) { |
|
| 150 | - throw new \Exception("Can't access the new root folder. Please check the permissions and make sure that the folder is in your data folder"); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * move system key folder |
|
| 158 | - * |
|
| 159 | - * @param string $oldRoot |
|
| 160 | - * @param string $newRoot |
|
| 161 | - */ |
|
| 162 | - protected function moveSystemKeys($oldRoot, $newRoot) { |
|
| 163 | - if ( |
|
| 164 | - $this->rootView->is_dir($oldRoot . '/files_encryption') && |
|
| 165 | - $this->targetExists($newRoot . '/files_encryption') === false |
|
| 166 | - ) { |
|
| 167 | - $this->rootView->rename($oldRoot . '/files_encryption', $newRoot . '/files_encryption'); |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * setup file system for the given user |
|
| 174 | - * |
|
| 175 | - * @param string $uid |
|
| 176 | - */ |
|
| 177 | - protected function setupUserFS($uid) { |
|
| 178 | - \OC_Util::tearDownFS(); |
|
| 179 | - \OC_Util::setupFS($uid); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * iterate over each user and move the keys to the new storage |
|
| 185 | - * |
|
| 186 | - * @param string $oldRoot |
|
| 187 | - * @param string $newRoot |
|
| 188 | - * @param OutputInterface $output |
|
| 189 | - */ |
|
| 190 | - protected function moveUserKeys($oldRoot, $newRoot, OutputInterface $output) { |
|
| 191 | - |
|
| 192 | - $progress = new ProgressBar($output); |
|
| 193 | - $progress->start(); |
|
| 194 | - |
|
| 195 | - |
|
| 196 | - foreach($this->userManager->getBackends() as $backend) { |
|
| 197 | - $limit = 500; |
|
| 198 | - $offset = 0; |
|
| 199 | - do { |
|
| 200 | - $users = $backend->getUsers('', $limit, $offset); |
|
| 201 | - foreach ($users as $user) { |
|
| 202 | - $progress->advance(); |
|
| 203 | - $this->setupUserFS($user); |
|
| 204 | - $this->moveUserEncryptionFolder($user, $oldRoot, $newRoot); |
|
| 205 | - } |
|
| 206 | - $offset += $limit; |
|
| 207 | - } while(count($users) >= $limit); |
|
| 208 | - } |
|
| 209 | - $progress->finish(); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * move user encryption folder to new root folder |
|
| 214 | - * |
|
| 215 | - * @param string $user |
|
| 216 | - * @param string $oldRoot |
|
| 217 | - * @param string $newRoot |
|
| 218 | - * @throws \Exception |
|
| 219 | - */ |
|
| 220 | - protected function moveUserEncryptionFolder($user, $oldRoot, $newRoot) { |
|
| 221 | - |
|
| 222 | - if ($this->userManager->userExists($user)) { |
|
| 223 | - |
|
| 224 | - $source = $oldRoot . '/' . $user . '/files_encryption'; |
|
| 225 | - $target = $newRoot . '/' . $user . '/files_encryption'; |
|
| 226 | - if ( |
|
| 227 | - $this->rootView->is_dir($source) && |
|
| 228 | - $this->targetExists($target) === false |
|
| 229 | - ) { |
|
| 230 | - $this->prepareParentFolder($newRoot . '/' . $user); |
|
| 231 | - $this->rootView->rename($source, $target); |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Make preparations to filesystem for saving a key file |
|
| 238 | - * |
|
| 239 | - * @param string $path relative to data/ |
|
| 240 | - */ |
|
| 241 | - protected function prepareParentFolder($path) { |
|
| 242 | - $path = Filesystem::normalizePath($path); |
|
| 243 | - // If the file resides within a subdirectory, create it |
|
| 244 | - if ($this->rootView->file_exists($path) === false) { |
|
| 245 | - $sub_dirs = explode('/', ltrim($path, '/')); |
|
| 246 | - $dir = ''; |
|
| 247 | - foreach ($sub_dirs as $sub_dir) { |
|
| 248 | - $dir .= '/' . $sub_dir; |
|
| 249 | - if ($this->rootView->file_exists($dir) === false) { |
|
| 250 | - $this->rootView->mkdir($dir); |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * check if target already exists |
|
| 258 | - * |
|
| 259 | - * @param $path |
|
| 260 | - * @return bool |
|
| 261 | - * @throws \Exception |
|
| 262 | - */ |
|
| 263 | - protected function targetExists($path) { |
|
| 264 | - if ($this->rootView->file_exists($path)) { |
|
| 265 | - throw new \Exception("new folder '$path' already exists"); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - return false; |
|
| 269 | - } |
|
| 42 | + /** @var View */ |
|
| 43 | + protected $rootView; |
|
| 44 | + |
|
| 45 | + /** @var IUserManager */ |
|
| 46 | + protected $userManager; |
|
| 47 | + |
|
| 48 | + /** @var IConfig */ |
|
| 49 | + protected $config; |
|
| 50 | + |
|
| 51 | + /** @var Util */ |
|
| 52 | + protected $util; |
|
| 53 | + |
|
| 54 | + /** @var QuestionHelper */ |
|
| 55 | + protected $questionHelper; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param View $view |
|
| 59 | + * @param IUserManager $userManager |
|
| 60 | + * @param IConfig $config |
|
| 61 | + * @param Util $util |
|
| 62 | + * @param QuestionHelper $questionHelper |
|
| 63 | + */ |
|
| 64 | + public function __construct(View $view, IUserManager $userManager, IConfig $config, Util $util, QuestionHelper $questionHelper) { |
|
| 65 | + parent::__construct(); |
|
| 66 | + $this->rootView = $view; |
|
| 67 | + $this->userManager = $userManager; |
|
| 68 | + $this->config = $config; |
|
| 69 | + $this->util = $util; |
|
| 70 | + $this->questionHelper = $questionHelper; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + protected function configure() { |
|
| 74 | + parent::configure(); |
|
| 75 | + $this |
|
| 76 | + ->setName('encryption:change-key-storage-root') |
|
| 77 | + ->setDescription('Change key storage root') |
|
| 78 | + ->addArgument( |
|
| 79 | + 'newRoot', |
|
| 80 | + InputArgument::OPTIONAL, |
|
| 81 | + 'new root of the key storage relative to the data folder' |
|
| 82 | + ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 86 | + $oldRoot = $this->util->getKeyStorageRoot(); |
|
| 87 | + $newRoot = $input->getArgument('newRoot'); |
|
| 88 | + |
|
| 89 | + if ($newRoot === null) { |
|
| 90 | + $question = new ConfirmationQuestion('No storage root given, do you want to reset the key storage root to the default location? (y/n) ', false); |
|
| 91 | + if (!$this->questionHelper->ask($input, $output, $question)) { |
|
| 92 | + return; |
|
| 93 | + } |
|
| 94 | + $newRoot = ''; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $oldRootDescription = $oldRoot !== '' ? $oldRoot : 'default storage location'; |
|
| 98 | + $newRootDescription = $newRoot !== '' ? $newRoot : 'default storage location'; |
|
| 99 | + $output->writeln("Change key storage root from <info>$oldRootDescription</info> to <info>$newRootDescription</info>"); |
|
| 100 | + $success = $this->moveAllKeys($oldRoot, $newRoot, $output); |
|
| 101 | + if ($success) { |
|
| 102 | + $this->util->setKeyStorageRoot($newRoot); |
|
| 103 | + $output->writeln(''); |
|
| 104 | + $output->writeln("Key storage root successfully changed to <info>$newRootDescription</info>"); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * move keys to new key storage root |
|
| 110 | + * |
|
| 111 | + * @param string $oldRoot |
|
| 112 | + * @param string $newRoot |
|
| 113 | + * @param OutputInterface $output |
|
| 114 | + * @return bool |
|
| 115 | + * @throws \Exception |
|
| 116 | + */ |
|
| 117 | + protected function moveAllKeys($oldRoot, $newRoot, OutputInterface $output) { |
|
| 118 | + |
|
| 119 | + $output->writeln("Start to move keys:"); |
|
| 120 | + |
|
| 121 | + if ($this->rootView->is_dir($oldRoot) === false) { |
|
| 122 | + $output->writeln("No old keys found: Nothing needs to be moved"); |
|
| 123 | + return false; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $this->prepareNewRoot($newRoot); |
|
| 127 | + $this->moveSystemKeys($oldRoot, $newRoot); |
|
| 128 | + $this->moveUserKeys($oldRoot, $newRoot, $output); |
|
| 129 | + |
|
| 130 | + return true; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * prepare new key storage |
|
| 135 | + * |
|
| 136 | + * @param string $newRoot |
|
| 137 | + * @throws \Exception |
|
| 138 | + */ |
|
| 139 | + protected function prepareNewRoot($newRoot) { |
|
| 140 | + if ($this->rootView->is_dir($newRoot) === false) { |
|
| 141 | + throw new \Exception("New root folder doesn't exist. Please create the folder or check the permissions and try again."); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $result = $this->rootView->file_put_contents( |
|
| 145 | + $newRoot . '/' . Storage::KEY_STORAGE_MARKER, |
|
| 146 | + 'Nextcloud will detect this folder as key storage root only if this file exists' |
|
| 147 | + ); |
|
| 148 | + |
|
| 149 | + if (!$result) { |
|
| 150 | + throw new \Exception("Can't access the new root folder. Please check the permissions and make sure that the folder is in your data folder"); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * move system key folder |
|
| 158 | + * |
|
| 159 | + * @param string $oldRoot |
|
| 160 | + * @param string $newRoot |
|
| 161 | + */ |
|
| 162 | + protected function moveSystemKeys($oldRoot, $newRoot) { |
|
| 163 | + if ( |
|
| 164 | + $this->rootView->is_dir($oldRoot . '/files_encryption') && |
|
| 165 | + $this->targetExists($newRoot . '/files_encryption') === false |
|
| 166 | + ) { |
|
| 167 | + $this->rootView->rename($oldRoot . '/files_encryption', $newRoot . '/files_encryption'); |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * setup file system for the given user |
|
| 174 | + * |
|
| 175 | + * @param string $uid |
|
| 176 | + */ |
|
| 177 | + protected function setupUserFS($uid) { |
|
| 178 | + \OC_Util::tearDownFS(); |
|
| 179 | + \OC_Util::setupFS($uid); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * iterate over each user and move the keys to the new storage |
|
| 185 | + * |
|
| 186 | + * @param string $oldRoot |
|
| 187 | + * @param string $newRoot |
|
| 188 | + * @param OutputInterface $output |
|
| 189 | + */ |
|
| 190 | + protected function moveUserKeys($oldRoot, $newRoot, OutputInterface $output) { |
|
| 191 | + |
|
| 192 | + $progress = new ProgressBar($output); |
|
| 193 | + $progress->start(); |
|
| 194 | + |
|
| 195 | + |
|
| 196 | + foreach($this->userManager->getBackends() as $backend) { |
|
| 197 | + $limit = 500; |
|
| 198 | + $offset = 0; |
|
| 199 | + do { |
|
| 200 | + $users = $backend->getUsers('', $limit, $offset); |
|
| 201 | + foreach ($users as $user) { |
|
| 202 | + $progress->advance(); |
|
| 203 | + $this->setupUserFS($user); |
|
| 204 | + $this->moveUserEncryptionFolder($user, $oldRoot, $newRoot); |
|
| 205 | + } |
|
| 206 | + $offset += $limit; |
|
| 207 | + } while(count($users) >= $limit); |
|
| 208 | + } |
|
| 209 | + $progress->finish(); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * move user encryption folder to new root folder |
|
| 214 | + * |
|
| 215 | + * @param string $user |
|
| 216 | + * @param string $oldRoot |
|
| 217 | + * @param string $newRoot |
|
| 218 | + * @throws \Exception |
|
| 219 | + */ |
|
| 220 | + protected function moveUserEncryptionFolder($user, $oldRoot, $newRoot) { |
|
| 221 | + |
|
| 222 | + if ($this->userManager->userExists($user)) { |
|
| 223 | + |
|
| 224 | + $source = $oldRoot . '/' . $user . '/files_encryption'; |
|
| 225 | + $target = $newRoot . '/' . $user . '/files_encryption'; |
|
| 226 | + if ( |
|
| 227 | + $this->rootView->is_dir($source) && |
|
| 228 | + $this->targetExists($target) === false |
|
| 229 | + ) { |
|
| 230 | + $this->prepareParentFolder($newRoot . '/' . $user); |
|
| 231 | + $this->rootView->rename($source, $target); |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Make preparations to filesystem for saving a key file |
|
| 238 | + * |
|
| 239 | + * @param string $path relative to data/ |
|
| 240 | + */ |
|
| 241 | + protected function prepareParentFolder($path) { |
|
| 242 | + $path = Filesystem::normalizePath($path); |
|
| 243 | + // If the file resides within a subdirectory, create it |
|
| 244 | + if ($this->rootView->file_exists($path) === false) { |
|
| 245 | + $sub_dirs = explode('/', ltrim($path, '/')); |
|
| 246 | + $dir = ''; |
|
| 247 | + foreach ($sub_dirs as $sub_dir) { |
|
| 248 | + $dir .= '/' . $sub_dir; |
|
| 249 | + if ($this->rootView->file_exists($dir) === false) { |
|
| 250 | + $this->rootView->mkdir($dir); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * check if target already exists |
|
| 258 | + * |
|
| 259 | + * @param $path |
|
| 260 | + * @return bool |
|
| 261 | + * @throws \Exception |
|
| 262 | + */ |
|
| 263 | + protected function targetExists($path) { |
|
| 264 | + if ($this->rootView->file_exists($path)) { |
|
| 265 | + throw new \Exception("new folder '$path' already exists"); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + return false; |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | 271 | } |