Skip to content

Commit 0924334

Browse files
committed
Communicate an error to the gpg-agent instead of exiting
The error still gets logged by the logger but we communicate the error back to the gpg-agent.
1 parent e51a8d4 commit 0924334

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

main.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,22 @@ func passwordPrompt(s pinentry.Settings) ([]byte, error) {
188188
return p.GetPin()
189189
}
190190

191+
func assuanError(err error) *common.Error {
192+
return &common.Error{
193+
Src: common.ErrSrcPinentry,
194+
SrcName: "pinentry",
195+
Code: common.ErrCanceled,
196+
Message: err.Error(),
197+
}
198+
}
199+
191200
// GetPIN executes the main logic for returning a password/pin back to the gpg-agent
192201
func (c KeychainClient) GetPIN(s pinentry.Settings) (string, *common.Error) {
193-
return GetPIN(c.authFn, c.promptFn, c.logger)(s)
202+
if len(s.Error) == 0 && len(s.RepeatPrompt) == 0 && s.Opts.AllowExtPasswdCache && len(s.KeyInfo) != 0 {
203+
return GetPIN(c.authFn, c.promptFn, c.logger)(s)
204+
}
205+
206+
return "", nil
194207
}
195208

196209
// Confirm Asks for confirmation, not implemented.
@@ -217,13 +230,15 @@ func GetPIN(authFn AuthFunc, promptFn PromptFunc, logger *log.Logger) GetPinFunc
217230
matches = keyIDRegex.FindStringSubmatch(s.Desc)
218231
keyID := matches[1]
219232
if len(keyID) != 8 && len(keyID) != 16 {
220-
logger.Fatalf("Invalid keyID: %s", keyID)
233+
logger.Printf("Invalid keyID: %s", keyID)
234+
return "", assuanError(fmt.Errorf("invalid keyID: %s", keyID))
221235
}
222236

223237
keychainLabel := fmt.Sprintf("%s <%s> (%s)", name, email, keyID)
224238
exists, err := checkEntryInKeychain(keychainLabel)
225239
if err != nil {
226-
logger.Fatalf("error checking entry in keychain: %s", err)
240+
logger.Printf("error checking entry in keychain: %s", err)
241+
return "", assuanError(err)
227242
}
228243

229244
// If the entry is not found in the keychain, we trigger `pinentry-mac` with the option
@@ -242,7 +257,8 @@ func GetPIN(authFn AuthFunc, promptFn PromptFunc, logger *log.Logger) GetPinFunc
242257
}
243258

244259
if len(pin) == 0 {
245-
logger.Fatalf("pinentry-mac didn't return a password")
260+
logger.Printf("pinentry-mac didn't return a password")
261+
return "", assuanError(fmt.Errorf("pinentry-mac didn't return a password"))
246262
}
247263

248264
// s.KeyInfo is always in the form of x/cacheId
@@ -256,7 +272,8 @@ func GetPIN(authFn AuthFunc, promptFn PromptFunc, logger *log.Logger) GetPinFunc
256272
// guarded by Touch ID.
257273
exists, err = checkEntryInKeychain(keychainLabel)
258274
if err != nil {
259-
logger.Fatalf("error checking entry in keychain: %s", err)
275+
logger.Printf("error checking entry in keychain: %s", err)
276+
return "", assuanError(err)
260277
}
261278

262279
if !exists {
@@ -265,7 +282,8 @@ func GetPIN(authFn AuthFunc, promptFn PromptFunc, logger *log.Logger) GetPinFunc
265282
err = storePasswordInKeychain(keychainLabel, keyInfo, pin)
266283

267284
if err == keychain.ErrorDuplicateItem {
268-
logger.Fatalf("Duplicated entry in the keychain")
285+
logger.Printf("Duplicated entry in the keychain")
286+
return "", assuanError(err)
269287
}
270288
} else {
271289
logger.Printf("The keychain entry was created by pinentry-mac. Permission will be required on next run.")
@@ -276,9 +294,8 @@ func GetPIN(authFn AuthFunc, promptFn PromptFunc, logger *log.Logger) GetPinFunc
276294

277295
var ok bool
278296
if ok, err = authFn(fmt.Sprintf("access the PIN for %s", keychainLabel)); err != nil {
279-
logger.Fatalf("Error authenticating with Touch ID: %s", err)
280-
281-
return "", nil
297+
logger.Printf("Error authenticating with Touch ID: %s", err)
298+
return "", assuanError(err)
282299
}
283300

284301
if !ok {

0 commit comments

Comments
 (0)