OccController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  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. namespace OC\Core\Controller;
  22. use OCP\AppFramework\Controller;
  23. use OCP\AppFramework\Http\JSONResponse;
  24. use OC\Console\Application;
  25. use OCP\IConfig;
  26. use OCP\IRequest;
  27. use Symfony\Component\Console\Input\ArrayInput;
  28. use Symfony\Component\Console\Output\BufferedOutput;
  29. class OccController extends Controller {
  30. /** @var array */
  31. private $allowedCommands = [
  32. 'app:disable',
  33. 'app:enable',
  34. 'app:getpath',
  35. 'app:list',
  36. 'check',
  37. 'config:list',
  38. 'maintenance:mode',
  39. 'status',
  40. 'upgrade'
  41. ];
  42. /** @var IConfig */
  43. private $config;
  44. /** @var Application */
  45. private $console;
  46. /**
  47. * OccController constructor.
  48. *
  49. * @param string $appName
  50. * @param IRequest $request
  51. * @param IConfig $config
  52. * @param Application $console
  53. */
  54. public function __construct($appName, IRequest $request,
  55. IConfig $config, Application $console) {
  56. parent::__construct($appName, $request);
  57. $this->config = $config;
  58. $this->console = $console;
  59. }
  60. /**
  61. * @PublicPage
  62. * @NoCSRFRequired
  63. *
  64. * Execute occ command
  65. * Sample request
  66. * POST http://domain.tld/index.php/occ/status',
  67. * {
  68. * 'params': {
  69. * '--no-warnings':'1',
  70. * '--output':'json'
  71. * },
  72. * 'token': 'someToken'
  73. * }
  74. *
  75. * @param string $command
  76. * @param string $token
  77. * @param array $params
  78. *
  79. * @return JSONResponse
  80. * @throws \Exception
  81. */
  82. public function execute($command, $token, $params = []) {
  83. try {
  84. $this->validateRequest($command, $token);
  85. $output = new BufferedOutput();
  86. $formatter = $output->getFormatter();
  87. $formatter->setDecorated(false);
  88. $this->console->setAutoExit(false);
  89. $this->console->loadCommands(new ArrayInput([]), $output);
  90. $inputArray = array_merge(['command' => $command], $params);
  91. $input = new ArrayInput($inputArray);
  92. $exitCode = $this->console->run($input, $output);
  93. $response = $output->fetch();
  94. $json = [
  95. 'exitCode' => $exitCode,
  96. 'response' => $response
  97. ];
  98. } catch (\UnexpectedValueException $e){
  99. $json = [
  100. 'exitCode' => 126,
  101. 'response' => 'Not allowed',
  102. 'details' => $e->getMessage()
  103. ];
  104. }
  105. return new JSONResponse($json);
  106. }
  107. /**
  108. * Check if command is allowed and has a valid security token
  109. * @param $command
  110. * @param $token
  111. */
  112. protected function validateRequest($command, $token){
  113. if (!in_array($this->request->getRemoteAddress(), ['::1', '127.0.0.1', 'localhost'])) {
  114. throw new \UnexpectedValueException('Web executor is not allowed to run from a different host');
  115. }
  116. if (!in_array($command, $this->allowedCommands)) {
  117. throw new \UnexpectedValueException(sprintf('Command "%s" is not allowed to run via web request', $command));
  118. }
  119. $coreToken = $this->config->getSystemValue('updater.secret', '');
  120. if ($coreToken === '') {
  121. throw new \UnexpectedValueException(
  122. 'updater.secret is undefined in config/config.php. Either browse the admin settings in your ownCloud and click "Open updater" or define a strong secret using <pre>php -r \'echo password_hash("MyStrongSecretDoUseYourOwn!", PASSWORD_DEFAULT)."\n";\'</pre> and set this in the config.php.'
  123. );
  124. }
  125. if (!password_verify($token, $coreToken)) {
  126. throw new \UnexpectedValueException(
  127. 'updater.secret does not match the provided token'
  128. );
  129. }
  130. }
  131. }