Skip to content

Commit

Permalink
Added support for entering password via command line
Browse files Browse the repository at this point in the history
  • Loading branch information
thisispiers committed Mar 20, 2024
1 parent 5c024b3 commit b9b5e13
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions deployment.sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ remote = ftp://user:[email protected]/directory
; you can use ftps://, sftp:// or file:// protocols (sftp requires SSH2 extension)

; do not like to specify user & password in 'remote'? Use these options:
; do not like to store password in config file? specify password = STDIN
;user = ...
;password = ...

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,5 @@ Need SSH authenticate using a public key?
```
publicKey = '/key/id_rsa.pub'
privateKey = '/key/id_rsa'
passPhrase = 'yourpass' #optional - If needed passphrase for privateKey
passPhrase = 'yourpass' #optional - If needed passphrase for privateKey, or set to STDIN to enter via command line
```
6 changes: 5 additions & 1 deletion src/Deployment/FtpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ public function connect(): void
? Safe::ftp_connect($this->url['host'], $this->url['port'] ?? 21)
: Safe::ftp_ssl_connect($this->url['host'], $this->url['port'] ?? 21);

Safe::ftp_login($this->connection, urldecode($this->url['user']), urldecode($this->url['pass']));
$pass = $this->url['pass'];
if ($pass === 'STDIN') {
$pass = Helpers::getHiddenInput("Enter password for {$this->url['user']}: ");
}
Safe::ftp_login($this->connection, urldecode($this->url['user']), urldecode($pass));

if ($this->passiveMode) {
Safe::ftp_set_option($this->connection, FTP_USEPASVADDRESS, false);
Expand Down
13 changes: 13 additions & 0 deletions src/Deployment/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,17 @@ public static function buildUrl(array $url): string
. (isset($url['port']) ? ':' . $url['port'] : '')
. ($url['path'] ?? '');
}


public static function getHiddenInput(string $prompt = ''): string
{
if ($prompt) {
echo $prompt;
}
@exec('stty -echo 2>&1');
$password = stream_get_line(STDIN, 1024, PHP_EOL);
echo PHP_EOL;
@exec('stty echo 2>&1');
return $password;
}
}
6 changes: 5 additions & 1 deletion src/Deployment/PhpsecServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public function connect(): void
}
$sftp = new SFTP($this->url['host'], $this->url['port'] ?? 22);
if ($this->privateKey) {
$key = PublicKeyLoader::load(file_get_contents($this->privateKey), $this->passPhrase ?? false);
$passPhrase = $this->passPhrase ?? false;
if ($passPhrase === 'STDIN') {
$passPhrase = Helpers::getHiddenInput("Enter password for private key: ");
}
$key = PublicKeyLoader::load(file_get_contents($this->privateKey), $passPhrase);
if (!$sftp->login(urldecode($this->url['user']), $key)) {
throw new ServerException('Login failed with private key');
}
Expand Down
12 changes: 10 additions & 2 deletions src/Deployment/SshServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,17 @@ public function connect(): void
}
$this->connection = Safe::ssh2_connect($this->url['host'], $this->url['port'] ?? 22);
if (isset($this->url['pass'])) {
Safe::ssh2_auth_password($this->connection, urldecode($this->url['user']), urldecode($this->url['pass']));
$pass = $this->url['pass'];
if ($pass === 'STDIN') {
$pass = Helpers::getHiddenInput("Enter password for {$this->url['user']}: ");
}
Safe::ssh2_auth_password($this->connection, urldecode($this->url['user']), urldecode($pass));
} elseif ($this->publicKey && $this->privateKey) {
Safe::ssh2_auth_pubkey_file($this->connection, urldecode($this->url['user']), $this->publicKey, $this->privateKey, (string) $this->passPhrase);
$passPhrase = $this->passPhrase;
if ($passPhrase === 'STDIN') {
$passPhrase = Helpers::getHiddenInput("Enter password for private key: ");
}
Safe::ssh2_auth_pubkey_file($this->connection, urldecode($this->url['user']), $this->publicKey, $this->privateKey, (string) $passPhrase);
} else {
Safe::ssh2_auth_agent($this->connection, urldecode($this->url['user']));
}
Expand Down

0 comments on commit b9b5e13

Please sign in to comment.