Skip to content

Commit

Permalink
Better solution for gpg 'socket name' error
Browse files Browse the repository at this point in the history
Improment for #89
  • Loading branch information
the-djmaze committed Jan 22, 2022
1 parent 6df7b76 commit 51fedab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
34 changes: 28 additions & 6 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Pgp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions snappymail/v/0.0.0/app/libraries/snappymail/pgp/gnupg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
3 changes: 2 additions & 1 deletion snappymail/v/0.0.0/app/libraries/snappymail/pgp/gpg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down

0 comments on commit 51fedab

Please sign in to comment.