Skip to main content

Python PAM module using ctypes, py3

Project description


Python pam module supporting py3 (and py2) for Linux type systems (!windows)

Commandline example:

[david@Scott python-pam]$ python pam/
Username: david
Auth result: Success (0)
Pam Environment List item: XDG_SEAT=seat0
Pam Environment item: XDG_SEAT=seat0
Missing Pam Environment item: asdf=None
Open session: Success (0)
Close session: Success (0)

Inline examples:

[david@Scott python-pam]$ python
Python 3.9.7 (default, Oct 10 2021, 15:13:22)
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pam
>>> p = pam.authenticate()
>>> p.authenticate('david', 'correctpassword')
>>> p.authenticate('david', 'badpassword')
>>> p.authenticate('david', 'correctpassword', service='login')
>>> p.authenticate('david', 'correctpassword', service='unknownservice')
>>> p.authenticate('david', 'correctpassword', service='login', resetcreds=True)
>>> p.authenticate('david', 'correctpassword', encoding='latin-1')
>>> print('{} {}'.format(p.code, p.reason))
0 Success
>>> p.authenticate('david', 'badpassword')
>>> print('{} {}'.format(p.code, p.reason))
7 Authentication failure

Authentication and privileges

Please note, python-pam and all tools that do authentication follow two rules:

  • You have root (or privileged access): you can check any account's password for validity
  • You don't have root: you can only check the validity of the username running the tool

If you need to authenticate multiple users, you must use an authentication stack that at some stage has privileged access. On Linux systems one example of doing this is using SSSD.

Typical Linux installations check against /etc/shadow with which will spawn /usr/bin/unix_chkpwd to verify the password. Both of these are intentionally written to meet the above two rules. You can test the functionality of unix_chkpwd in the following manner:

Replace good with the correct password, replace david with your appropriate username.

~$ mkfifo /tmp/myfifo

~$ (echo -ne 'good\0' > /tmp/myfifo & /usr/bin/unix_chkpwd david nullok < /tmp/myfifo ) ; echo $?

~$ (echo -ne 'bad\0' > /tmp/myfifo & /usr/bin/unix_chkpwd david nullok < /tmp/myfifo ) ; echo $?

~$ (echo -ne 'good\0' > /tmp/myfifo & /usr/bin/unix_chkpwd someotheruser nullok < /tmp/myfifo ) ; echo $?

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-pam-2.0.2.tar.gz (11.4 kB view hashes)

Uploaded source

Built Distribution

python_pam-2.0.2-py3-none-any.whl (10.7 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page