How aiexplorerofficial/ubuntu-pam-authentication works
1. What this repository is
- This is a Linux authentication extension, not an application server or daemon.
- The core deliverable is a PAM shared module (pam_auth.so) that Ubuntu loads during login and sudo-style authentication.
- The repo also includes the installer, enrollment tooling, and a verification test binary.
2. Top-level runtime boundary
- The main trust boundary is PAM ↔ operating system authentication.
- When a user logs in, PAM calls into the module, which then checks:
1. username validity,
2. password,
3. per-user TOTP secret,
4. 6-digit one-time code.
- Only if all checks pass does the module return success to PAM.
3. Authentication orchestration
- auth_module/src/pam_auth.c is the central control point.
- It coordinates the auth sequence and decides whether to accept or reject the login.
- It also ties together failure tracking, TOTP verification, secret loading, and audit logging.
- This file is the best place to anchor the “main request flow” in a diagram.
4. Security utility layer
- auth_module/src/security.c and security.h are shared support code used by both authentication and logging paths.
- This layer handles:
- input sanitization,
- constant-time comparison,
- secure memory wiping,
- failure counters and lockout logic,
- filesystem permission enforcement for state files.
- It acts as the project’s common security boundary enforcement layer.
5. TOTP engine and secret handling
- auth_module/src/totp_engine.c and totp_engine.h implement the one-time-password logic.
- It generates or loads shared secrets, decodes Base32 secret material, and computes RFC 6238 TOTP codes using HMAC-SHA1.
- The secret for each user lives outside the codebase in /etc/auth_module/<user>.secret, so the module depends on filesystem-stored enrollment state rather than embedded credentials.
6. Audit subsystem
- auth_module/src/audit_log.c and audit_log.h implement a tamper-evident log.
- Every authentication event is appended to /var/log/auth_module.log.
- Each entry is cryptographically chained to the previous one with an HMAC, so deletion, insertion, reordering, or modification can be detected later.
- The audit key is stored separately in /etc/auth_module/audit.key.
7. Enrollment and provisioning path
- auth_module/tools/setup_user.py appears to handle user enrollment and secret provisioning.
- It is the user-facing setup path that creates the TOTP secret and likely produces the QR-code / manual setup payload for authenticator apps.
- This script sits outside the PAM runtime path and is part of the administrative bootstrap flow.
8. Installer and system integration
- auth_module/install_2fa.sh is the system integration entry point.
- It compiles the module, installs dependencies, writes the shared object into the PAM load path, and edits /etc/pam.d/common-auth.
- It also creates rollback backups before modifying PAM configuration, which is important because a mistake here affects system login behavior.
9. State and filesystem layout
- The design relies on a small set of root-owned filesystem locations:
- /etc/auth_module/ for enrolled secrets and audit key,
- /var/lib/auth_module/ for lockout/failure state,
- /var/log/auth_module.log for authentication history,
- /lib/security/pam_auth.so for the module binary.
- This makes the architecture strongly stateful but keeps the state outside the code path and under privileged control.
10. Build and packaging model
- auth_module/Makefile builds both the PAM module and a test binary.
- The build is C-based and uses shared-library conventions appropriate for PAM deployment.
- The README’s security claims indicate OpenSSL is a key dependency for HMAC, random secret generation, and related crypto operations.
11. Testing and verification
- auth_module/tests/test_main.c builds into the auth_test tool used to verify audit log integrity.
- Testing is therefore not only functional auth testing; it also includes an offline integrity check of the log chain.
- This matters for architecture diagrams because the test path validates a separate security subsystem, not just login behavior.
12. End-to-end data flow to show in a diagram
- User enters username/password in PAM.
- pam_auth.c validates input and consults security.c for lockout state.
- It loads the user secret from /etc/auth_module/.
- totp_engine.c computes/validates the TOTP code.
- audit_log.c records success or failure into the chained log.
- On success, PAM continues normal OS authentication; on failure, PAM rejects access.
Main entry points: pam_auth.c PAM auth · install_2fa.sh Installer · audit_log.c Audit log · Makefile Build
Components 14
PAM Runtime
Login-time authentication flow inside the PAM boundary.
- PAM / OS · Linux login and sudo-style authentication environment that loads the PAM module.
- PAM auth · Central authentication controller that accepts or rejects the login request.
auth_module/src/pam_auth.c
Security Core
Shared checks and cryptographic helpers used by authentication and logging.
- Security core · Input sanitization, constant-time compare, secure wiping, failure counters, and permission enforcement.
auth_module/src/security.c - TOTP engine · Loads or creates user secrets and verifies RFC 6238 codes with HMAC-SHA1.
auth_module/src/totp_engine.c - Audit log · Appends chained authentication events to detect modification or reordering.
auth_module/src/audit_log.c
Filesystem State
Privileged files that hold secrets, lockout state, and audit history.
- User secret · Per-user TOTP secret stored outside the codebase for authentication.
- Lockout state · Root-owned failure and lockout records used to slow repeated attempts.
- Audit key · Separate key used to chain audit log entries.
- Audit file · Chained authentication history written by the audit subsystem.
Provisioning
Enrollment and installation paths outside the login-time flow.
- User setup · Administrative enrollment path that provisions a user secret and setup payload.
auth_module/tools/setup_user.py - Installer · Builds the module, installs dependencies, and updates PAM configuration.
auth_module/install_2fa.sh - pam_auth.so · Compiled PAM shared object loaded by the operating system during authentication.
Connections 16
- PAM / OS → PAM auth: loads · PAM invokes the module during login or sudo-style authentication.
- PAM auth → Security core: validates · Authentication controller delegates input checks and lockout handling.
- PAM auth → TOTP engine: verifies code · Module computes and checks the one-time password.
- PAM auth → Audit log: records event · Every success or failure is appended to the chained log.
- Security core → Lockout state: reads/writes · Failure counters and lockout status are maintained in filesystem state.
- TOTP engine → User secret: loads secret · Per-user secret material is read from privileged storage.
- Audit log → Audit file: appends · Authentication events are written to the log file.
- Audit log → Audit key: uses key · Log chaining relies on a separately stored HMAC key.
- User setup → User secret: provisions · Enrollment script creates or installs the user's TOTP secret.
- Installer → Build: builds via · Installer relies on the project build to produce deployable artifacts.
- Build → pam_auth.so: produces · Build output includes the PAM shared module.
- Installer → pam_auth.so: installs · Installer places the module into the PAM load path.
- Installer → PAM / OS: configures · Installer edits PAM configuration to activate the module.
- Build → Auth test: produces · Build output includes the audit verification binary.
- Auth test → Audit file: verifies · Offline test checks chained log integrity.
- PAM auth → PAM / OS: accepts/rejects · Successful authentication returns control to the operating system; failure blocks access.