2017-12-26 12:38:53 +02:00
|
|
|
const { BaseCommand } = require('./base-command.js');
|
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const EncryptionService = require('lib/services/EncryptionService');
|
|
|
|
const DecryptionWorker = require('lib/services/DecryptionWorker');
|
2017-12-04 21:01:56 +02:00
|
|
|
const BaseItem = require('lib/models/BaseItem');
|
2017-12-26 12:38:53 +02:00
|
|
|
const Setting = require('lib/models/Setting.js');
|
2019-03-05 22:57:15 +02:00
|
|
|
const { shim } = require('lib/shim');
|
|
|
|
const pathUtils = require('lib/path-utils.js');
|
|
|
|
const imageType = require('image-type');
|
|
|
|
const readChunk = require('read-chunk');
|
2017-12-26 12:38:53 +02:00
|
|
|
|
|
|
|
class Command extends BaseCommand {
|
|
|
|
usage() {
|
2017-12-04 21:01:56 +02:00
|
|
|
return 'e2ee <command> [path]';
|
2017-12-26 12:38:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
description() {
|
2019-03-05 22:57:15 +02:00
|
|
|
return _('Manages E2EE configuration. Commands are `enable`, `disable`, `decrypt`, `status`, `decrypt-file` and `target-status`.');
|
2017-12-26 12:38:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
options() {
|
|
|
|
return [
|
|
|
|
// This is here mostly for testing - shouldn't be used
|
|
|
|
['-p, --password <password>', 'Use this password as master password (For security reasons, it is not recommended to use this option).'],
|
2017-12-04 21:01:56 +02:00
|
|
|
['-v, --verbose', 'More verbose output for the `target-status` command'],
|
2019-03-05 22:57:15 +02:00
|
|
|
['-o, --output <directory>', 'Output directory'],
|
2017-12-26 12:38:53 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
async action(args) {
|
|
|
|
// change-password
|
|
|
|
|
|
|
|
const options = args.options;
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
const askForMasterKey = async error => {
|
2019-03-05 22:57:15 +02:00
|
|
|
const masterKeyId = error.masterKeyId;
|
|
|
|
const password = await this.prompt(_('Enter master password:'), { type: 'string', secure: true });
|
|
|
|
if (!password) {
|
|
|
|
this.stdout(_('Operation cancelled'));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Setting.setObjectKey('encryption.passwordCache', masterKeyId, password);
|
|
|
|
await EncryptionService.instance().loadMasterKeysFromSettings();
|
|
|
|
return true;
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2019-03-05 22:57:15 +02:00
|
|
|
|
2017-12-26 12:38:53 +02:00
|
|
|
if (args.command === 'enable') {
|
|
|
|
const password = options.password ? options.password.toString() : await this.prompt(_('Enter master password:'), { type: 'string', secure: true });
|
|
|
|
if (!password) {
|
|
|
|
this.stdout(_('Operation cancelled'));
|
|
|
|
return;
|
|
|
|
}
|
2019-10-30 20:12:06 +02:00
|
|
|
const password2 = await this.prompt(_('Confirm password:'), { type: 'string', secure: true });
|
2019-10-02 08:48:58 +02:00
|
|
|
if (!password2) {
|
|
|
|
this.stdout(_('Operation cancelled'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (password !== password2) {
|
2019-10-03 01:32:32 +02:00
|
|
|
this.stdout(_('Passwords do not match!'));
|
2019-10-02 08:48:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-12-26 12:38:53 +02:00
|
|
|
await EncryptionService.instance().generateMasterKeyAndEnableEncryption(password);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.command === 'disable') {
|
|
|
|
await EncryptionService.instance().disableEncryption();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.command === 'decrypt') {
|
2019-02-10 18:43:55 +02:00
|
|
|
if (args.path) {
|
|
|
|
const plainText = await EncryptionService.instance().decryptString(args.path);
|
|
|
|
this.stdout(plainText);
|
|
|
|
} else {
|
|
|
|
this.stdout(_('Starting decryption... Please wait as it may take several minutes depending on how much there is to decrypt.'));
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
await DecryptionWorker.instance().start();
|
|
|
|
break;
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'masterKeyNotLoaded') {
|
2019-03-05 22:57:15 +02:00
|
|
|
const ok = await askForMasterKey(error);
|
|
|
|
if (!ok) return;
|
2019-02-10 18:43:55 +02:00
|
|
|
continue;
|
2018-09-10 18:39:19 +02:00
|
|
|
}
|
2017-12-28 21:51:24 +02:00
|
|
|
|
2019-02-10 18:43:55 +02:00
|
|
|
throw error;
|
|
|
|
}
|
2018-09-10 18:39:19 +02:00
|
|
|
}
|
2019-02-10 18:43:55 +02:00
|
|
|
|
|
|
|
this.stdout(_('Completed decryption.'));
|
2018-06-28 21:48:39 +02:00
|
|
|
}
|
2017-12-28 21:51:24 +02:00
|
|
|
|
2017-12-26 12:38:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-12-04 21:01:56 +02:00
|
|
|
|
2018-01-05 19:40:57 +02:00
|
|
|
if (args.command === 'status') {
|
|
|
|
this.stdout(_('Encryption is: %s', Setting.value('encryption.enabled') ? _('Enabled') : _('Disabled')));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-05 22:57:15 +02:00
|
|
|
if (args.command === 'decrypt-file') {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
const outputDir = options.output ? options.output : require('os').tmpdir();
|
2019-09-19 23:51:18 +02:00
|
|
|
let outFile = `${outputDir}/${pathUtils.filename(args.path)}.${Date.now()}.bin`;
|
2019-03-05 22:57:15 +02:00
|
|
|
await EncryptionService.instance().decryptFile(args.path, outFile);
|
|
|
|
const buffer = await readChunk(outFile, 0, 64);
|
|
|
|
const detectedType = imageType(buffer);
|
|
|
|
|
|
|
|
if (detectedType) {
|
2019-09-19 23:51:18 +02:00
|
|
|
const newOutFile = `${outFile}.${detectedType.ext}`;
|
2019-03-05 22:57:15 +02:00
|
|
|
await shim.fsDriver().move(outFile, newOutFile);
|
|
|
|
outFile = newOutFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stdout(outFile);
|
|
|
|
break;
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'masterKeyNotLoaded') {
|
|
|
|
const ok = await askForMasterKey(error);
|
|
|
|
if (!ok) return;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-04 21:01:56 +02:00
|
|
|
if (args.command === 'target-status') {
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
|
|
|
|
const targetPath = args.path;
|
|
|
|
if (!targetPath) throw new Error('Please specify the sync target path.');
|
|
|
|
|
|
|
|
const dirPaths = function(targetPath) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const paths = [];
|
2019-07-30 09:35:42 +02:00
|
|
|
fs.readdirSync(targetPath).forEach(path => {
|
2017-12-04 21:01:56 +02:00
|
|
|
paths.push(path);
|
|
|
|
});
|
|
|
|
return paths;
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2017-12-04 21:01:56 +02:00
|
|
|
|
|
|
|
let itemCount = 0;
|
|
|
|
let resourceCount = 0;
|
|
|
|
let encryptedItemCount = 0;
|
|
|
|
let encryptedResourceCount = 0;
|
|
|
|
let otherItemCount = 0;
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const encryptedPaths = [];
|
|
|
|
const decryptedPaths = [];
|
2017-12-04 21:01:56 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const paths = dirPaths(targetPath);
|
2017-12-04 21:01:56 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < paths.length; i++) {
|
|
|
|
const path = paths[i];
|
2019-09-19 23:51:18 +02:00
|
|
|
const fullPath = `${targetPath}/${path}`;
|
2017-12-04 21:01:56 +02:00
|
|
|
const stat = await fs.stat(fullPath);
|
|
|
|
|
|
|
|
// this.stdout(fullPath);
|
|
|
|
|
|
|
|
if (path === '.resource') {
|
2020-03-14 01:46:14 +02:00
|
|
|
const resourcePaths = dirPaths(fullPath);
|
2017-12-04 21:01:56 +02:00
|
|
|
for (let j = 0; j < resourcePaths.length; j++) {
|
|
|
|
const resourcePath = resourcePaths[j];
|
|
|
|
resourceCount++;
|
2019-09-19 23:51:18 +02:00
|
|
|
const fullResourcePath = `${fullPath}/${resourcePath}`;
|
2017-12-04 21:01:56 +02:00
|
|
|
const isEncrypted = await EncryptionService.instance().fileIsEncrypted(fullResourcePath);
|
|
|
|
if (isEncrypted) {
|
|
|
|
encryptedResourceCount++;
|
|
|
|
encryptedPaths.push(fullResourcePath);
|
|
|
|
} else {
|
|
|
|
decryptedPaths.push(fullResourcePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (stat.isDirectory()) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
const content = await fs.readFile(fullPath, 'utf8');
|
|
|
|
const item = await BaseItem.unserialize(content);
|
|
|
|
const ItemClass = BaseItem.itemClass(item);
|
|
|
|
|
|
|
|
if (!ItemClass.encryptionSupported()) {
|
|
|
|
otherItemCount++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-26 01:01:18 +02:00
|
|
|
itemCount++;
|
|
|
|
|
2017-12-04 21:01:56 +02:00
|
|
|
const isEncrypted = await EncryptionService.instance().itemIsEncrypted(item);
|
|
|
|
|
|
|
|
if (isEncrypted) {
|
|
|
|
encryptedItemCount++;
|
|
|
|
encryptedPaths.push(fullPath);
|
|
|
|
} else {
|
|
|
|
decryptedPaths.push(fullPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
this.stdout(`Encrypted items: ${encryptedItemCount}/${itemCount}`);
|
|
|
|
this.stdout(`Encrypted resources: ${encryptedResourceCount}/${resourceCount}`);
|
|
|
|
this.stdout(`Other items (never encrypted): ${otherItemCount}`);
|
2017-12-04 21:01:56 +02:00
|
|
|
|
|
|
|
if (options.verbose) {
|
|
|
|
this.stdout('');
|
|
|
|
this.stdout('# Encrypted paths');
|
|
|
|
this.stdout('');
|
|
|
|
for (let i = 0; i < encryptedPaths.length; i++) {
|
|
|
|
const path = encryptedPaths[i];
|
|
|
|
this.stdout(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stdout('');
|
|
|
|
this.stdout('# Decrypted paths');
|
|
|
|
this.stdout('');
|
|
|
|
for (let i = 0; i < decryptedPaths.length; i++) {
|
|
|
|
const path = decryptedPaths[i];
|
|
|
|
this.stdout(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:40:57 +02:00
|
|
|
return;
|
2017-12-04 21:01:56 +02:00
|
|
|
}
|
2017-12-26 12:38:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
module.exports = Command;
|