diff options
author | Marco Trevisan (Treviño) <[email protected]> | 2023-09-25 13:44:45 +0200 |
---|---|---|
committer | Marco Trevisan (Treviño) <[email protected]> | 2023-11-30 01:16:39 +0100 |
commit | a5f5ad6470e625b72c71cac9477173307b005af2 (patch) | |
tree | 9d4d6c20be67c47ed33162305f7796f020d35c6c | |
parent | ea51cc0fe42e421ba0eaedded96bfe6448cbc43c (diff) |
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.
-rw-r--r-- | transaction.go | 11 | ||||
-rw-r--r-- | 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) { |