Commit Graph

33 Commits

Author SHA1 Message Date
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) 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
Marco Trevisan (Treviño) 067f634acb transaction: Fix comment typo 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) 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
Marco Trevisan (Treviño) b9265b1c6a transaction: Add support for Binary conversation
PAM upports binary conversations using private protocols, this
can be handled by C but it's not supported here because we
implicitly convert all the messages to string, and this may lead
to issues when this is not the case (as in binary protocol the
pointer could contain zeros that the GoString conversion would
consider them the end of the message).

So, add another conversation handler implementation that allows
to handle the binary protocol, whose function callback accepts
a pointer to the struct (we can't use bytes as the length is
unknown and may be defined in the header of the pointer itself).

However since the binary prompt is not supported by all the
platforms we need to do a compile-time check to disable it in
case is used when not supported.
2023-09-22 04:05:16 +02:00
Marco Trevisan (Treviño) 78ffef4acd transaction: Use cgo.Handle to pass callback data to PAM
Go provides a nicer way to handle Go structs lifetime when they
are passed to C now, so use this instead of a custom
implementation that requires to store them in a map
2023-09-19 20:06:38 +02:00
Didier Roche bc958bdbd7
Allow to define confdir
PAM has a pam_start_confdir() which allows to define the configuration
directory where all services are located.
This is useful to define your own service on tests in particular, so
that you can control your stack and be independant of the host when
running them.
Allow defining this configuration directory, with a new StartConfDir
function.
Also, allow pre-checking for the API availability with
CheckPamHasStartConfdir().
2022-09-16 08:09:26 +02:00
Michael Steinert 02ccfbfaf0 [ci skip] Update documentation 2015-12-04 10:05:44 -06:00
Michael Steinert 1d0c8bc60c Fix lint 2015-12-04 09:46:42 -06:00
Michael Steinert 7f5fc62ba4 Remove dead code 2015-12-04 09:21:38 -06:00
Michael Steinert a245f1098c Fix a memory leak 2015-12-04 09:03:39 -06:00
Michael Steinert 8ec1202046 Stop passing Go pointers to C 2015-12-03 14:59:51 -06:00
Michael Steinert 61a0e177d2 Fix golint warnings 2015-04-10 15:04:52 -05:00
Michael Steinert 767cadc80c Small documentation updates 2015-03-30 19:16:13 -05:00
Michael Steinert 3215d873b9 This looks a bit better 2015-03-30 18:20:09 -05:00
Michael Steinert 77471e65f1 Fix a silly bug and add test coverage 2015-03-30 18:13:10 -05:00
Michael Steinert a0cde3fe01 Rework pam_getenvlist so it doesn't leak 2015-03-30 14:53:16 -05:00
Michael Steinert 9c771166c9 Add a test suite 2015-03-29 11:25:00 -05:00
Michael Steinert d67bb86e54 Update interface 2015-03-28 17:00:58 -05:00