This guide provides comprehensive information about configuring and using PAM (Pluggable Authentication Modules) authentication with the stable-diffusion.cpp-rest server.
PAM authentication allows the server to authenticate users against the system's authentication infrastructure, enabling integration with:
New Feature: When Unix authentication is enabled and PAM is available, Unix authentication now delegates to PAM as the authentication backend. This provides a seamless integration where Unix auth uses PAM for credential verification while maintaining the Unix token-based session management.
Unix Auth Enabled + PAM Available:
┌─────────────────┐
│ Client Request │
│ (username, │
│ password) │
└─────────┬───────┘
│
▼
┌─────────────────┐
│ AuthMiddleware │
│ (extracts │
│ credentials) │
└─────────┬───────┘
│
▼
┌─────────────────┐
│ UserManager │
│ authenticateUnix│
│ (delegates to │
│ PAM) │
└─────────┬───────┘
│
▼
┌─────────────────┐
│ PamAuth │
│ (system auth) │
└─────────┬───────┘
│
▼
┌─────────────────┐
│ Unix Token │
│ (session mgmt) │
└─────────────────┘
To enable Unix+PAM integration:
# Enable Unix authentication with PAM backend
./stable-diffusion-rest-server \
--auth unix \
--pam-service-name stable-diffusion-rest \
--port 8080
Or enable PAM authentication directly:
# Enable PAM authentication
./stable-diffusion-rest-server \
--auth pam \
--pam-service-name stable-diffusion-rest \
--port 8080
| Configuration | Password Required | Authentication Method | Token Type |
|---|---|---|---|
| Unix auth + PAM enabled | Yes | PAM system auth | Unix token |
| Unix auth + PAM disabled | No | Traditional Unix auth | Unix token |
| JWT auth | Yes | Internal user database | JWT token |
| PAM auth | Yes | PAM system auth | JWT token |
The WebUI has been updated to support the Unix+PAM authentication flow:
The login API endpoint now accepts passwords for Unix authentication:
# Unix+PAM login (password required when PAM enabled)
curl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"username": "your_username",
"password": "your_password"
}'
New error codes for Unix+PAM integration:
MISSING_PASSWORD: When PAM enabled but no password providedAUTHENTICATION_FAILED: When PAM authentication failsPAM_NOT_AVAILABLE: When PAM required but not compiled in--enable-pam-auth/etc/pam.d/stable-diffusion-restPAM Development Libraries:
# Ubuntu/Debian
sudo apt-get install libpam0g-dev
# CentOS/RHEL/Fedora
sudo yum install pam-devel
# Arch Linux
sudo pacman -S pam
Build Requirements:
PAM authentication support is enabled by default when PAM libraries are detected. You can control this with CMake options:
# Build with PAM support (default when available)
mkdir build && cd build
cmake -DENABLE_PAM_AUTH=ON ..
cmake --build . --parallel
# Build without PAM support
cmake -DENABLE_PAM_AUTH=OFF ..
cmake --build . --parallel
# Check if PAM support will be built
cmake -LA | grep ENABLE_PAM_AUTH
Create a PAM service file at /etc/pam.d/stable-diffusion-rest:
sudo touch /etc/pam.d/stable-diffusion-rest
sudo chmod 644 /etc/pam.d/stable-diffusion-rest
A basic configuration using standard Unix authentication:
# /etc/pam.d/stable-diffusion-rest
# Basic PAM configuration for stable-diffusion-rest
# Use the system's standard authentication method
auth sufficient pam_unix.so try_first_pass nullok_secure
auth required pam_deny.so
# Account management
account sufficient pam_unix.so
account required pam_deny.so
# Password management (if needed)
password sufficient pam_unix.so nullok_use_authtok nullok_secure md5 shadow
password required pam_deny.so
# Session management
session required pam_limits.so
session required pam_unix.so
For LDAP authentication:
# /etc/pam.d/stable-diffusion-rest
# LDAP authentication configuration
# Authenticate against LDAP
auth sufficient pam_ldap.so use_first_pass
auth required pam_deny.so
# Account management through LDAP
account sufficient pam_ldap.so
account required pam_deny.so
# Password management through LDAP
password sufficient pam_ldap.so
password required pam_deny.so
# Session management
session required pam_mkhomedir.so skel=/etc/skel/ umask=0022
session required pam_limits.so
session optional pam_ldap.so
For Active Directory via Winbind/Samba:
# /etc/pam.d/stable-diffusion-rest
# Active Directory authentication
# Authenticate against Active Directory
auth sufficient pam_winbind.so use_first_pass
auth required pam_deny.so
# Account management
account sufficient pam_winbind.so
account required pam_deny.so
# Password management
password sufficient pam_winbind.so use_authtok use_first_pass
password required pam_deny.so
# Session management
session required pam_mkhomedir.so skel=/etc/skel/ umask=0022
session required pam_limits.so
Start the server with PAM authentication enabled:
./stable-diffusion-rest-server \
--models-dir /data/SD_MODELS \
--checkpoints checkpoints \
--auth pam \
--pam-service-name stable-diffusion-rest \
--port 8080 \
--host 0.0.0.0
| Option | Description | Default |
|---|---|---|
--auth |
Authentication method (none, jwt, api-key, unix, pam, optional) | none |
--auth-method |
Authentication method (alias for --auth) | none |
--pam-service-name |
PAM service name | stable-diffusion-rest |
--enable-guest-access |
Allow unauthenticated access | false |
--auth-realm |
Authentication realm for HTTP auth | stable-diffusion-rest |
Allow both authenticated and unauthenticated access:
./stable-diffusion-rest-server \
--models-dir /data/SD_MODELS \
--checkpoints checkpoints \
--auth optional \
--pam-service-name stable-diffusion-rest \
--enable-guest-access \
--port 8080
The following options are deprecated and will be removed in a future version:
--enable-unix-auth - Use --auth unix instead--enable-pam-auth - Use --auth pam insteadcurl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"username": "your_username",
"password": "your_password"
}'
curl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"username": "your_username",
"password": "your_password"
}'
curl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"username": "your_username"
}'
Response:
{
"success": true,
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": "1001",
"username": "your_username",
"role": "user",
"permissions": ["generate", "models_view"]
},
"expires_in": 3600
}
Include the token in subsequent requests:
curl -X GET http://localhost:8080/api/v1/models \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
{
"error": {
"message": "Authentication failure",
"code": "AUTHENTICATION_FAILED",
"timestamp": 1634567890
}
}
{
"error": {
"message": "PAM authentication not available",
"code": "PAM_AUTH_UNAVAILABLE",
"timestamp": 1634567890
}
}
{
"error": {
"message": "User account has expired",
"code": "ACCOUNT_EXPIRED",
"timestamp": 1634567890
}
}
Restrict Service File Permissions:
sudo chmod 644 /etc/pam.d/stable-diffusion-rest
sudo chown root:root /etc/pam.d/stable-diffusion-rest
Use Specific PAM Modules: Avoid overly permissive PAM configurations
Account Lockout: Configure account lockout in PAM to prevent brute force attacks
Audit Logging: Enable PAM logging for security monitoring:
# Add to /etc/pam.d/stable-diffusion-rest
auth required pam_warn.so
Error: PAM authentication not available
Solutions:
Check if PAM libraries are installed:
ldconfig -p | grep libpam
Verify server was built with PAM support:
./stable-diffusion-rest-server --help | grep pam
Rebuild with PAM support:
cmake -DENABLE_PAM_AUTH=ON ..
cmake --build . --parallel
Error: Authentication failure
Solutions:
Check PAM service file syntax:
sudo pam-auth-update --package stable-diffusion-rest
Test PAM configuration directly:
sudo pamtester stable-diffusion-rest username authenticate
Check system logs:
sudo journalctl -u systemd-logind
sudo tail -f /var/log/auth.log
Error: User account has expired or Credential expired
Solutions:
Check account status:
sudo chage -l username
Update account expiration:
sudo chage -E -1 username
Unlock locked account:
sudo passwd -u username
Enable debug logging for troubleshooting:
./stable-diffusion-rest-server \
--models-dir /data/SD_MODELS \
--checkpoints checkpoints \
--auth-method pam \
--verbose \
--log-file /tmp/stable-diffusion-auth.log
Use pamtester to test PAM configuration:
# Install pamtester
sudo apt-get install pamtester
# Test authentication
sudo pamtester stable-diffusion-rest username authenticate
# Test account management
sudo pamtester stable-diffusion-rest username acct_mgmt
Test the integrated Unix+PAM authentication:
# Start server with Unix auth and PAM enabled
./build/stable-diffusion-rest-server --auth unix
# Test login with password (should authenticate via PAM)
curl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "youruser", "password": "yourpassword"}'
# Test login without password (will fail if PAM is enabled)
curl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "youruser"}'
Test Unix authentication when PAM is not available:
# Start server with Unix auth but PAM disabled
./build/stable-diffusion-rest-server --auth unix
# Test login with username only (should work)
curl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "youruser"}'
Use a custom PAM service name:
./stable-diffusion-rest-server \
--models-dir /data/SD_MODELS \
--checkpoints checkpoints \
--auth pam \
--pam-service-name my-custom-service
Then create /etc/pam.d/my-custom-service with your desired configuration.
Configure PAM for multi-factor authentication:
# /etc/pam.d/stable-diffusion-rest
# Multi-factor authentication example
# First factor: Password
auth [success=1 default=ignore] pam_unix.so nullok_secure
auth requisite pam_deny.so
# Second factor: Google Authenticator
auth sufficient pam_google_authenticator.so
# Fallback
auth required pam_deny.so
For SSH key-based authentication:
# /etc/pam.d/stable-diffusion-rest
# SSH key authentication
auth sufficient pam_sshauth.so
auth required pam_deny.so
Update server configuration to use PAM:
--auth pam
Update client applications to use PAM login endpoint
Migrate existing users to system accounts if needed
Use optional authentication mode:
--auth optional
For issues with PAM authentication:
/var/log/auth.log or journalctlpamtesterFor additional help, create an issue in the project repository with: