PAMELA: Python PAM Interface

1.2.0 · active · verified Thu Apr 16

Pamela is a Python wrapper for Pluggable Authentication Modules (PAM) that utilizes `ctypes` to interface with the system's PAM libraries. It merges functionality from previously abandoned Python PAM projects (gnosek/python-pam and simplepam) to provide robust Python 3 support, raise informative `PamError` exceptions on failure, and manage PAM sessions. Maintained by Project Jupyter, it aims to offer a reliable and up-to-date solution for system authentication on Unix-like operating systems. The current version is 1.2.0, released in August 2024, indicating active development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `pamela.authenticate` to verify a user's credentials against the system's PAM configuration. It includes error handling for `PAMError` and prompts the user for their system password using `getpass`. The default PAM service 'login' is used, and a commented-out example for a 'sudo' service is provided to illustrate flexibility.

import getpass
from pamela import authenticate, PAMError

def verify_user_pam(username, password, service='login'):
    try:
        # The 'login' service is a common default, but can be changed.
        # On some systems, `auth` might also be a default.
        # A common test is `python -m pamela -a $(whoami)`
        if authenticate(username, password, service=service):
            print(f"Authentication successful for user: {username}")
            return True
        else:
            # authenticate returns False for simple failures (e.g., bad password)
            # but can also raise PAMError for more critical issues.
            print(f"Authentication failed for user: {username}.")
            return False
    except PAMError as e:
        print(f"PAM error during authentication for {username}: {e}")
        return False
    except Exception as e:
        print(f"An unexpected error occurred: {e}")
        return False

if __name__ == '__main__':
    user = getpass.getuser()
    pwd = getpass.getpass(f"Password for {user}: ")
    
    # Example with default 'login' service
    print("\n--- Testing 'login' service ---")
    verify_user_pam(user, pwd, service='login')
    
    # Example with a custom service, e.g., 'sudo' (requires appropriate PAM setup)
    # For this to work, a PAM configuration for 'sudo' might be needed, or ensure
    # the user has permissions for this service.
    # print("\n--- Testing 'sudo' service (if configured) ---")
    # verify_user_pam(user, pwd, service='sudo')

view raw JSON →