From a5f5ad6470e625b72c71cac9477173307b005af2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Mon, 25 Sep 2023 13:44:45 +0200 Subject: transaction: Return errors wrapping pam.Error values on failure If the transaction fails during start, there's no way to get the error detail in a programmatic way, so let's wrap the pam.Error to allow more per-type checks. --- transaction.go | 11 +++++++---- transaction_test.go | 7 +++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/transaction.go b/transaction.go index 1142fdc..6859817 100644 --- a/transaction.go +++ b/transaction.go @@ -22,7 +22,7 @@ package pam import "C" import ( - "errors" + "fmt" "runtime" "runtime/cgo" "strings" @@ -164,7 +164,9 @@ func StartFunc(service, user string, handler func(Style, string) (string, error) // transaction provides an interface to the remainder of the API. func StartConfDir(service, user string, handler ConversationHandler, confDir string) (*Transaction, error) { if !CheckPamHasStartConfdir() { - return nil, errors.New("StartConfDir() was used, but the pam version on the system is not recent enough") + return nil, fmt.Errorf( + "%w: StartConfDir was used, but the pam version on the system is not recent enough", + ErrSystem) } return start(service, user, handler, confDir) @@ -174,7 +176,8 @@ func start(service, user string, handler ConversationHandler, confDir string) (* switch handler.(type) { case BinaryConversationHandler: if !CheckPamHasBinaryProtocol() { - return nil, errors.New("BinaryConversationHandler() was used, but it is not supported by this platform") + return nil, fmt.Errorf("%w: BinaryConversationHandler was used, but it is not supported by this platform", + ErrSystem) } } t := &Transaction{ @@ -198,7 +201,7 @@ func start(service, user string, handler ConversationHandler, confDir string) (* t.status = C.pam_start_confdir(s, u, t.conv, c, &t.handle) } if t.status != success { - return nil, t + return nil, Error(t.status) } return t, nil } diff --git a/transaction_test.go b/transaction_test.go index 809364c..5bc858e 100644 --- a/transaction_test.go +++ b/transaction_test.go @@ -208,6 +208,13 @@ func TestPAM_ConfDir_FailNoServiceOrUnsupported(t *testing.T) { if len(s) == 0 { t.Fatalf("error #expected an error message") } + var pamErr Error + if !errors.As(err, &pamErr) { + t.Fatalf("error #unexpected type: %#v", err) + } + if pamErr != ErrAbort { + t.Fatalf("error #unexpected status: %v", pamErr) + } } func TestPAM_ConfDir_InfoMessage(t *testing.T) { -- cgit v1.2.3