summaryrefslogtreecommitdiffstats
path: root/apps/encryption/lib/migration.php
blob: db9541d6f5a41039f4f4dca26c05da2be178faff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/**
 * @author Björn Schießle <schiessle@owncloud.com>
 * @author Joas Schilling <nickvergessen@owncloud.com>
 * @author Robin Appelman <icewind@owncloud.com>
 *
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace OCA\Encryption;


use OC\Files\View;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\ILogger;

class Migration {

	private $moduleId;
	/** @var \OC\Files\View */
	private $view;
	/** @var \OCP\IDBConnection */
	private $connection;
	/** @var IConfig */
	private $config;
	/** @var  ILogger */
	private $logger;
	/** @var string*/
	protected $installedVersion;

	/**
	 * @param IConfig $config
	 * @param View $view
	 * @param IDBConnection $connection
	 * @param ILogger $logger
	 */
	public function __construct(IConfig $config, View $view, IDBConnection $connection, ILogger $logger) {
		$this->view = $view;
		$this->view->disableCacheUpdate();
		$this->connection = $connection;
		$this->moduleId = \OCA\Encryption\Crypto\Encryption::ID;
		$this->config = $config;
		$this->logger = $logger;
		$this->installedVersion = $this->config->getAppValue('files_encryption', 'installed_version', '-1');
	}

	public function finalCleanUp() {
		$this->view->deleteAll('files_encryption/public_keys');
		$this->updateFileCache();
		$this->config->deleteAppValue('files_encryption', 'installed_version');
	}

	/**
	 * update file cache, copy unencrypted_size to the 'size' column
	 */
	private function updateFileCache() {
		// make sure that we don't update the file cache multiple times
		// only update during the first run
		if ($this->installedVersion !== '-1') {
			$query = $this->connection->getQueryBuilder();
			$query->update('filecache')
				->set('size', 'unencrypted_size')
				->where($query->expr()->eq('encrypted', $query->createParameter('encrypted')))
				->setParameter('encrypted', 1);
			$query->execute();
		}
	}

	/**
	 * iterate through users and reorganize the folder structure
	 */
	public function reorganizeFolderStructure() {
		$this->reorganizeSystemFolderStructure();

		$limit = 500;
		$offset = 0;
		do {
			$users = \OCP\User::getUsers('', $limit, $offset);
			foreach ($users as $user) {
				$this->reorganizeFolderStructureForUser($user);
			}
			$offset += $limit;
		} while (count($users) >= $limit);
	}

	/**
	 * reorganize system wide folder structure
	 */
	public function reorganizeSystemFolderStructure() {

		$this->createPathForKeys('/files_encryption');

		// backup system wide folders
		$this->backupSystemWideKeys();

		// rename system wide mount point
		$this->renameFileKeys('', '/files_encryption/keys');

		// rename system private keys
		$this->renameSystemPrivateKeys();

		$storage = $this->view->getMount('')->getStorage();
		$storage->getScanner()->scan('files_encryption');
	}


	/**
	 * reorganize folder structure for user
	 *
	 * @param string $user
	 */
	public function reorganizeFolderStructureForUser($user) {
		// backup all keys
		\OC_Util::tearDownFS();
		\OC_Util::setupFS($user);
		if ($this->backupUserKeys($user)) {
			// rename users private key
			$this->renameUsersPrivateKey($user);
			$this->renameUsersPublicKey($user);
			// rename file keys
			$path = '/files_encryption/keys';
			$this->renameFileKeys($user, $path);
			$trashPath = '/files_trashbin/keys';
			if (\OC_App::isEnabled('files_trashbin') && $this->view->is_dir($user . '/' . $trashPath)) {
				$this->renameFileKeys($user, $trashPath, true);
				$this->view->deleteAll($trashPath);
			}
			// delete old folders
			$this->deleteOldKeys($user);
			$this->view->getMount('/' . $user)->getStorage()->getScanner()->scan('files_encryption');
		}
	}

	/**
	 * update database
	 */
	public function updateDB() {

		// make sure that we don't update the file cache multiple times
		// only update during the first run
		if ($this->installedVersion === '-1') {
			return;
		}

		// delete left-over from old encryption which is no longer needed
		$this->config->deleteAppValue('files_encryption', 'ocsid');
		$this->config->deleteAppValue('files_encryption', 'types');
		$this->config->deleteAppValue('files_encryption', 'enabled');

		$oldAppValues = $this->connection->getQueryBuilder();
		$oldAppValues->select('*')
			->from('appconfig')
			->where($oldAppValues->expr()->eq('appid', $oldAppValues->createParameter('appid')))
			->setParameter('appid', 'files_encryption');
		$appSettings = $oldAppValues->execute();

		while ($row = $appSettings->fetch()) {
			// 'installed_version' gets deleted at the end of the migration process
			if ($row['configkey'] !== 'installed_version' ) {
				$this->config->setAppValue('encryption', $row['configkey'], $row['configvalue']);
				$this->config->deleteAppValue('files_encryption', $row['configkey']);
			}
		}

		$oldPreferences = $this->connection->getQueryBuilder();
		$oldPreferences->select('*')
			->from('preferences')
			->where($oldPreferences->expr()->eq('appid', $oldPreferences->createParameter('appid')))
			->setParameter('appid', 'files_encryption');
		$preferenceSettings = $oldPreferences->execute();

		while ($row = $preferenceSettings->fetch()) {
			$this->config->setUserValue($row['userid'], 'encryption', $row['configkey'], $row['configvalue']);
			$this->config->deleteUserValue($row['userid'], 'files_encryption', $row['configkey']);
		}
	}

	/**
	 * create backup of system-wide keys
	 */
	private function backupSystemWideKeys() {
		$backupDir = 'encryption_migration_backup_' . date("Y-m-d_H-i-s");
		$this->view->mkdir($backupDir);
		$this->view->copy('files_encryption', $backupDir . '/files_encryption');
	}

	/**
	 * create backup of user specific keys
	 *
	 * @param string $user
	 * @return bool
	 */
	private function backupUserKeys($user) {
		$encryptionDir = $user . '/files_encryption';
		if ($this->view->is_dir($encryptionDir)) {
			$backupDir = $user . '/encryption_migration_backup_' . date("Y-m-d_H-i-s");
			$this->view->mkdir($backupDir);
			$this->view->copy($encryptionDir, $backupDir);
			return true;
		}
		return false;
	}

	/**
	 * rename system-wide private keys
	 */
	private function renameSystemPrivateKeys() {
		$dh = $this->view->opendir('files_encryption');
		$this->createPathForKeys('/files_encryption/' . $this->moduleId );
		if (is_resource($dh)) {
			while (($privateKey = readdir($dh)) !== false) {
				if (!\OC\Files\Filesystem::isIgnoredDir($privateKey) ) {
					if (!$this->view->is_dir('/files_encryption/' . $privateKey)) {
						$this->view->rename('files_encryption/' . $privateKey, 'files_encryption/' . $this->moduleId . '/' . $privateKey);
						$this->renameSystemPublicKey($privateKey);
					}
				}
			}
			closedir($dh);
		}
	}

	/**
	 * rename system wide public key
	 *
	 * @param string $privateKey private key for which we want to rename the corresponding public key
	 */
	private function renameSystemPublicKey($privateKey) {
		$publicKey = substr($privateKey,0 , strrpos($privateKey, '.privateKey')) . '.publicKey';
		$this->view->rename('files_encryption/public_keys/' . $publicKey, 'files_encryption/' . $this->moduleId . '/' . $publicKey);
	}

	/**
	 * rename user-specific private keys
	 *
	 * @param string $user
	 */
	private function renameUsersPrivateKey($user) {
		$oldPrivateKey = $user . '/files_encryption/' . $user . '.privateKey';
		$newPrivateKey = $user . '/files_encryption/' . $this->moduleId . '/' . $user . '.privateKey';
		if ($this->view->file_exists($oldPrivateKey)) {
			$this->createPathForKeys(dirname($newPrivateKey));
			$this->view->rename($oldPrivateKey, $newPrivateKey);
		}
	}

	/**
	 * rename user-specific public keys
	 *
	 * @param string $user
	 */
	private function renameUsersPublicKey($user) {
		$oldPublicKey = '/files_encryption/public_keys/' . $user . '.publicKey';
		$newPublicKey = $user . '/files_encryption/' . $this->moduleId . '/' . $user . '.publicKey';
		if ($this->view->file_exists($oldPublicKey)) {
			$this->createPathForKeys(dirname($newPublicKey));
			$this->view->rename($oldPublicKey, $newPublicKey);
		}
	}

	/**
	 * rename file keys
	 *
	 * @param string $user
	 * @param string $path
	 * @param bool $trash
	 */
	private function renameFileKeys($user, $path, $trash = false) {

		if ($this->view->is_dir($user . '/' . $path) === false) {
			$this->logger->info('Skip dir /' . $user . '/' . $path . ': does not exist');
			return;
		}

		$dh = $this->view->opendir($user . '/' . $path);

		if (is_resource($dh)) {
			while (($file = readdir($dh)) !== false) {
				if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
					if ($this->view->is_dir($user . '/' . $path . '/' . $file)) {
						$this->renameFileKeys($user, $path . '/' . $file, $trash);
					} else {
						$target = $this->getTargetDir($user, $path, $file, $trash);
						if ($target !== false) {
							$this->createPathForKeys(dirname($target));
							$this->view->rename($user . '/' . $path . '/' . $file, $target);
						} else {
							$this->logger->warning(
								'did not move key "' . $file
								. '" could not find the corresponding file in /data/' . $user . '/files.'
							. 'Most likely the key was already moved in a previous migration run and is already on the right place.');
						}
					}
				}
			}
			closedir($dh);
		}
	}

	/**
	 * get system mount points
	 * wrap static method so that it can be mocked for testing
	 *
	 * @internal
	 * @return array
	 */
	protected function getSystemMountPoints() {
		return \OC_Mount_Config::getSystemMountPoints();
	}

	/**
	 * generate target directory
	 *
	 * @param string $user
	 * @param string $keyPath
	 * @param string $filename
	 * @param bool $trash
	 * @return string
	 */
	private function getTargetDir($user, $keyPath, $filename, $trash) {
		if ($trash) {
			$filePath = substr($keyPath, strlen('/files_trashbin/keys/'));
			$targetDir = $user . '/files_encryption/keys/files_trashbin/' . $filePath . '/' . $this->moduleId . '/' . $filename;
		} else {
			$filePath = substr($keyPath, strlen('/files_encryption/keys/'));
			$targetDir = $user . '/files_encryption/keys/files/' . $filePath . '/' . $this->moduleId . '/' . $filename;
		}

		if ($user === '') {
			// for system wide mounts we need to check if the mount point really exists
			$normalized = \OC\Files\Filesystem::normalizePath($filePath);
			$systemMountPoints = $this->getSystemMountPoints();
			foreach ($systemMountPoints as $mountPoint) {
				$normalizedMountPoint = \OC\Files\Filesystem::normalizePath($mountPoint['mountpoint']) . '/';
				if (strpos($normalized, $normalizedMountPoint) === 0)
					return $targetDir;
			}
		} else if ($trash === false && $this->view->file_exists('/' . $user. '/files/' . $filePath)) {
			return $targetDir;
		} else if ($trash === true && $this->view->file_exists('/' . $user. '/files_trashbin/' . $filePath)) {
				return $targetDir;
			}

		return false;
	}

	/**
	 * delete old keys
	 *
	 * @param string $user
	 */
	private function deleteOldKeys($user) {
		$this->view->deleteAll($user . '/files_encryption/keyfiles');
		$this->view->deleteAll($user . '/files_encryption/share-keys');
	}

	/**
	 * create directories for the keys recursively
	 *
	 * @param string $path
	 */
	private function createPathForKeys($path) {
		if (!$this->view->file_exists($path)) {
			$sub_dirs = explode('/', $path);
			$dir = '';
			foreach ($sub_dirs as $sub_dir) {
				$dir .= '/' . $sub_dir;
				if (!$this->view->is_dir($dir)) {
					$this->view->mkdir($dir);
				}
			}
		}
	}
}