Commit Graph

119 Commits

Author SHA1 Message Date
Lars Scheibling 5d43b022cb Change to v1 2024-01-12 09:32:51 +00:00
Lars Scheibling c1a0f72fa1 Updated path 2024-01-12 09:20:18 +00:00
Lars Scheibling fabe599f29 Merge branch 'pam-moduler'
Test / test (1.20.x, ubuntu-latest) (push) Has been cancelled Details
Test / test (1.21.x, ubuntu-latest) (push) Has been cancelled Details
Lint / lint (push) Has been cancelled Details
2024-01-12 09:14:01 +00:00
Marco Trevisan (Treviño) 6c585aed74 ci: Show coverage for all packages
Test / test (1.20.x, ubuntu-latest) (pull_request) Has been cancelled Details
Test / test (1.21.x, ubuntu-latest) (pull_request) Has been cancelled Details
Lint / lint (pull_request) Has been cancelled Details
Test / test (1.20.x, ubuntu-latest) (push) Has been cancelled Details
Test / test (1.21.x, ubuntu-latest) (push) Has been cancelled Details
Lint / lint (push) Has been cancelled Details
We have test utils in other packages that are not shown as tested, while
they definitely are.
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) 04578a00b0 README: Update how to run tests 2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) 89c1e430c1 transaction: Add support for using raw binary pointers conversation handler
This requires the allocating function to provide a binary pointer that
will be free'd by the conversation handlers finalizers.

This is for a more advanced usage scenario where the binary conversion
may be handled manually.
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) 0143d11445 transaction: Add BinaryConversationFunc adapter 2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) eac1f2d85d github/test: Run tests with address sanitizer
We have lots of cgo interaction here so better to check things fully.

This also requires manually checking for leaks, so add support for this.
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) 04ad7bdc73 module-transaction: Do not allow parallel conversations by default
Pam conversations per se may also run in parallel, but this implies that
the application supports this.

Since this normally not the case, do not create modules that may invoke
the pam conversations in parallel by default, adding a mutex to protect
such calls.
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) ac879208ea module-transaction: Add support for binary conversations
A module can now initiate a binary conversation decoding the native
pointer value as it wants.

Added tests to verify the main cases
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) 883dc86533 module-transaction: Add support for initiating PAM Conversations
Modules have the ability to start PAM conversations, so while the
transaction code can handle them we did not have a way to init them.
Yet.

So add some APIs allowing this, making it easier from the go side to
handle the conversations.

In this commit we only support text-based conversations, but code is
designed with the idea of supporting binary cases too.

Added the integration tests using the module that is now able to both
start conversation and handle them using Go only.
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) 7a073f5ba0 module-transaction: Add support for setting/getting module data
Module data is data associated with a module handle that is available
for the whole module loading time so it can be used also during
different operations.

We use cgo handles to preserve the life of the go objects so any value
can be associated with a pam transaction.
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) 449b2672b9 module-transaction: Add GetUser() method that prompts an user if non-set
We can now finally test this properly both using a mock and through the
interactive module that will do the request for us in various conditions.
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) c1b7ee1623 tests: Add a module implementation with dynamic control from the app
In order to properly test the interaction of a module transaction from
the application point of view, we need to perform operation in the
module and ensure that the expected values are returned and handled

In order to do this, without using the PAM apis that we want to test,
use a simple trick:
 - Create an application that works as server using an unix socket
 - Create a module that connects to it
 - Pass the socket to the module via the module service file arguments
 - Add some basic protocol that allows the application to send a request
   and to the module to reply to that.
 - Use reflection and serialization to automatically call module methods
   and return the values to the application where we do the check
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) 8c30b5946a pam-moduler: Add test that generates a new debug module and verify it works
We mimic what pam_debug.so does by default, by implementing a similar
module fully in go, generated using pam-moduler.

This requires various utilities to generate the module and run the tests
that are in a separate internal modules so that it can be shared between
multiple implementations
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) 6f3af6e9b2 moduler: Move module transaction invoke handling to transaction itself
So we can reduce the generated code and add more unit tests
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) e0e1d2de2c transaction: Move PAM app side function only to app-transaction
In this way all these features not even compiled when creating modules,
avoiding generating unused code.
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) 05f676c233 transaction, moduler: Do not export PAM conv handler function to modules
This function is only needed when using go PAM for creating applications
so it's not something we expect to have exported to library modules.

To prevent this use an `asPamModule` tag to prevent compilation of
application-only features.
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) 4b39bd8e11 transaction: Define C functions as unexported static inlines
This will make it easier to avoid exporting unexpected symbols to the
generated PAM libraries.

Also it makes less messy handling C code inside go files.
2023-12-14 22:07:50 +01:00
Marco Trevisan (Treviño) a8c4a14150 pam-moduler: Add first implementation of a Go PAM Module generator
A PAM module can be generated using pam-moduler and implemented fully in
go without having to manually deal with the C setup.

Module can be compiled using go generate, so go:generate directives can be
used to make this process automatic, with a single go generate call as shown
in the example.
2023-12-14 22:07:48 +01:00
Marco Trevisan (Treviño) f0d140c281 transaction: Properly handle nil bytes in binary transactions
If returned binaries are nil, we should pass them as nil and not as an
empty bytes array.
2023-12-01 19:59:59 +01:00
Marco Trevisan (Treviño) 11daf4a88d transaction: Add ModuleTransaction type and ModuleHandler interface
This allows to easily define go-handlers for module operations.

We need to expose few more types externally so that it's possible to
create the module transaction handler and return specific transaction
errors
2023-12-01 19:59:59 +01:00
Marco Trevisan (Treviño) bbc25e137c transaction: Add a transaction base type to define more transaction kinds
A pam handler can be used both by a module and by an Application, go-pam
is meant to be used in the application side right now, but it can be
easily changed to also create modules.

