Skip to content

Commit e788989

Browse files
author
epriestley
committedDec 19, 2012
Implement SSHD glue and Conduit SSH endpoint
Summary: - Build "sshd-auth" (for authentication) and "sshd-exec" (for command execution) binaries. These are callable by "sshd-vcs", located [[https://github.com/epriestley/sshd-vcs | in my account on GitHub]]. They are based on precursors [[https://github.com/epriestley/sshd-vcs-glue | here on GitHub]] which I deployed for TenXer about a year ago, so I have some confidence they at least basically work. - The problem this solves is that normally every user would need an account on a machine to connect to it, and/or their public keys would all need to be listed in `~/.authorized_keys`. This is a big pain in most installs. Software like Gitosis/Gitolite solve this problem by giving you an easy way to add public keys to `~/.authorized_keys`, but this is pretty gross. - Roughly, instead of looking in `~/.authorized_keys` when a user connects, the patched sshd instead runs `echo <public key> | sshd-auth`. The `sshd-auth` script looks up the public key and authorizes the matching user, if they exist. It also forces sshd to run `sshd-exec` instead of a normal shell. - `sshd-exec` receives the authenticated user and any command which was passed to ssh (like `git receive-pack`) and can route them appropriately. - Overall, this permits a single account to be set up on a server which all Phabricator users can connect to without any extra work, and which can safely execute commands and apply appropriate permissions, and disable users when they are disabled in Phabricator and all that stuff. - Build out "sshd-exec" to do more thorough checks and setup, and delegate command execution to Workflows (they now exist, and did not when I originally built this stuff). - Convert @btrahan's conduit API script into a workflow and slightly simplify it (ConduitCall did not exist at the time it was written). The next steps here on the Repository side are to implement Workflows for Git, SVN and HG wire protocols. These will mostly just proxy the protocols, but also need to enforce permissions. So the approach will basically be: - Implement workflows for stuff like `git receive-pack`. - These workflows will implement enough of the underlying protocol to determine what resource the user is trying to access, and whether they want to read or write it. - They'll then do a permissons check, and kick the user out if they don't have permission to do whatever they are trying to do. - If the user does have permission, we just proxy the rest of the transaction. Next steps on the Conduit side are more simple: - Make ConduitClient understand "ssh://" URLs. Test Plan: Ran `sshd-exec --phabricator-ssh-user epriestley conduit differential.query`, etc. This will get a more comprehensive test once I set up sshd-vcs. Reviewers: btrahan, vrana Reviewed By: btrahan CC: aran Maniphest Tasks: T603, T550 Differential Revision: https://secure.phabricator.com/D4229
1 parent db89e23 commit e788989

File tree

8 files changed

+269
-79
lines changed

8 files changed

+269
-79
lines changed
 

‎bin/ssh-auth

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../scripts/ssh/ssh-auth.php

‎bin/ssh-exec

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../scripts/ssh/ssh-exec.php

‎scripts/conduit/api.php

-79
This file was deleted.

‎scripts/ssh/ssh-auth.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
$root = dirname(dirname(dirname(__FILE__)));
5+
require_once $root.'/scripts/__init_script__.php';
6+
7+
$cert = file_get_contents('php://stdin');
8+
9+
$user = null;
10+
if ($cert) {
11+
$user_dao = new PhabricatorUser();
12+
$ssh_dao = new PhabricatorUserSSHKey();
13+
$conn = $user_dao->establishConnection('r');
14+
15+
list($type, $body) = array_merge(
16+
explode(' ', $cert),
17+
array('', ''));
18+
19+
$row = queryfx_one(
20+
$conn,
21+
'SELECT userName FROM %T u JOIN %T ssh ON u.phid = ssh.userPHID
22+
WHERE ssh.keyBody = %s AND ssh.keyType = %s',
23+
$user_dao->getTableName(),
24+
$ssh_dao->getTableName(),
25+
$body,
26+
$type);
27+
if ($row) {
28+
$user = idx($row, 'userName');
29+
}
30+
}
31+
32+
if (!$user) {
33+
exit(1);
34+
}
35+
36+
if (!PhabricatorUser::validateUsername($user)) {
37+
exit(1);
38+
}
39+
40+
$bin = $root.'/bin/ssh-exec';
41+
$cmd = csprintf('%s --phabricator-ssh-user %s', $bin, $user);
42+
// This is additional escaping for the SSH 'command="..."' string.
43+
$cmd = str_replace('"', '\\"', $cmd);
44+
45+
$options = array(
46+
'command="'.$cmd.'"',
47+
'no-port-forwarding',
48+
'no-X11-forwarding',
49+
'no-agent-forwarding',
50+
'no-pty',
51+
);
52+
53+
echo implode(',', $options);
54+
exit(0);

‎scripts/ssh/ssh-exec.php

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
$root = dirname(dirname(dirname(__FILE__)));
5+
require_once $root.'/scripts/__init_script__.php';
6+
7+
$args = new PhutilArgumentParser($argv);
8+
$args->setTagline('receive SSH requests');
9+
$args->setSynopsis(<<<EOSYNOPSIS
10+
**ssh-exec** --phabricator-ssh-user __user__ __commmand__ [__options__]
11+
Receive SSH requests.
12+
13+
EOSYNOPSIS
14+
);
15+
16+
// NOTE: Do NOT parse standard arguments. Arguments are coming from a remote
17+
// client over SSH, and they should not be able to execute "--xprofile",
18+
// "--recon", etc.
19+
20+
$args->parsePartial(
21+
array(
22+
array(
23+
'name' => 'phabricator-ssh-user',
24+
'param' => 'username',
25+
),
26+
));
27+
28+
try {
29+
$user_name = $args->getArg('phabricator-ssh-user');
30+
if (!strlen($user_name)) {
31+
throw new Exception("No username.");
32+
}
33+
34+
$user = id(new PhabricatorUser())->loadOneWhere(
35+
'userName = %s',
36+
$user_name);
37+
if (!$user) {
38+
throw new Exception("Invalid username.");
39+
}
40+
41+
if ($user->getIsDisabled()) {
42+
throw new Exception("You have been exiled.");
43+
}
44+
45+
$workflows = array(
46+
new ConduitSSHWorkflow(),
47+
);
48+
49+
// This duplicates logic in parseWorkflows(), but allows us to raise more
50+
// concise/relevant exceptions when the client is a remote SSH.
51+
$remain = $args->getUnconsumedArgumentVector();
52+
if (empty($remain)) {
53+
throw new Exception("No command.");
54+
} else {
55+
$command = head($remain);
56+
$workflow_names = mpull($workflows, 'getName', 'getName');
57+
if (empty($workflow_names[$command])) {
58+
throw new Exception("Invalid command.");
59+
}
60+
}
61+
62+
$workflow = $args->parseWorkflows($workflows);
63+
$workflow->setUser($user);
64+
65+
$sock_stdin = fopen('php://stdin', 'r');
66+
if (!$sock_stdin) {
67+
throw new Exception("Unable to open stdin.");
68+
}
69+
70+
$sock_stdout = fopen('php://stdout', 'w');
71+
if (!$sock_stdout) {
72+
throw new Exception("Unable to open stdout.");
73+
}
74+
75+
$socket_channel = new PhutilSocketChannel(
76+
$sock_stdin,
77+
$sock_stdout);
78+
$metrics_channel = new PhutilMetricsChannel($socket_channel);
79+
$workflow->setIOChannel($metrics_channel);
80+
81+
$err = $workflow->execute($args);
82+
83+
$metrics_channel->flush();
84+
} catch (Exception $ex) {
85+
echo "phabricator-ssh-exec: ".$ex->getMessage()."\n";
86+
exit(1);
87+
}

‎src/__phutil_library_map__.php

+4
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@
193193
'ConduitCall' => 'applications/conduit/call/ConduitCall.php',
194194
'ConduitCallTestCase' => 'applications/conduit/call/__tests__/ConduitCallTestCase.php',
195195
'ConduitException' => 'applications/conduit/protocol/ConduitException.php',
196+
'ConduitSSHWorkflow' => 'applications/conduit/ssh/ConduitSSHWorkflow.php',
196197
'DarkConsoleConfigPlugin' => 'aphront/console/plugin/DarkConsoleConfigPlugin.php',
197198
'DarkConsoleController' => 'aphront/console/DarkConsoleController.php',
198199
'DarkConsoleCore' => 'aphront/console/DarkConsoleCore.php',
@@ -1074,6 +1075,7 @@
10741075
'PhabricatorRequestOverseer' => 'infrastructure/PhabricatorRequestOverseer.php',
10751076
'PhabricatorS3FileStorageEngine' => 'applications/files/engine/PhabricatorS3FileStorageEngine.php',
10761077
'PhabricatorSQLPatchList' => 'infrastructure/storage/patch/PhabricatorSQLPatchList.php',
1078+
'PhabricatorSSHWorkflow' => 'infrastructure/ssh/PhabricatorSSHWorkflow.php',
10771079
'PhabricatorScopedEnv' => 'infrastructure/PhabricatorScopedEnv.php',
10781080
'PhabricatorSearchAbstractDocument' => 'applications/search/index/PhabricatorSearchAbstractDocument.php',
10791081
'PhabricatorSearchAttachController' => 'applications/search/controller/PhabricatorSearchAttachController.php',
@@ -1521,6 +1523,7 @@
15211523
'ConduitAPI_user_whoami_Method' => 'ConduitAPI_user_Method',
15221524
'ConduitCallTestCase' => 'PhabricatorTestCase',
15231525
'ConduitException' => 'Exception',
1526+
'ConduitSSHWorkflow' => 'PhabricatorSSHWorkflow',
15241527
'DarkConsoleConfigPlugin' => 'DarkConsolePlugin',
15251528
'DarkConsoleController' => 'PhabricatorController',
15261529
'DarkConsoleErrorLogPlugin' => 'DarkConsolePlugin',
@@ -2337,6 +2340,7 @@
23372340
'PhabricatorRepositorySymbol' => 'PhabricatorRepositoryDAO',
23382341
'PhabricatorRepositoryTestCase' => 'PhabricatorTestCase',
23392342
'PhabricatorS3FileStorageEngine' => 'PhabricatorFileStorageEngine',
2343+
'PhabricatorSSHWorkflow' => 'PhutilArgumentWorkflow',
23402344
'PhabricatorSearchAttachController' => 'PhabricatorSearchBaseController',
23412345
'PhabricatorSearchBaseController' => 'PhabricatorController',
23422346
'PhabricatorSearchCommitIndexer' => 'PhabricatorSearchDocumentIndexer',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
final class ConduitSSHWorkflow extends PhabricatorSSHWorkflow {
4+
5+
public function didConstruct() {
6+
$this->setName('conduit');
7+
$this->setArguments(
8+
array(
9+
array(
10+
'name' => 'method',
11+
'wildcard' => true,
12+
),
13+
));
14+
}
15+
16+
public function execute(PhutilArgumentParser $args) {
17+
$time_start = microtime(true);
18+
19+
$methodv = $args->getArg('method');
20+
if (!$methodv) {
21+
throw new Exception("No Conduit method provided.");
22+
} else if (count($methodv) > 1) {
23+
throw new Exception("Too many Conduit methods provided.");
24+
}
25+
26+
$method = head($methodv);
27+
28+
$json = $this->readAllInput();
29+
$raw_params = json_decode($json, true);
30+
if (!is_array($raw_params)) {
31+
throw new Exception("Invalid JSON input.");
32+
}
33+
34+
$params = $raw_params;
35+
unset($params['__conduit__']);
36+
$metadata = idx($raw_params, '__conduit__', array());
37+
38+
$call = null;
39+
$error_code = null;
40+
$error_info = null;
41+
42+
try {
43+
$call = new ConduitCall($method, $params);
44+
$call->setUser($this->getUser());
45+
46+
$result = $call->execute();
47+
} catch (ConduitException $ex) {
48+
$result = null;
49+
$error_code = $ex->getMessage();
50+
if ($ex->getErrorDescription()) {
51+
$error_info = $ex->getErrorDescription();
52+
} else if ($call) {
53+
$error_info = $call->getErrorDescription($error_code);
54+
}
55+
}
56+
57+
$response = id(new ConduitAPIResponse())
58+
->setResult($result)
59+
->setErrorCode($error_code)
60+
->setErrorInfo($error_info);
61+
62+
$json_out = json_encode($response->toDictionary());
63+
$json_out = $json_out."\n";
64+
65+
$this->getIOChannel()->write($json_out);
66+
67+
// NOTE: Flush here so we can get an accurate result for the duration,
68+
// if the response is large and the receiver is slow to read it.
69+
$this->getIOChannel()->flush();
70+
71+
$time_end = microtime(true);
72+
73+
$connection_id = idx($metadata, 'connectionID');
74+
$log = new PhabricatorConduitMethodCallLog();
75+
$log->setConnectionID($connection_id);
76+
$log->setMethod($method);
77+
$log->setError((string)$error_code);
78+
$log->setDuration(1000000 * ($time_end - $time_start));
79+
$log->save();
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
abstract class PhabricatorSSHWorkflow extends PhutilArgumentWorkflow {
4+
5+
private $user;
6+
private $iochannel;
7+
8+
public function setUser(PhabricatorUser $user) {
9+
$this->user = $user;
10+
return $this;
11+
}
12+
13+
public function getUser() {
14+
return $this->user;
15+
}
16+
17+
final public function isExecutable() {
18+
return false;
19+
}
20+
21+
public function setIOChannel(PhutilChannel $channel) {
22+
$this->iochannel = $channel;
23+
return $this;
24+
}
25+
26+
public function getIOChannel() {
27+
return $this->iochannel;
28+
}
29+
30+
public function readAllInput() {
31+
$channel = $this->getIOChannel();
32+
while ($channel->update()) {
33+
PhutilChannel::waitForAny(array($channel));
34+
if (!$channel->isOpenForReading()) {
35+
break;
36+
}
37+
}
38+
return $channel->read();
39+
}
40+
41+
}

0 commit comments

Comments
 (0)
Failed to load comments.