diff options
author | Robin Jarry <robin@jarry.cc> | 2022-12-06 12:00:14 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-12-06 17:52:43 +0100 |
commit | d25efff65aba373be44d1a514c35472bf52994e5 (patch) | |
tree | 3b1e6ea01cf85a7bdaf5795144d1cbce740b20bc | |
parent | c1784b624aea6c93e398a90a4274abe158e72b8a (diff) | |
download | aerc-d25efff65aba373be44d1a514c35472bf52994e5.tar.gz |
pgp-provider: set default value to auto
Change the default provider to gpg unless the internal keyring is
initialized and contains one key.
This should be more user friendly.
Link: https://lists.sr.ht/~rjarry/aerc-discuss/%3CCO783CI3IU9F.184DBQTPMIPBS%40paul%3E
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Moritz Poldrack <moritz@poldrack.dev>
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | config/aerc.conf | 9 | ||||
-rw-r--r-- | config/general.go | 6 | ||||
-rw-r--r-- | doc/aerc-config.5.scd | 8 | ||||
-rw-r--r-- | lib/crypto/crypto.go | 13 | ||||
-rw-r--r-- | lib/crypto/pgp/pgp.go | 15 |
6 files changed, 43 insertions, 13 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e29ea5c..555f8c0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - LIST-STATUS support for imap - built-in `wrap` filter that does not mess up nested quotes and lists. +### Changed + +- `pgp-provider` now defaults to `auto`. It will use the system `gpg` unless + the internal keyring exists and contains at least one key. + ### Fixed - `:pipe -m git am -3` on patch series when `Message-Id` headers have not been diff --git a/config/aerc.conf b/config/aerc.conf index 862b3be8..8f3fd097 100644 --- a/config/aerc.conf +++ b/config/aerc.conf @@ -8,12 +8,13 @@ # #default-save-path= -# # If set to "gpg", aerc will use system gpg binary and keystore for all crypto -# operations. Otherwise, the internal openpgp implementation will be used. +# operations. If set to "internal", the internal openpgp keyring will be used. +# If set to "auto", the system gpg will be preferred unless the internal +# keyring already exists, in which case the latter will be used. # -# Default: internal -#pgp-provider=internal +# Default: auto +#pgp-provider=auto # By default, the file permissions of accounts.conf must be restrictive and # only allow reading by the file owner (0600). Set this option to true to diff --git a/config/general.go b/config/general.go index 8ca22471..b06eddde 100644 --- a/config/general.go +++ b/config/general.go @@ -20,7 +20,7 @@ type GeneralConfig struct { func defaultGeneralConfig() GeneralConfig { return GeneralConfig{ - PgpProvider: "internal", + PgpProvider: "auto", UnsafeAccountsConf: false, LogLevel: log.INFO, } @@ -71,9 +71,9 @@ end: func (gen *GeneralConfig) validatePgpProvider() error { switch gen.PgpProvider { - case "gpg", "internal": + case "gpg", "internal", "auto": return nil default: - return fmt.Errorf("pgp-provider must be either gpg or internal") + return fmt.Errorf("pgp-provider must be either auto, gpg or internal") } } diff --git a/doc/aerc-config.5.scd b/doc/aerc-config.5.scd index c95e5aab..e7000b4c 100644 --- a/doc/aerc-config.5.scd +++ b/doc/aerc-config.5.scd @@ -31,12 +31,14 @@ These options are configured in the *[general]* section of _aerc.conf_. *default-save-path* = _<path>_ Used as a default path for save operations if no other path is specified. -*pgp-provider* = _gpg_|_internal_ +*pgp-provider* = _auto_|_gpg_|_internal_ If set to _gpg_, aerc will use system gpg binary and keystore for all crypto operations. If set to _internal_, the internal openpgp keyring - will be used. + will be used. If set to _auto_, the system gpg will be preferred unless + the internal keyring already exists, in which case the latter will be + used. - Default: _internal_ + Default: _auto_ *unsafe-accounts-conf* = _true_|_false_ By default, the file permissions of _accounts.conf_ must be restrictive diff --git a/lib/crypto/crypto.go b/lib/crypto/crypto.go index b7afe638..cb026696 100644 --- a/lib/crypto/crypto.go +++ b/lib/crypto/crypto.go @@ -6,6 +6,7 @@ import ( "git.sr.ht/~rjarry/aerc/lib/crypto/gpg" "git.sr.ht/~rjarry/aerc/lib/crypto/pgp" + "git.sr.ht/~rjarry/aerc/log" "git.sr.ht/~rjarry/aerc/models" "github.com/ProtonMail/go-crypto/openpgp" "github.com/emersion/go-message/mail" @@ -25,10 +26,20 @@ type Provider interface { func New(s string) Provider { switch s { + case "auto": + internal := &pgp.Mail{} + if internal.KeyringExists() { + log.Debugf("internal pgp keyring exists") + return internal + } + log.Debugf("no internal pgp keyring, using system gpg") + fallthrough case "gpg": return &gpg.Mail{} - default: + case "internal": return &pgp.Mail{} + default: + return nil } } diff --git a/lib/crypto/pgp/pgp.go b/lib/crypto/pgp/pgp.go index b2f5fa24..8d7c135a 100644 --- a/lib/crypto/pgp/pgp.go +++ b/lib/crypto/pgp/pgp.go @@ -28,6 +28,17 @@ var ( locked bool ) +func (m *Mail) KeyringExists() bool { + keypath := path.Join(xdg.DataHome(), "aerc", "keyring.asc") + keyfile, err := os.Open(keypath) + if err != nil { + return false + } + defer keyfile.Close() + _, err = openpgp.ReadKeyRing(keyfile) + return err == nil +} + func (m *Mail) Init() error { log.Debugf("Initializing PGP keyring") err := os.MkdirAll(path.Join(xdg.DataHome(), "aerc"), 0o700) @@ -50,13 +61,13 @@ func (m *Mail) Init() error { if os.IsNotExist(err) { return nil } else if err != nil { - panic(err) + return err } defer keyfile.Close() Keyring, err = openpgp.ReadKeyRing(keyfile) if err != nil { - panic(err) + return err } return nil } |