diff options
| author | roerick <roerick@wyo.town> | 2025-10-19 14:24:40 -0600 |
|---|---|---|
| committer | roerick <roerick@wyo.town> | 2025-10-19 14:24:40 -0600 |
| commit | 132e0fe607396123a9fc5390c8657ef145bba3c6 (patch) | |
| tree | 9a420d6f89a2de70d17bfb08789a26ccc501a9b9 /bsdauth | |
| parent | a1c3883334a9c54358173ebd822a5a85036dcda6 (diff) | |
initial commit with auth working
Diffstat (limited to 'bsdauth')
| -rw-r--r-- | bsdauth/bsdauth.go | 48 | ||||
| -rw-r--r-- | bsdauth/bsdauth_stub.go | 11 |
2 files changed, 59 insertions, 0 deletions
diff --git a/bsdauth/bsdauth.go b/bsdauth/bsdauth.go new file mode 100644 index 0000000..6105360 --- /dev/null +++ b/bsdauth/bsdauth.go @@ -0,0 +1,48 @@ +//go:build openbsd && cgo + +package bsdauth + +/* +#cgo CFLAGS: -DOPENBSD +#cgo LDFLAGS: -lutil +#include <stdlib.h> +#include <sys/types.h> +#include <login_cap.h> +#include <bsd_auth.h> + +// Tiny wrapper to keep the cgo call site clean. +static int go_auth_userokay(const char *name, const char *style, const char *atype, const char *password) { + return auth_userokay((char*)name, (char*)style, (char*)atype, (char*)password); +} +*/ +import "C" +import ( + "errors" + "unsafe" +) + +// Authenticate verifies a username and password using OpenBSD’s BSD Authentication subsystem. +// `atype` identifies the service name (e.g., "auth-myapp"). +// `style` is left empty to use the system default style from login.conf. +func Authenticate(username, password, atype string) (bool, error) { + if username == "" { + return false, errors.New("bsdauth: empty username") + } + if atype == "" { + atype = "auth-myapp" + } + + cUser := C.CString(username) + cPass := C.CString(password) + cType := C.CString(atype) + var cStyle *C.char = nil // default style from login.conf + defer func() { + C.free(unsafe.Pointer(cUser)) + C.free(unsafe.Pointer(cPass)) + C.free(unsafe.Pointer(cType)) + }() + + ok := C.go_auth_userokay(cUser, cStyle, cType, cPass) + return ok != 0, nil +} + diff --git a/bsdauth/bsdauth_stub.go b/bsdauth/bsdauth_stub.go new file mode 100644 index 0000000..95315f3 --- /dev/null +++ b/bsdauth/bsdauth_stub.go @@ -0,0 +1,11 @@ +//go:build !openbsd || !cgo + +package bsdauth + +import "errors" + +// Authenticate is a stub for non-OpenBSD platforms. +func Authenticate(username, password, atype string) (bool, error) { + return false, errors.New("bsdauth: unsupported platform (requires OpenBSD + cgo)") +} + |
