Ralnoc / pam-python

pam_python is a PAM module that runs the Python interpreter, and so allows PAM modules to be written in Python.
https://sourceforge.net/p/pam-python/code/ci/default/tree/
22 stars 10 forks source link

PAM module in virtualenv #2

Open mrvanes opened 6 years ago

mrvanes commented 6 years ago

What would the PAM configuration line look like if the python PAM module is developed in a virtualenv?

mrvanes commented 6 years ago

To answer my own question: auth optional pam_env.so envfile=/lib/security/pam_websso/env where /lib/security/pam_websso/env contains a single line PATH=/lib/security/pam_websso/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin seems to do the trick

Or, add #!/usr/bin/env /lib/security/pam_websso/bin/python at the top of the module

mrvanes commented 6 years ago

It turns out I was testing (pamtester) from an activated virtualenv which was sufficient to let pamtester succeed. It still fails from a clean env using any combination of the above tests, so I still don't have a way to start the virtualenv from within pam-python. For now, I'll install the required modules system wide.

hmoffatt commented 4 years ago

It fails because your script is not being run as a program (using the interpreter specified by the #! line) - the python interpreter is actually embedded in this module.

You just need to add the virtualenv path to sys.path before importing the other modules. Something like this:

import os.path
import sys

activate_this = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'venv/bin/activate_this.py')
exec(compile(open(activate_this).read(), activate_this, 'exec'), dict(__file__=activate_this))
mrvanes commented 4 years ago

Wow, thx. It's been a while since I worked on this (the proof of concept module works) but I'll keep this in mind if and when the module will be deployed in the wild!