Skip to content

Instantly share code, notes, and snippets.

@medigeek
Created February 22, 2012 00:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save medigeek/1880253 to your computer and use it in GitHub Desktop.
Save medigeek/1880253 to your computer and use it in GitHub Desktop.
Idea for timekpr
#!/usr/bin/python
# sudo ./pamtest.py <username>
# If your run this as root, you don't need authentication.
# Perfect for True or False (if user is allowed to login or not)
import sys
import PAM
from getpass import getpass
def pam_conv(auth, query_list, userData):
resp = []
for i in range(len(query_list)):
query, type = query_list[i]
if type == PAM.PAM_PROMPT_ECHO_ON:
val = raw_input(query)
resp.append((val, 0))
elif type == PAM.PAM_PROMPT_ECHO_OFF:
val = getpass(query)
resp.append((val, 0))
elif type == PAM.PAM_PROMPT_ERROR_MSG or type == PAM.PAM_PROMPT_TEXT_INFO:
print query
resp.append(('', 0))
else:
return None
return resp
service = 'login'
if len(sys.argv) == 2:
user = sys.argv[1]
else:
#user = None
exit("Usage: ./pamtest.py <username>")
auth = PAM.pam()
auth.start(service)
if user != None:
auth.set_item(PAM.PAM_USER, user)
auth.set_item(PAM.PAM_CONV, pam_conv)
try:
#auth.authenticate()
auth.acct_mgmt()
except PAM.error, resp:
print 'Go away! (%s)' % resp
except:
print 'Internal error'
else:
print 'Good to go!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment