|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Proxy an IO channel to an underlying command, with optional callbacks. This |
| 5 | + * is a mostly a more general version of @{class:PhutilExecPassthru}. This |
| 6 | + * class is used to proxy Git, SVN and Mercurial traffic to the commands which |
| 7 | + * can actually serve it. |
| 8 | + * |
| 9 | + * Largely, this just reads an IO channel (like stdin from SSH) and writes |
| 10 | + * the results into a command channel (like a command's stdin). Then it reads |
| 11 | + * the command channel (like the command's stdout) and writes it into the IO |
| 12 | + * channel (like stdout from SSH): |
| 13 | + * |
| 14 | + * IO Channel Command Channel |
| 15 | + * stdin -> stdin |
| 16 | + * stdout <- stdout |
| 17 | + * stderr <- stderr |
| 18 | + * |
| 19 | + * You can provide **read and write callbacks** which are invoked as data |
| 20 | + * is passed through this class. They allow you to inspect and modify traffic. |
| 21 | + * |
| 22 | + * IO Channel Passthru Command Channel |
| 23 | + * stdout -> willWrite -> stdin |
| 24 | + * stdin <- willRead <- stdout |
| 25 | + * stderr <- (identity) <- stderr |
| 26 | + * |
| 27 | + * Primarily, this means: |
| 28 | + * |
| 29 | + * - the **IO Channel** can be a @{class:PhutilProtocolChannel} if the |
| 30 | + * **write callback** can convert protocol messages into strings; and |
| 31 | + * - the **write callback** can inspect and reject requests over the channel, |
| 32 | + * e.g. to enforce policies. |
| 33 | + * |
| 34 | + * In practice, this is used when serving repositories to check each command |
| 35 | + * issued over SSH and determine if it is a read command or a write command. |
| 36 | + * Writes can then be checked for appropriate permissions. |
| 37 | + */ |
| 38 | +final class PhabricatorSSHPassthruCommand extends Phobject { |
| 39 | + |
| 40 | + private $commandChannel; |
| 41 | + private $ioChannel; |
| 42 | + private $errorChannel; |
| 43 | + private $execFuture; |
| 44 | + private $willWriteCallback; |
| 45 | + private $willReadCallback; |
| 46 | + |
| 47 | + public function setCommandChannelFromExecFuture(ExecFuture $exec_future) { |
| 48 | + $exec_channel = new PhutilExecChannel($exec_future); |
| 49 | + $exec_channel->setStderrHandler(array($this, 'writeErrorIOCallback')); |
| 50 | + |
| 51 | + $this->execFuture = $exec_future; |
| 52 | + $this->commandChannel = $exec_channel; |
| 53 | + |
| 54 | + return $this; |
| 55 | + } |
| 56 | + |
| 57 | + public function setIOChannel(PhutilChannel $io_channel) { |
| 58 | + $this->ioChannel = $io_channel; |
| 59 | + return $this; |
| 60 | + } |
| 61 | + |
| 62 | + public function setErrorChannel(PhutilChannel $error_channel) { |
| 63 | + $this->errorChannel = $error_channel; |
| 64 | + return $this; |
| 65 | + } |
| 66 | + |
| 67 | + public function setWillReadCallback($will_read_callback) { |
| 68 | + $this->willReadCallback = $will_read_callback; |
| 69 | + return $this; |
| 70 | + } |
| 71 | + |
| 72 | + public function setWillWriteCallback($will_write_callback) { |
| 73 | + $this->willWriteCallback = $will_write_callback; |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + public function writeErrorIOCallback(PhutilChannel $channel, $data) { |
| 78 | + $this->errorChannel->write($data); |
| 79 | + } |
| 80 | + |
| 81 | + public function execute() { |
| 82 | + $command_channel = $this->commandChannel; |
| 83 | + $io_channel = $this->ioChannel; |
| 84 | + $error_channel = $this->errorChannel; |
| 85 | + |
| 86 | + if (!$command_channel) { |
| 87 | + throw new Exception("Set a command channel before calling execute()!"); |
| 88 | + } |
| 89 | + |
| 90 | + if (!$io_channel) { |
| 91 | + throw new Exception("Set an IO channel before calling execute()!"); |
| 92 | + } |
| 93 | + |
| 94 | + if (!$error_channel) { |
| 95 | + throw new Exception("Set an error channel before calling execute()!"); |
| 96 | + } |
| 97 | + |
| 98 | + $channels = array($command_channel, $io_channel, $error_channel); |
| 99 | + |
| 100 | + while (true) { |
| 101 | + PhutilChannel::waitForAny($channels); |
| 102 | + |
| 103 | + $io_channel->update(); |
| 104 | + $command_channel->update(); |
| 105 | + $error_channel->update(); |
| 106 | + |
| 107 | + $done = !$command_channel->isOpen(); |
| 108 | + |
| 109 | + $in_message = $io_channel->read(); |
| 110 | + $in_message = $this->willWriteData($in_message); |
| 111 | + if ($in_message !== null) { |
| 112 | + $command_channel->write($in_message); |
| 113 | + } |
| 114 | + |
| 115 | + $out_message = $command_channel->read(); |
| 116 | + $out_message = $this->willReadData($out_message); |
| 117 | + if ($out_message !== null) { |
| 118 | + $io_channel->write($out_message); |
| 119 | + } |
| 120 | + |
| 121 | + // If we have nothing left on stdin, close stdin on the subprocess. |
| 122 | + if (!$io_channel->isOpenForReading()) { |
| 123 | + // TODO: This should probably be part of PhutilExecChannel? |
| 124 | + $this->execFuture->write(''); |
| 125 | + } |
| 126 | + |
| 127 | + if ($done) { |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + list($err) = $this->execFuture->resolve(); |
| 133 | + |
| 134 | + return $err; |
| 135 | + } |
| 136 | + |
| 137 | + public function willWriteData($message) { |
| 138 | + if ($this->willWriteCallback) { |
| 139 | + return call_user_func($this->willWriteCallback, $this, $message); |
| 140 | + } else { |
| 141 | + if (strlen($message)) { |
| 142 | + return $message; |
| 143 | + } else { |
| 144 | + return null; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public function willReadData($message) { |
| 150 | + if ($this->willReadCallback) { |
| 151 | + return call_user_func($this->willReadCallback, $this, $message); |
| 152 | + } else { |
| 153 | + if (strlen($message)) { |
| 154 | + return $message; |
| 155 | + } else { |
| 156 | + return null; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments