SelfUpdateCommand.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Composer\Composer;
  13. use Composer\Factory;
  14. use Composer\Config;
  15. use Composer\Util\Filesystem;
  16. use Composer\Util\Keys;
  17. use Composer\IO\IOInterface;
  18. use Composer\Downloader\FilesystemException;
  19. use Symfony\Component\Console\Input\InputInterface;
  20. use Symfony\Component\Console\Input\InputOption;
  21. use Symfony\Component\Console\Input\InputArgument;
  22. use Symfony\Component\Console\Output\OutputInterface;
  23. use Symfony\Component\Finder\Finder;
  24. /**
  25. * @author Igor Wiedler <igor@wiedler.ch>
  26. * @author Kevin Ran <kran@adobe.com>
  27. * @author Jordi Boggiano <j.boggiano@seld.be>
  28. */
  29. class SelfUpdateCommand extends Command
  30. {
  31. const HOMEPAGE = 'getcomposer.org';
  32. const OLD_INSTALL_EXT = '-old.phar';
  33. protected function configure()
  34. {
  35. $this
  36. ->setName('self-update')
  37. ->setAliases(array('selfupdate'))
  38. ->setDescription('Updates composer.phar to the latest version.')
  39. ->setDefinition(array(
  40. new InputOption('rollback', 'r', InputOption::VALUE_NONE, 'Revert to an older installation of composer'),
  41. new InputOption('clean-backups', null, InputOption::VALUE_NONE, 'Delete old backups during an update. This makes the current version of composer the only backup available after the update'),
  42. new InputArgument('version', InputArgument::OPTIONAL, 'The version to update to'),
  43. new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'),
  44. new InputOption('update-keys', null, InputOption::VALUE_NONE, 'Prompt user for a key update'),
  45. ))
  46. ->setHelp(<<<EOT
  47. The <info>self-update</info> command checks getcomposer.org for newer
  48. versions of composer and if found, installs the latest.
  49. <info>php composer.phar self-update</info>
  50. EOT
  51. )
  52. ;
  53. }
  54. protected function execute(InputInterface $input, OutputInterface $output)
  55. {
  56. $config = Factory::createConfig();
  57. if ($config->get('disable-tls') === true) {
  58. $baseUrl = 'http://' . self::HOMEPAGE;
  59. } else {
  60. $baseUrl = 'https://' . self::HOMEPAGE;
  61. }
  62. $io = $this->getIO();
  63. $remoteFilesystem = Factory::createRemoteFilesystem($io, $config);
  64. $cacheDir = $config->get('cache-dir');
  65. $rollbackDir = $config->get('data-dir');
  66. $home = $config->get('home');
  67. $localFilename = realpath($_SERVER['argv'][0]) ?: $_SERVER['argv'][0];
  68. if ($input->getOption('update-keys')) {
  69. return $this->fetchKeys($io, $config);
  70. }
  71. // check if current dir is writable and if not try the cache dir from settings
  72. $tmpDir = is_writable(dirname($localFilename)) ? dirname($localFilename) : $cacheDir;
  73. // check for permissions in local filesystem before start connection process
  74. if (!is_writable($tmpDir)) {
  75. throw new FilesystemException('Composer update failed: the "'.$tmpDir.'" directory used to download the temp file could not be written');
  76. }
  77. if ($input->getOption('rollback')) {
  78. return $this->rollback($output, $rollbackDir, $localFilename);
  79. }
  80. $latestVersion = trim($remoteFilesystem->getContents(self::HOMEPAGE, $baseUrl. '/version', false));
  81. $updateVersion = $input->getArgument('version') ?: $latestVersion;
  82. if (preg_match('{^[0-9a-f]{40}$}', $updateVersion) && $updateVersion !== $latestVersion) {
  83. $io->writeError('<error>You can not update to a specific SHA-1 as those phars are not available for download</error>');
  84. return 1;
  85. }
  86. if (Composer::VERSION === $updateVersion) {
  87. $io->writeError('<info>You are already using composer version '.$updateVersion.'.</info>');
  88. return 0;
  89. }
  90. $tempFilename = $tmpDir . '/' . basename($localFilename, '.phar').'-temp.phar';
  91. $backupFile = sprintf(
  92. '%s/%s-%s%s',
  93. $rollbackDir,
  94. strtr(Composer::RELEASE_DATE, ' :', '_-'),
  95. preg_replace('{^([0-9a-f]{7})[0-9a-f]{33}$}', '$1', Composer::VERSION),
  96. self::OLD_INSTALL_EXT
  97. );
  98. $updatingToTag = !preg_match('{^[0-9a-f]{40}$}', $updateVersion);
  99. $io->write(sprintf("Updating to version <info>%s</info>.", $updateVersion));
  100. $remoteFilename = $baseUrl . ($updatingToTag ? "/download/{$updateVersion}/composer.phar" : '/composer.phar');
  101. $signature = $remoteFilesystem->getContents(self::HOMEPAGE, $remoteFilename.'.sig', false);
  102. $remoteFilesystem->copy(self::HOMEPAGE, $remoteFilename, $tempFilename, !$input->getOption('no-progress'));
  103. if (!file_exists($tempFilename) || !$signature) {
  104. $io->writeError('<error>The download of the new composer version failed for an unexpected reason</error>');
  105. return 1;
  106. }
  107. // verify phar signature
  108. if (!extension_loaded('openssl') && $config->get('disable-tls')) {
  109. $io->writeError('<warning>Skipping phar signature verification as you have disabled OpenSSL via config.disable-tls</warning>');
  110. } else {
  111. if (!extension_loaded('openssl')) {
  112. throw new \RuntimeException('The openssl extension is required for phar signatures to be verified but it is not available. '
  113. . 'If you can not enable the openssl extension, you can disable this error, at your own risk, by setting the \'disable-tls\' option to true.');
  114. }
  115. $sigFile = 'file://'.$home.'/' . ($updatingToTag ? 'keys.tags.pub' : 'keys.dev.pub');
  116. if (!file_exists($sigFile)) {
  117. file_put_contents($home.'/keys.dev.pub', <<<DEVPUBKEY
  118. -----BEGIN PUBLIC KEY-----
  119. MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAnBDHjZS6e0ZMoK3xTD7f
  120. FNCzlXjX/Aie2dit8QXA03pSrOTbaMnxON3hUL47Lz3g1SC6YJEMVHr0zYq4elWi
  121. i3ecFEgzLcj+pZM5X6qWu2Ozz4vWx3JYo1/a/HYdOuW9e3lwS8VtS0AVJA+U8X0A
  122. hZnBmGpltHhO8hPKHgkJtkTUxCheTcbqn4wGHl8Z2SediDcPTLwqezWKUfrYzu1f
  123. o/j3WFwFs6GtK4wdYtiXr+yspBZHO3y1udf8eFFGcb2V3EaLOrtfur6XQVizjOuk
  124. 8lw5zzse1Qp/klHqbDRsjSzJ6iL6F4aynBc6Euqt/8ccNAIz0rLjLhOraeyj4eNn
  125. 8iokwMKiXpcrQLTKH+RH1JCuOVxQ436bJwbSsp1VwiqftPQieN+tzqy+EiHJJmGf
  126. TBAbWcncicCk9q2md+AmhNbvHO4PWbbz9TzC7HJb460jyWeuMEvw3gNIpEo2jYa9
  127. pMV6cVqnSa+wOc0D7pC9a6bne0bvLcm3S+w6I5iDB3lZsb3A9UtRiSP7aGSo7D72
  128. 8tC8+cIgZcI7k9vjvOqH+d7sdOU2yPCnRY6wFh62/g8bDnUpr56nZN1G89GwM4d4
  129. r/TU7BQQIzsZgAiqOGXvVklIgAMiV0iucgf3rNBLjjeNEwNSTTG9F0CtQ+7JLwaE
  130. wSEuAuRm+pRqi8BRnQ/GKUcCAwEAAQ==
  131. -----END PUBLIC KEY-----
  132. DEVPUBKEY
  133. );
  134. file_put_contents($home.'/keys.tags.pub', <<<TAGSPUBKEY
  135. -----BEGIN PUBLIC KEY-----
  136. MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA0Vi/2K6apCVj76nCnCl2
  137. MQUPdK+A9eqkYBacXo2wQBYmyVlXm2/n/ZsX6pCLYPQTHyr5jXbkQzBw8SKqPdlh
  138. vA7NpbMeNCz7wP/AobvUXM8xQuXKbMDTY2uZ4O7sM+PfGbptKPBGLe8Z8d2sUnTO
  139. bXtX6Lrj13wkRto7st/w/Yp33RHe9SlqkiiS4MsH1jBkcIkEHsRaveZzedUaxY0M
  140. mba0uPhGUInpPzEHwrYqBBEtWvP97t2vtfx8I5qv28kh0Y6t+jnjL1Urid2iuQZf
  141. noCMFIOu4vksK5HxJxxrN0GOmGmwVQjOOtxkwikNiotZGPR4KsVj8NnBrLX7oGuM
  142. nQvGciiu+KoC2r3HDBrpDeBVdOWxDzT5R4iI0KoLzFh2pKqwbY+obNPS2bj+2dgJ
  143. rV3V5Jjry42QOCBN3c88wU1PKftOLj2ECpewY6vnE478IipiEu7EAdK8Zwj2LmTr
  144. RKQUSa9k7ggBkYZWAeO/2Ag0ey3g2bg7eqk+sHEq5ynIXd5lhv6tC5PBdHlWipDK
  145. tl2IxiEnejnOmAzGVivE1YGduYBjN+mjxDVy8KGBrjnz1JPgAvgdwJ2dYw4Rsc/e
  146. TzCFWGk/HM6a4f0IzBWbJ5ot0PIi4amk07IotBXDWwqDiQTwyuGCym5EqWQ2BD95
  147. RGv89BPD+2DLnJysngsvVaUCAwEAAQ==
  148. -----END PUBLIC KEY-----
  149. TAGSPUBKEY
  150. );
  151. }
  152. $pubkeyid = openssl_pkey_get_public($sigFile);
  153. $algo = defined('OPENSSL_ALGO_SHA384') ? OPENSSL_ALGO_SHA384 : 'SHA384';
  154. if (!in_array('SHA384', openssl_get_md_methods())) {
  155. throw new \RuntimeException('SHA384 is not supported by your openssl extension, could not verify the phar file integrity');
  156. }
  157. $signature = json_decode($signature, true);
  158. $signature = base64_decode($signature['sha384']);
  159. $verified = 1 === openssl_verify(file_get_contents($tempFilename), $signature, $pubkeyid, $algo);
  160. openssl_free_key($pubkeyid);
  161. if (!$verified) {
  162. throw new \RuntimeException('The phar signature did not match the file you downloaded, this means your public keys are outdated or that the phar file is corrupt/has been modified');
  163. }
  164. }
  165. // remove saved installations of composer
  166. if ($input->getOption('clean-backups')) {
  167. $finder = $this->getOldInstallationFinder($rollbackDir);
  168. $fs = new Filesystem;
  169. foreach ($finder as $file) {
  170. $file = (string) $file;
  171. $io->writeError('<info>Removing: '.$file.'</info>');
  172. $fs->remove($file);
  173. }
  174. }
  175. if ($err = $this->setLocalPhar($localFilename, $tempFilename, $backupFile)) {
  176. $io->writeError('<error>The file is corrupted ('.$err->getMessage().').</error>');
  177. $io->writeError('<error>Please re-run the self-update command to try again.</error>');
  178. return 1;
  179. }
  180. if (file_exists($backupFile)) {
  181. $io->writeError('Use <info>composer self-update --rollback</info> to return to version '.Composer::VERSION);
  182. } else {
  183. $io->writeError('<warning>A backup of the current version could not be written to '.$backupFile.', no rollback possible</warning>');
  184. }
  185. }
  186. protected function fetchKeys(IOInterface $io, Config $config)
  187. {
  188. if (!$io->isInteractive()) {
  189. throw new \RuntimeException('Public keys can not be fetched in non-interactive mode, please run Composer interactively');
  190. }
  191. $io->write('Open <info>https://composer.github.io/pubkeys.html</info> to find the latest keys');
  192. $validator = function ($value) {
  193. if (!preg_match('{^-----BEGIN PUBLIC KEY-----$}', trim($value))) {
  194. throw new \UnexpectedValueException('Invalid input');
  195. }
  196. return trim($value)."\n";
  197. };
  198. $devKey = '';
  199. while (!preg_match('{(-----BEGIN PUBLIC KEY-----.+?-----END PUBLIC KEY-----)}s', $devKey, $match)) {
  200. $devKey = $io->askAndValidate('Enter Dev / Snapshot Public Key (including lines with -----): ', $validator);
  201. while ($line = $io->ask('')) {
  202. $devKey .= trim($line)."\n";
  203. if (trim($line) === '-----END PUBLIC KEY-----') {
  204. break;
  205. }
  206. }
  207. }
  208. file_put_contents($keyPath = $config->get('home').'/keys.dev.pub', $match[0]);
  209. $io->write('Stored key with fingerprint: ' . Keys::fingerprint($keyPath));
  210. $tagsKey = '';
  211. while (!preg_match('{(-----BEGIN PUBLIC KEY-----.+?-----END PUBLIC KEY-----)}s', $tagsKey, $match)) {
  212. $tagsKey = $io->askAndValidate('Enter Tags Public Key (including lines with -----): ', $validator);
  213. while ($line = $io->ask('')) {
  214. $tagsKey .= trim($line)."\n";
  215. if (trim($line) === '-----END PUBLIC KEY-----') {
  216. break;
  217. }
  218. }
  219. }
  220. file_put_contents($keyPath = $config->get('home').'/keys.tags.pub', $match[0]);
  221. $io->write('Stored key with fingerprint: ' . Keys::fingerprint($keyPath));
  222. $io->write('Public keys stored in '.$config->get('home'));
  223. }
  224. protected function rollback(OutputInterface $output, $rollbackDir, $localFilename)
  225. {
  226. $rollbackVersion = $this->getLastBackupVersion($rollbackDir);
  227. if (!$rollbackVersion) {
  228. throw new \UnexpectedValueException('Composer rollback failed: no installation to roll back to in "'.$rollbackDir.'"');
  229. }
  230. $old = $rollbackDir . '/' . $rollbackVersion . self::OLD_INSTALL_EXT;
  231. if (!is_file($old)) {
  232. throw new FilesystemException('Composer rollback failed: "'.$old.'" could not be found');
  233. }
  234. if (!is_readable($old)) {
  235. throw new FilesystemException('Composer rollback failed: "'.$old.'" could not be read');
  236. }
  237. $oldFile = $rollbackDir . "/{$rollbackVersion}" . self::OLD_INSTALL_EXT;
  238. $io = $this->getIO();
  239. $io->writeError(sprintf("Rolling back to version <info>%s</info>.", $rollbackVersion));
  240. if ($err = $this->setLocalPhar($localFilename, $oldFile)) {
  241. $io->writeError('<error>The backup file was corrupted ('.$err->getMessage().') and has been removed.</error>');
  242. return 1;
  243. }
  244. return 0;
  245. }
  246. /**
  247. * @param string $localFilename
  248. * @param string $newFilename
  249. * @param string $backupTarget
  250. */
  251. protected function setLocalPhar($localFilename, $newFilename, $backupTarget = null)
  252. {
  253. try {
  254. @chmod($newFilename, fileperms($localFilename));
  255. if (!ini_get('phar.readonly')) {
  256. // test the phar validity
  257. $phar = new \Phar($newFilename);
  258. // free the variable to unlock the file
  259. unset($phar);
  260. }
  261. // copy current file into installations dir
  262. if ($backupTarget && file_exists($localFilename)) {
  263. @copy($localFilename, $backupTarget);
  264. }
  265. rename($newFilename, $localFilename);
  266. } catch (\Exception $e) {
  267. if ($backupTarget) {
  268. @unlink($newFilename);
  269. }
  270. if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) {
  271. throw $e;
  272. }
  273. return $e;
  274. }
  275. }
  276. protected function getLastBackupVersion($rollbackDir)
  277. {
  278. $finder = $this->getOldInstallationFinder($rollbackDir);
  279. $finder->sortByName();
  280. $files = iterator_to_array($finder);
  281. if (count($files)) {
  282. return basename(end($files), self::OLD_INSTALL_EXT);
  283. }
  284. return false;
  285. }
  286. protected function getOldInstallationFinder($rollbackDir)
  287. {
  288. $finder = Finder::create()
  289. ->depth(0)
  290. ->files()
  291. ->name('*' . self::OLD_INSTALL_EXT)
  292. ->in($rollbackDir);
  293. return $finder;
  294. }
  295. }