This is the prerequisite work to support this.
2023-12-01 19:59:59 +01:00
Michael Steinert 2bda45159c Update README for v2 2023-11-30 12:49:59 -06:00
Mike Steinert 30fb28f158
Merge pull request #21 from msteinert/v2
Update module version to v2
2023-11-30 12:47:56 -06:00
Michael Steinert 50ded1b0e7 Update module version to v2 2023-11-30 12:41:22 -06:00
Mike Steinert 6339228bcd
Merge pull request #19 from msteinert/codecov
Another try at adding codecov
2023-11-29 21:59:13 -06:00
Michael Steinert 004c7008f4 Another try at adding codecov 2023-11-29 21:50:06 -06:00
Mike Steinert fb46dfb9da
Merge pull request #18 from msteinert/codecov
Add codecov configuration
2023-11-29 21:39:46 -06:00
Michael Steinert 64ad2183e8 Add codecov configuration 2023-11-29 21:36:53 -06:00
Mike Steinert 6bce85f6ba
Merge pull request #17 from msteinert/update-example
Update example code to call Transaction.End
2023-11-29 21:17:23 -06:00
Michael Steinert 4537bb5583 Update example code to call Transaction.End 2023-11-29 21:14:45 -06:00
Mike Steinert 4ce1d8a7e0
Merge pull request #15 from 3v1n0/safer-transaction
Safer transaction: add End() method and don't use as error
2023-11-29 20:08:15 -06:00
Marco Trevisan (Treviño) 067f634acb transaction: Fix comment typo 2023-11-30 02:49:29 +01:00
Marco Trevisan (Treviño) e6f817312a transaction: Skip some tests requiring confdir if not available 2023-11-30 02:49:29 +01:00
Marco Trevisan (Treviño) 01f62f11f7 transaction_test: Add tests checking the loaded services match 2023-11-30 02:49:29 +01:00
Marco Trevisan (Treviño) 31a452ad25 transaction: Add missing default PAM item types 2023-11-30 02:49:29 +01:00
Marco Trevisan (Treviño) fe75bbaeee transaction: Mark Item, Flags and Style const values as Item, Flags and Style types
We redefined various PAM constant values for items, flags and style, but
only few of them were marked as being Item's or Flag's. This caused go to
just consider them as generic integers instead of the actual subtype.
2023-11-30 02:49:29 +01:00
Marco Trevisan (Treviño) c7ecbf20dc transaction: Add a test finalizer checking if transaction has ended
Check if a transaction is ended in in tests.
2023-11-30 02:49:29 +01:00
Marco Trevisan (Treviño) c635cfc38a transaction: Add End() method and Remove Transaction finalizer
A PAM transaction needs to be ended in order to release the associated
resources, however this can't be sadly automated as the go finalizers
run in goroutines and this could cause problems to modules that we load.

In fact a module code may be called back during pam_end (to cleanup data
for example) and the module code could not be thread safe.

So let's make this more manual, but safer.
The transaction status is still preserved in the transaction so end will
be automatically called with the last-known status.

Closes: #14
2023-11-30 01:16:39 +01:00
Marco Trevisan (Treviño) 7162004668 transaction: Do not make Transaction to implement error interface anymore
As per previous commit, Transaction can't be used anymore as an error
value, but we instead we always return the status code.
2023-11-30 01:16:39 +01:00
Marco Trevisan (Treviño) adffdfbbdc transaction: Never return Transaction as error
While transaction does implement error, it's not a valid error
implementer because it may have bogous values since it's not thread-safe
and so we may read the result of Error() when it's into an invalid state

As per this never return it as an error, while always return the Status
unless when not available, where we still return pam.Error.
2023-11-30 01:16:39 +01:00
Marco Trevisan (Treviño) 911a346a00 transaction: Use Atomic to store/load the status
Transactions save the status of each operation in a status field, however
such field could be written concurrently by various operations, so we
need to be sure that:
 - We always return the status for the current operation
 - We store the status in a atomic way so that other actions won't
   create write races

In general, in a multi-thread operation one should not rely on
Transaction.Error() to get info about the last operation.
2023-11-30 01:16:39 +01:00
Marco Trevisan (Treviño) 3e4f7f5e4b transaction: Add an helper function to handle pam functions return status
All the pam functions return an integer with the status of the operation
so instead of duplicating the same code everywhere, that is quite error
prone, use an helper function.

It would have been nice to make this more dynamic, but cgo doesn't allow
us to do much magic here.

This is enough though.
2023-11-30 01:16:39 +01:00
Marco Trevisan (Treviño) a5f5ad6470 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.
2023-11-30 01:16:39 +01:00
Marco Trevisan (Treviño) ea51cc0fe4 transaction: Add tests for all the possible Status (and error) values
Use pam_debug.so to generate pam configurations at test time and
check if the returned values expect the ones we want.
2023-11-30 01:16:39 +01:00
Marco Trevisan (Treviño) 6bb315c571 transaction: Add PAM Error types Go definitions
And use them instead of C ones. Given that we have strings for them we
can easily implement error interfaces for it too.
2023-11-30 01:16:39 +01:00
Marco Trevisan (Treviño) 44c364e364 ci: Use golang-ci linter 2023-11-30 01:16:38 +01:00
Michael Steinert a85a609bbe Run go fmt 2023-09-23 10:38:09 -05:00
Mike Steinert 2e7b8b76d5
Merge pull request #9 from 3v1n0/binary-protocol
transaction: Add support for Binary conversation
2023-09-22 09:16:15 -05:00