Skip to content

Commit

Permalink
module-transaction: Add support for binary conversations
Browse files Browse the repository at this point in the history
A module can now initiate a binary conversation decoding the native
pointer value as it wants.

Added tests to verify the main cases
  • Loading branch information
3v1n0 committed Oct 14, 2023
1 parent b96def3 commit 3b8e957
Show file tree
Hide file tree
Showing 8 changed files with 587 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func (m *integrationTesterModule) handleRequest(authReq *authRequest, r *Request
case SerializableStringConvRequest:
args = append(args, reflect.ValueOf(
pam.NewStringConvRequest(v.Style, v.Request)))
case SerializableBinaryConvRequest:
args = append(args, reflect.ValueOf(
pam.NewBinaryConvRequestFromBytes(v.Request)))
default:
if arg == nil {
args = append(args, reflect.Zero(method.Type().In(i)))
Expand All @@ -75,6 +78,13 @@ func (m *integrationTesterModule) handleRequest(authReq *authRequest, r *Request
case pam.StringConvResponse:
res.ActionArgs = append(res.ActionArgs,
SerializableStringConvResponse{value.Style(), value.Response()})
case pam.BinaryConvResponse:
data, err := value.Decode(utils.TestBinaryDataDecoder)
if err != nil {
return nil, err
}
res.ActionArgs = append(res.ActionArgs,
SerializableBinaryConvResponse{data})
case pam.Status:
authReq.lastError = value
res.ActionArgs = append(res.ActionArgs, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ func ensureEnv(tx *pam.Transaction, variable string, expected string) error {
}
}

func (r *Request) toBytes(t *testing.T) []byte {
if bytes, err := r.GOB(); err != nil {
t.Fatalf("error: %v", err)
return nil
} else {
return bytes
}
}

func (r *Request) toTransactionData(t *testing.T) []byte {
return utils.TestBinaryDataEncoder(r.toBytes(t))
}

func Test_Moduler_IntegrationTesterModule(t *testing.T) {
if !pam.CheckPamHasStartConfdir() {
t.Skip("this requires PAM with Conf dir support")
Expand Down Expand Up @@ -854,6 +867,107 @@ func Test_Moduler_IntegrationTesterModule(t *testing.T) {
},
},
},
"start-conv-binary": {
expectedStatus: pam.Success,
credentials: utils.NewBinaryTransactionWithData([]byte(
"\x00This is a binary data request\xC5\x00\xffYes it is!"),
[]byte{0x01, 0x02, 0x03, 0x05, 0x00, 0x99}),
checkedRequests: []checkedRequest{
{
r: NewRequest("StartConv", SerializableBinaryConvRequest{
utils.TestBinaryDataEncoder(
[]byte("\x00This is a binary data request\xC5\x00\xffYes it is!")),
}),
exp: []interface{}{SerializableBinaryConvResponse{
[]byte{0x01, 0x02, 0x03, 0x05, 0x00, 0x99},
}, nil},
},
{
r: NewRequest("StartBinaryConv",
utils.TestBinaryDataEncoder(
[]byte("\x00This is a binary data request\xC5\x00\xffYes it is!"))),
exp: []interface{}{SerializableBinaryConvResponse{
[]byte{0x01, 0x02, 0x03, 0x05, 0x00, 0x99},
}, nil},
},
},
},
"start-conv-binary-handle-failure-passed-data-mismatch": {
expectedStatus: pam.ConvErr,
credentials: utils.NewBinaryTransactionWithData([]byte(
"\x00This is a binary data request\xC5\x00\xffYes it is!"),
[]byte{0x01, 0x02, 0x03, 0x05, 0x00, 0x99}),
checkedRequests: []checkedRequest{
{
r: NewRequest("StartConv", SerializableBinaryConvRequest{
(&Request{"Not the expected binary data", nil}).toTransactionData(t),
}),
exp: []interface{}{nil, pam.ConvErr},
},
{
r: NewRequest("StartBinaryConv",
(&Request{"Not the expected binary data", nil}).toTransactionData(t)),
exp: []interface{}{nil, pam.ConvErr},
},
},
},
"start-conv-binary-handle-failure-returned-data-mismatch": {
expectedStatus: pam.ConvErr,
credentials: utils.NewBinaryTransactionWithRandomData(100,
[]byte{0x01, 0x02, 0x03, 0x05, 0x00, 0x99}),
checkedRequests: []checkedRequest{
{
r: NewRequest("StartConv", SerializableBinaryConvRequest{
(&Request{"Wrong binary data", nil}).toTransactionData(t),
}),
exp: []interface{}{nil, pam.ConvErr},
},
{
r: NewRequest("StartBinaryConv",
(&Request{"Wrong binary data", nil}).toTransactionData(t)),
exp: []interface{}{nil, pam.ConvErr},
},
},
},
"start-conv-binary-in-nil": {
expectedStatus: pam.Success,
credentials: utils.NewBinaryTransactionWithData(nil,
(&Request{"Binary data", []interface{}{true, 123, 0.5, "yay!"}}).toBytes(t)),
checkedRequests: []checkedRequest{
{
r: NewRequest("StartConv", SerializableBinaryConvRequest{}),
exp: []interface{}{SerializableBinaryConvResponse{
(&Request{"Binary data", []interface{}{true, 123, 0.5, "yay!"}}).toBytes(t),
}, nil},
},
{
r: NewRequest("StartBinaryConv", nil),
exp: []interface{}{SerializableBinaryConvResponse{
(&Request{"Binary data", []interface{}{true, 123, 0.5, "yay!"}}).toBytes(t),
}, nil},
},
},
},
"start-conv-binary-out-nil": {
expectedStatus: pam.Success,
credentials: utils.NewBinaryTransactionWithData([]byte(
"\x00This is a binary data request\xC5\x00\xffGimme nil!"), nil),
checkedRequests: []checkedRequest{
{
r: NewRequest("StartConv", SerializableBinaryConvRequest{
utils.TestBinaryDataEncoder(
[]byte("\x00This is a binary data request\xC5\x00\xffGimme nil!")),
}),
exp: []interface{}{SerializableBinaryConvResponse{}, nil},
},
{
r: NewRequest("StartBinaryConv",
utils.TestBinaryDataEncoder(
[]byte("\x00This is a binary data request\xC5\x00\xffGimme nil!"))),
exp: []interface{}{SerializableBinaryConvResponse{}, nil},
},
},
},
}

for name, tc := range tests {
Expand Down Expand Up @@ -1119,6 +1233,15 @@ func Test_Moduler_IntegrationTesterModule_Authenticate(t *testing.T) {
exp: []interface{}{nil, pam.SystemErr},
}},
},
"StartConv-Binary": {
expectedStatus: pam.SystemErr,
checkedRequests: []checkedRequest{{
r: NewRequest("StartConv", SerializableBinaryConvRequest{
[]byte{0x01, 0x02, 0x03, 0x05, 0x00, 0x99},
}),
exp: []interface{}{nil, pam.SystemErr},
}},
},
}

