1<?php
2/**
3 * @author Joas Schilling <coding@schilljs.com>
4 *
5 * @copyright Copyright (c) 2018, ownCloud GmbH
6 * @license AGPL-3.0
7 *
8 * This code is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License, version 3,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License, version 3,
18 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19 *
20 */
21
22namespace OC\Core\Command\Encryption;
23
24use OCP\IConfig;
25use OCP\IDBConnection;
26use Symfony\Component\Console\Command\Command;
27use Symfony\Component\Console\Input\InputInterface;
28use Symfony\Component\Console\Output\OutputInterface;
29
30class Disable extends Command {
31	/** @var IDBConnection */
32	protected $db;
33	/** @var IConfig */
34	protected $config;
35
36	/**
37	 * @param IDBConnection $db
38	 * @param IConfig $config
39	 */
40	public function __construct(IDBConnection $db, IConfig $config) {
41		parent::__construct();
42		$this->db = $db;
43		$this->config = $config;
44	}
45
46	protected function configure() {
47		$this
48			->setName('encryption:disable')
49			->setDescription('Disable encryption.')
50		;
51	}
52
53	protected function execute(InputInterface $input, OutputInterface $output) {
54		$qb = $this->db->getQueryBuilder();
55		$qb->select($qb->expr()->literal('1'))
56			->from('filecache', 'fc')
57			->where($qb->expr()->gte('fc.encrypted', $qb->expr()->literal('1')))
58			->setMaxResults(1);
59		$results = $qb->execute();
60		$hasEncryptedFiles = (bool) $results->fetchColumn(0);
61		$results->closeCursor();
62		if ($hasEncryptedFiles !== false) {
63			$output->writeln('<info>The system still have encrypted files. Please decrypt them all before disabling encryption.</info>');
64			return 1;
65		}
66
67		/**
68		 * Delete both useMasterKey and userSpecificKey
69		 */
70		$this->config->deleteAppValue('encryption', 'useMasterKey');
71		$this->config->deleteAppValue('encryption', 'userSpecificKey');
72
73		$output->writeln('<info>Cleaned up config</info>');
74
75		if ($this->config->getAppValue('core', 'encryption_enabled', 'no') !== 'yes') {
76			$output->writeln('Encryption is already disabled');
77		} else {
78			$this->config->setAppValue('core', 'encryption_enabled', 'no');
79			$output->writeln('<info>Encryption disabled</info>');
80		}
81		return 0;
82	}
83}
84