|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +// This is a wrapper script for Git, Mercurial, and Subversion. It primarily |
| 5 | +// serves to inject "-o StrictHostKeyChecking=no" into the SSH arguments. |
| 6 | + |
| 7 | +$root = dirname(dirname(dirname(__FILE__))); |
| 8 | +require_once $root.'/scripts/__init_script__.php'; |
| 9 | + |
| 10 | +$target_name = getenv('PHABRICATOR_SSH_TARGET'); |
| 11 | +if (!$target_name) { |
| 12 | + throw new Exception(pht("No 'PHABRICATOR_SSH_TARGET' in environment!")); |
| 13 | +} |
| 14 | + |
| 15 | +$repository = id(new PhabricatorRepositoryQuery()) |
| 16 | + ->setViewer(PhabricatorUser::getOmnipotentUser()) |
| 17 | + ->withCallsigns(array($target_name)) |
| 18 | + ->executeOne(); |
| 19 | +if (!$repository) { |
| 20 | + throw new Exception(pht('No repository with callsign "%s"!', $target_name)); |
| 21 | +} |
| 22 | + |
| 23 | +$pattern = array(); |
| 24 | +$arguments = array(); |
| 25 | + |
| 26 | +$pattern[] = 'ssh'; |
| 27 | + |
| 28 | +$pattern[] = '-o'; |
| 29 | +$pattern[] = 'StrictHostKeyChecking=no'; |
| 30 | + |
| 31 | +$login = $repository->getSSHLogin(); |
| 32 | +if (strlen($login)) { |
| 33 | + $pattern[] = '-l'; |
| 34 | + $pattern[] = '%P'; |
| 35 | + $arguments[] = new PhutilOpaqueEnvelope($login); |
| 36 | +} |
| 37 | + |
| 38 | +$ssh_identity = null; |
| 39 | + |
| 40 | +$key = $repository->getDetail('ssh-key'); |
| 41 | +$keyfile = $repository->getDetail('ssh-keyfile'); |
| 42 | +if ($keyfile) { |
| 43 | + $ssh_identity = $keyfile; |
| 44 | +} else if ($key) { |
| 45 | + $tmpfile = new TempFile('phabricator-repository-ssh-key'); |
| 46 | + chmod($tmpfile, 0600); |
| 47 | + Filesystem::writeFile($tmpfile, $key); |
| 48 | + $ssh_identity = (string)$tmpfile; |
| 49 | +} |
| 50 | + |
| 51 | +if ($ssh_identity) { |
| 52 | + $pattern[] = '-i'; |
| 53 | + $pattern[] = '%P'; |
| 54 | + $arguments[] = new PhutilOpaqueEnvelope($keyfile); |
| 55 | +} |
| 56 | + |
| 57 | +$pattern[] = '--'; |
| 58 | + |
| 59 | +$passthru_args = array_slice($argv, 1); |
| 60 | +foreach ($passthru_args as $passthru_arg) { |
| 61 | + $pattern[] = '%s'; |
| 62 | + $arguments[] = $passthru_arg; |
| 63 | +} |
| 64 | + |
| 65 | +$pattern = implode(' ', $pattern); |
| 66 | +array_unshift($arguments, $pattern); |
| 67 | + |
| 68 | +$err = newv('PhutilExecPassthru', $arguments) |
| 69 | + ->execute(); |
| 70 | + |
| 71 | +exit($err); |
0 commit comments