From 51fedab4fcbbd771309b4421b0a9fd9a4ed6285c Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Sat, 22 Jan 2022 09:21:21 +0100 Subject: [PATCH] Better solution for gpg 'socket name' error Improment for #89 --- .../app/libraries/RainLoop/Actions/Pgp.php | 34 +++++++++++++++---- .../app/libraries/snappymail/pgp/gnupg.php | 6 ++-- .../app/libraries/snappymail/pgp/gpg.php | 3 +- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Pgp.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Pgp.php index d6a259b811..4ffbc5679e 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Pgp.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Pgp.php @@ -14,14 +14,36 @@ public function GnuPG() : ?\SnappyMail\PGP\GnuPG return null; } - $home = ($_SERVER['HOME'] ?: \exec('echo ~')) . '/.gnupg/'; - if ($oAccount instanceof \RainLoop\Model\AdditionalAccount) { - $home .= \sha1($oAccount->ParentEmail()); - } else { - $home .= \sha1($oAccount->Email()); + $homedir = \dirname($this->StorageProvider()->GenerateFilePath( + $oAccount, + \RainLoop\Providers\Storage\Enumerations\StorageType::PGP + )) . '/.gnupg'; + + /** + * Workaround error: socket name for '/very/long/path/to/.gnupg/S.gpg-agent.extra' is too long + * BSD 4.4 max length = 104 + */ + if (80 < \strlen($homedir)) { + // First try a symbolic link + $link = \sys_get_temp_dir() . '/snappymail'; + if (\is_dir($link) || \mkdir($link, 0700, true)) { + $link = $tmpdir . '/' . \md5($homedir); + if (\is_link($homedir) || \symlink($homedir, $link)) { + $homedir = $link; + } + } + // Else try ~/.gnupg/ + hash(email address) + if (80 < \strlen($homedir)) { + $homedir = ($_SERVER['HOME'] ?: \exec('echo ~')) . '/.gnupg/'; + if ($oAccount instanceof \RainLoop\Model\AdditionalAccount) { + $homedir .= \sha1($oAccount->ParentEmail()); + } else { + $homedir .= \sha1($oAccount->Email()); + } + } } - return \SnappyMail\PGP\GnuPG::getInstance($home); + return \SnappyMail\PGP\GnuPG::getInstance($homedir); } public function DoGnupgGetKeys() : array diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/pgp/gnupg.php b/snappymail/v/0.0.0/app/libraries/snappymail/pgp/gnupg.php index 017a68415b..11d9ba5853 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/pgp/gnupg.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/pgp/gnupg.php @@ -14,13 +14,15 @@ class GnuPG public static function isSupported() : bool { return \class_exists('gnupg') - || \stream_resolve_include_path('Crypt/GPG.php'); + || \stream_resolve_include_path('Crypt/GPG.php') + || \SnappyMail\PGP\GPG::isSupported(); } public static function getInstance(string $homedir) : ?self { $homedir = \rtrim($homedir, '/\\'); - if (107 <= \strlen($homedir . '/S.gpg-agent.extra')) { + // BSD 4.4 max length + if (104 <= \strlen($homedir . '/S.gpg-agent.extra')) { throw new \Exception('socket name for S.gpg-agent.extra is too long'); } diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/pgp/gpg.php b/snappymail/v/0.0.0/app/libraries/snappymail/pgp/gpg.php index c7cbb3e799..8ee3f8040d 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/pgp/gpg.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/pgp/gpg.php @@ -88,7 +88,8 @@ class GPG function __construct(string $homedir) { $homedir = \rtrim($homedir, '/\\'); - if (107 <= \strlen($homedir . '/S.gpg-agent.extra')) { + // BSD 4.4 max length + if (104 <= \strlen($homedir . '/S.gpg-agent.extra')) { throw new \Exception("socket name for '{$homedir}/S.gpg-agent.extra' is too long"); }