# aiexplorerofficial/ubuntu-pam-authentication architecture

> Architecture diagram of the GitHub repository aiexplorerofficial/ubuntu-pam-authentication, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aiexplorerofficial/ubuntu-pam-authentication
- Repository: https://github.com/aiexplorerofficial/ubuntu-pam-authentication

## Overview

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: [`auth_module/src/pam_auth.c`](https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/src/pam_auth.c) (PAM auth), [`auth_module/install_2fa.sh`](https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/install_2fa.sh) (Installer), [`auth_module/src/audit_log.c`](https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/src/audit_log.c) (Audit log), [`auth_module/Makefile`](https://github.com/aiexplorerofficial/ubuntu-pam-authentication/tree/main/auth_module/Makefile) (Build)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["PAM Runtime"]
  node_pam_os(("PAM / OS<br/>runtime boundary"))
  node_pam_auth["PAM auth<br/>PAM module<br/>[pam_auth.c]"]
end

subgraph group_support["Security Core"]
  node_security_core["Security core<br/>auth support<br/>[security.c]"]
  node_totp_engine["TOTP engine<br/>[totp_engine.c]"]
  node_audit_log["Audit log<br/>tamper-evident log<br/>[audit_log.c]"]
end

subgraph group_state["Filesystem State"]
  node_user_secret["User secret<br/>enrolled secret"]
  node_lockout_state["Lockout state<br/>failure state"]
  node_audit_key["Audit key<br/>hmac key"]
  node_audit_file["Audit file<br/>log file"]
end

subgraph group_provisioning["Provisioning"]
  node_setup_user["User setup<br/>enrollment script<br/>[setup_user.py]"]
  node_installer["Installer<br/>[install_2fa.sh]"]
  node_module_bin["pam_auth.so<br/>shared module"]
end

subgraph group_build["Build and Test"]
  node_makefile["Build"]
  node_auth_test["Auth test<br/>verification binary<br/>[test_main.c]"]
end

node_pam_os -->|"loads"| node_pam_auth
node_pam_auth -->|"validates"| node_security_core
node_pam_auth -->|"verifies code"| node_totp_engine
node_pam_auth -->|"records event"| node_audit_log
node_security_core -->|"reads/writes"| node_lockout_state
node_totp_engine -->|"loads secret"| node_user_secret
node_audit_log -->|"appends"| node_audit_file
node_audit_log -->|"uses key"| node_audit_key
node_setup_user -->|"provisions"| node_user_secret
node_installer -->|"builds via"| node_makefile
node_makefile -->|"produces"| node_module_bin
node_installer -->|"installs"| node_module_bin
node_installer -->|"configures"| node_pam_os
node_makefile -->|"produces"| node_auth_test
node_auth_test -->|"verifies"| node_audit_file
node_pam_auth -->|"accepts/rejects"| node_pam_os

click node_pam_auth "https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/src/pam_auth.c"
click node_security_core "https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/src/security.c"
click node_totp_engine "https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/src/totp_engine.c"
click node_audit_log "https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/src/audit_log.c"
click node_setup_user "https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/tools/setup_user.py"
click node_installer "https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/install_2fa.sh"
click node_makefile "https://github.com/aiexplorerofficial/ubuntu-pam-authentication/tree/main/auth_module/Makefile"
click node_auth_test "https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/tests/test_main.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pam_os,node_pam_auth toneBlue
class node_security_core,node_totp_engine,node_audit_log toneAmber
class node_user_secret,node_lockout_state,node_audit_key,node_audit_file toneMint
class node_setup_user,node_installer,node_module_bin toneRose
class node_makefile,node_auth_test toneIndigo
```

## Components

### PAM Runtime

Login-time authentication flow inside the PAM boundary.

- **PAM / OS**: runtime boundary · Linux login and sudo-style authentication environment that loads the PAM module.
- [PAM auth](https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/src/pam_auth.c): PAM module · 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](https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/src/security.c): auth support · Input sanitization, constant-time compare, secure wiping, failure counters, and permission enforcement. · `auth_module/src/security.c`
- [TOTP engine](https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/src/totp_engine.c): otp engine · Loads or creates user secrets and verifies RFC 6238 codes with HMAC-SHA1. · `auth_module/src/totp_engine.c`
- [Audit log](https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/src/audit_log.c): tamper-evident 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**: enrolled secret · Per-user TOTP secret stored outside the codebase for authentication.
- **Lockout state**: failure state · Root-owned failure and lockout records used to slow repeated attempts.
- **Audit key**: hmac key · Separate key used to chain audit log entries.
- **Audit file**: log file · Chained authentication history written by the audit subsystem.

### Provisioning

Enrollment and installation paths outside the login-time flow.

- [User setup](https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/tools/setup_user.py): enrollment script · Administrative enrollment path that provisions a user secret and setup payload. · `auth_module/tools/setup_user.py`
- [Installer](https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/install_2fa.sh): system installer · Builds the module, installs dependencies, and updates PAM configuration. · `auth_module/install_2fa.sh`
- **pam_auth.so**: shared module · Compiled PAM shared object loaded by the operating system during authentication.

### Build and Test

Compilation and offline verification of the module and audit log.

- [Build](https://github.com/aiexplorerofficial/ubuntu-pam-authentication/tree/main/auth_module/Makefile): build config · Builds the PAM module and the verification binary. · `auth_module/Makefile`
- [Auth test](https://github.com/aiexplorerofficial/ubuntu-pam-authentication/blob/main/auth_module/tests/test_main.c): verification binary · Offline tool that verifies audit log integrity. · `auth_module/tests/test_main.c`

## Connections

- 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.
