For our issue #89
This commit is contained in:
the-djmaze 2022-01-21 20:00:18 +01:00
parent cff662009f
commit 6df7b76c8a
3 changed files with 48 additions and 12 deletions

View file

@ -9,11 +9,19 @@ trait Pgp
*/ */
public function GnuPG() : ?\SnappyMail\PGP\GnuPG public function GnuPG() : ?\SnappyMail\PGP\GnuPG
{ {
$pgp_dir = \dirname($this->StorageProvider()->GenerateFilePath( $oAccount = $this->getAccountFromToken();
$this->getAccountFromToken(), if (!$oAccount) {
\RainLoop\Providers\Storage\Enumerations\StorageType::PGP return null;
)); }
return \SnappyMail\PGP\GnuPG::getInstance($pgp_dir);
$home = ($_SERVER['HOME'] ?: \exec('echo ~')) . '/.gnupg/';
if ($oAccount instanceof \RainLoop\Model\AdditionalAccount) {
$home .= \sha1($oAccount->ParentEmail());
} else {
$home .= \sha1($oAccount->Email());
}
return \SnappyMail\PGP\GnuPG::getInstance($home);
} }
public function DoGnupgGetKeys() : array public function DoGnupgGetKeys() : array

View file

@ -17,17 +17,22 @@ class GnuPG
|| \stream_resolve_include_path('Crypt/GPG.php'); || \stream_resolve_include_path('Crypt/GPG.php');
} }
public static function getInstance(string $base_dir) : ?self public static function getInstance(string $homedir) : ?self
{ {
$homedir = \rtrim($homedir, '/\\');
if (107 <= \strlen($homedir . '/S.gpg-agent.extra')) {
throw new \Exception('socket name for S.gpg-agent.extra is too long');
}
$self = null; $self = null;
$home = $base_dir . '/.gnupg'; // if (\version_compare(\phpversion('gnupg'), '1.5', '>=')) {
if (\class_exists('gnupg')) { if (\class_exists('gnupg')) {
$self = new self; $self = new self;
$self->GnuPG = new \gnupg([ $self->GnuPG = new \gnupg([
// It is the file name of the executable program implementing this protocol which is usually path of the gpg executable. // It is the file name of the executable program implementing this protocol which is usually path of the gpg executable.
// 'file_name' => '/usr/bin/gpg', // 'file_name' => '/usr/bin/gpg',
// It is the directory name of the configuration directory. It also overrides GNUPGHOME environment variable that is used for the same purpose. // It is the directory name of the configuration directory. It also overrides GNUPGHOME environment variable that is used for the same purpose.
'home_dir' => $home 'home_dir' => $homedir
]); ]);
// Output is ASCII // Output is ASCII
$self->GnuPG->setarmor(1); $self->GnuPG->setarmor(1);
@ -42,13 +47,13 @@ class GnuPG
$self->Crypt_GPG = new \Crypt_GPG([ $self->Crypt_GPG = new \Crypt_GPG([
// 'debug' => true, // 'debug' => true,
// 'binary' => $binary, // 'binary' => $binary,
'homedir' => $home 'homedir' => $homedir
]); ]);
} }
} }
if ($self) { if ($self) {
$self->homedir = $home; $self->homedir = $homedir;
// \putenv("GNUPGHOME={$home}"); // \putenv("GNUPGHOME={$homedir}");
} }
return $self; return $self;
} }
@ -367,6 +372,14 @@ class GnuPG
{ {
if ($this->GnuPG) { if ($this->GnuPG) {
return $this->GnuPG->keyinfo($pattern); return $this->GnuPG->keyinfo($pattern);
/* // v1.5 Slow and fails
return \array_merge(
// Public
$this->GnuPG->keyinfo($pattern),
// Private, read https://github.com/php-gnupg/php-gnupg/issues/5
$this->GnuPG->keyinfo($pattern, 1)
);
*/
} }
if ($this->Crypt_GPG) { if ($this->Crypt_GPG) {
return true; return true;

View file

@ -87,7 +87,16 @@ class GPG
function __construct(string $homedir) function __construct(string $homedir)
{ {
$this->options['homedir'] = \rtrim($homedir, '/'); $homedir = \rtrim($homedir, '/\\');
if (107 <= \strlen($homedir . '/S.gpg-agent.extra')) {
throw new \Exception("socket name for '{$homedir}/S.gpg-agent.extra' is too long");
}
if (!\is_dir($homedir)) {
\mkdir($homedir, 0700, true);
}
$this->options['homedir'] = $homedir;
// the random seed file makes subsequent actions faster so only disable it if we have to. // the random seed file makes subsequent actions faster so only disable it if we have to.
if ($this->options['homedir'] && !\is_writeable($this->options['homedir'])) { if ($this->options['homedir'] && !\is_writeable($this->options['homedir'])) {
@ -601,6 +610,12 @@ return [];
*/ */
} }
public function agent()
{
// $home = \escapeshellarg($this->options['homedir']);
// echo `gpg-agent --daemon --homedir $home 2>&1`;
}
private function exec(array $arguments) private function exec(array $arguments)
{ {
if (\version_compare($this->version, '2.2.5', '<')) { if (\version_compare($this->version, '2.2.5', '<')) {