4 $root = dirname(dirname(dirname(__FILE__
)));
5 require_once $root.'/scripts/init/init-script.php';
7 $error_log = id(new PhutilErrorLog())
8 ->setLogName(pht('SSH Error Log'))
9 ->setLogPath(PhabricatorEnv
::getEnvConfig('log.ssh-error.path'))
12 // TODO: For now, this is using "parseParital()", not "parse()". This allows
13 // the script to accept (and ignore) additional arguments. This preserves
14 // backward compatibility until installs have time to migrate to the new
17 $args = id(new PhutilArgumentParser($argv))
24 'Accepts the "%%k" parameter from "AuthorizedKeysCommand".'),
28 $sshd_key = $args->getArg('sshd-key');
30 // NOTE: We are caching a datastructure rather than the flat key file because
31 // the path on disk to "ssh-exec" is arbitrarily mutable at runtime. See T12397.
33 $cache = PhabricatorCaches
::getMutableCache();
34 $authstruct_key = PhabricatorAuthSSHKeyQuery
::AUTHSTRUCT_CACHEKEY
;
35 $authstruct_raw = $cache->getKey($authstruct_key);
39 if ($authstruct_raw !== null && strlen($authstruct_raw)) {
41 $authstruct = phutil_json_decode($authstruct_raw);
42 } catch (Exception
$ex) {
43 // Ignore any issues with the cached data; we'll just rebuild the
48 if ($authstruct === null) {
49 $keys = id(new PhabricatorAuthSSHKeyQuery())
50 ->setViewer(PhabricatorUser
::getOmnipotentUser())
55 echo pht('No keys found.')."\n";
60 foreach ($keys as $ssh_key) {
62 $object = $ssh_key->getObject();
63 if ($object instanceof PhabricatorUser
) {
64 $key_argv[] = '--phabricator-ssh-user';
65 $key_argv[] = $object->getUsername();
66 } else if ($object instanceof AlmanacDevice
) {
67 if (!$ssh_key->getIsTrusted()) {
68 // If this key is not a trusted device key, don't allow SSH
72 $key_argv[] = '--phabricator-ssh-device';
73 $key_argv[] = $object->getName();
75 // We don't know what sort of key this is; don't permit SSH auth.
79 $key_argv[] = '--phabricator-ssh-key';
80 $key_argv[] = $ssh_key->getID();
82 // Strip out newlines and other nonsense from the key type and key body.
83 $type = $ssh_key->getKeyType();
84 $type = preg_replace('@[\x00-\x20]+@', '', $type);
89 $key = $ssh_key->getKeyBody();
90 $key = preg_replace('@[\x00-\x20]+@', '', $key);
106 $authstruct_raw = phutil_json_encode($authstruct);
107 $ttl = phutil_units('24 hours in seconds');
108 $cache->setKey($authstruct_key, $authstruct_raw, $ttl);
111 // If we've received an "--sshd-key" argument and it matches some known key,
112 // only emit that key. (For now, if the key doesn't match, we'll fall back to
113 // emitting all keys.)
114 if ($sshd_key !== null) {
116 foreach ($authstruct['keys'] as $key => $key_struct) {
117 if ($key_struct['key'] === $sshd_key) {
118 $matches[$key] = $key_struct;
123 $authstruct['keys'] = $matches;
127 $bin = $root.'/bin/ssh-exec';
128 $instance = PhabricatorEnv
::getEnvConfig('cluster.instance');
131 foreach ($authstruct['keys'] as $key_struct) {
132 $key_argv = $key_struct['argv'];
133 $key = $key_struct['key'];
134 $type = $key_struct['type'];
136 $cmd = csprintf('%s %Ls', $bin, $key_argv);
138 if ($instance !== null && strlen($instance)) {
139 $cmd = csprintf('PHABRICATOR_INSTANCE=%s %C', $instance, $cmd);
142 // This is additional escaping for the SSH 'command="..."' string.
143 $cmd = addcslashes($cmd, '"\\');
146 'command="'.$cmd.'"',
147 'no-port-forwarding',
149 'no-agent-forwarding',
152 $options = implode(',', $options);
154 $lines[] = $options.' '.$type.' '.$key."\n";
157 $authfile = implode('', $lines);