for name, tc := range tests {
Expand Down
12 changes: 12 additions & 0 deletions cmd/pam-moduler/tests/integration-tester-module/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ type SerializableStringConvResponse struct {
Response string
}

type SerializableBinaryConvRequest struct {
Request []byte
}

type SerializableBinaryConvResponse struct {
Response []byte
}

func init() {
gob.Register(map[string]string{})
gob.Register(Request{})
Expand All @@ -43,5 +51,9 @@ func init() {
SerializableStringConvRequest{})
gob.RegisterName("main.SerializableStringConvResponse",
SerializableStringConvResponse{})
gob.RegisterName("main.SerializableBinaryConvRequest",
SerializableBinaryConvRequest{})
gob.RegisterName("main.SerializableBinaryConvResponse",
SerializableBinaryConvResponse{})
gob.Register(utils.SerializableError{})
}
84 changes: 84 additions & 0 deletions cmd/pam-moduler/tests/internal/utils/test-utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package utils

//#include <stdint.h>
import "C"

import (
"crypto/rand"
"encoding/binary"
"fmt"
"reflect"
"unsafe"

"github.com/msteinert/pam"
)
Expand Down Expand Up @@ -148,3 +155,80 @@ func (c Credentials) RespondPAM(s pam.Style, msg string) (string, error) {
return "", pam.NewTransactionError(
&SerializableError{fmt.Sprintf("unhandled style: %v", s)}, pam.ConvErr)
}

type BinaryTransaction struct {
data []byte
ExpectedNull bool
ReturnedData []byte
}

func TestBinaryDataEncoder(bytes []byte) []byte {
if len(bytes) > 0xff {
panic("Binary transaction size not supported")
}

if bytes == nil {
return bytes
}

data := make([]byte, 0, len(bytes)+1)
data = append(data, byte(len(bytes)))
data = append(data, bytes...)
return data
}

func TestBinaryDataDecoder(ptr pam.BinaryPointer) ([]byte, error) {
if ptr == nil {
return nil, nil
}

length := uint8(*((*C.uint8_t)(ptr)))
return C.GoBytes(unsafe.Pointer(ptr), C.int(length+1))[1:], nil
}

func NewBinaryTransactionWithData(data []byte, retData []byte) BinaryTransaction {
t := BinaryTransaction{ReturnedData: retData}
t.data = TestBinaryDataEncoder(data)
t.ExpectedNull = data == nil
return t
}

func NewBinaryTransactionWithRandomData(size uint8, retData []byte) BinaryTransaction {
t := BinaryTransaction{ReturnedData: retData}
randomData := make([]byte, size)
if err := binary.Read(rand.Reader, binary.LittleEndian, &randomData); err != nil {
panic(err)
}

t.data = TestBinaryDataEncoder(randomData)
return t
}

func (b BinaryTransaction) Data() []byte {
return b.data
}

func (b BinaryTransaction) RespondPAM(s pam.Style, msg string) (string, error) {
return "", pam.NewTransactionError(
&SerializableError{"unexpected non-binary request"}, pam.ConvErr)
}

func (b BinaryTransaction) RespondPAMBinary(ptr pam.BinaryPointer) ([]byte, error) {
if ptr == nil && !b.ExpectedNull {
return nil, pam.NewTransactionError(
&SerializableError{"unexpected null binary data"}, pam.ConvErr)
} else if ptr == nil {
return TestBinaryDataEncoder(b.ReturnedData), nil
}

bytes, _ := TestBinaryDataDecoder(ptr)
if !reflect.DeepEqual(bytes, b.data[1:]) {
return nil, pam.NewTransactionError(
&SerializableError{
fmt.Sprintf("data mismatch %v vs %v", bytes, b.data[1:]),
},
pam.ConvErr)
}

return TestBinaryDataEncoder(b.ReturnedData), nil
}
47 changes: 47 additions & 0 deletions module-transaction-mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ void init_pam_conv(struct pam_conv *conv, uintptr_t appdata);
*/
import "C"
import (
"errors"
"fmt"
"reflect"
"runtime"
"runtime/cgo"
"testing"
Expand Down Expand Up @@ -131,8 +133,11 @@ type mockConversationHandler struct {
PromptEchoOff string
TextInfo string
ErrorMsg string
Binary []byte
ExpectedMessage string
ExpectedMessagesByStyle map[Style]string
ExpectedNil bool
ExpectedBinary []byte
CheckEmptyMessage bool
ExpectedStyle Style
CheckZeroStyle bool
Expand Down Expand Up @@ -175,3 +180,45 @@ func (c mockConversationHandler) RespondPAM(s Style, msg string) (string, error)
return "", NewTransactionError(
fmt.Errorf("unhandled style: %v", s), ConvErr)
}

func testBinaryDataEncoder(bytes []byte) []byte {
if len(bytes) > 0xff {
panic("Binary transaction size not supported")
}

if bytes == nil {
return bytes
}

data := make([]byte, 0, len(bytes)+1)
data = append(data, byte(len(bytes)))
data = append(data, bytes...)
return data
}

func testBinaryDataDecoder(ptr BinaryPointer) ([]byte, error) {
if ptr == nil {
return nil, nil
}

length := uint8(*((*C.uint8_t)(ptr)))
return C.GoBytes(unsafe.Pointer(ptr), C.int(length+1))[1:], nil
}

func (m mockConversationHandler) RespondPAMBinary(ptr BinaryPointer) ([]byte, error) {
if ptr == nil && !m.ExpectedNil {
return nil, NewTransactionError(
errors.New("unexpected null binary data"), ConvErr)
} else if ptr == nil {
return testBinaryDataEncoder(m.Binary), nil
}

bytes, _ := testBinaryDataDecoder(ptr)
if !reflect.DeepEqual(bytes, m.ExpectedBinary) {
return nil, NewTransactionError(
fmt.Errorf("data mismatch %v vs %v", bytes, m.ExpectedBinary),
ConvErr)
}

return testBinaryDataEncoder(m.Binary), nil
}
Loading

0 comments on commit 3b8e957

Please sign in to comment.