1
0
Fork 0
gnuviechadmin-historic/backend/changepoppassword.py

60 lines
1.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python
import psycopg, getopt, sys
from gvadm import PasswordTools, DomainTools
def usage():
print """Usage information:
=====================
%(process)s -h|--help
- prints this help text
%(process)s --domain=<domain> --user=<user> [--password=<password>]
- updates the password of a pop user for the given domain
- if the optional password is ommitted a generated one is used
- the password is checked using cracklib
- if the password is too weak a generated one is used
""" % {'process': sys.argv[0]}
if __name__ == "__main__":
try:
options = getopt.getopt(sys.argv[1:], "h", ['help', 'password=',
'domain=', 'user='])
except getopt.GetoptError:
usage()
sys.exit(1)
if (not options[0] or
dict(options[0]).has_key('-h') or
dict(options[0]).has_key('--help') or
not dict(options[0]).has_key('--domain') or
not dict(options[0])['--domain'].strip() or
not dict(options[0]).has_key('--user') or
not dict(options[0])['--user'].strip()):
usage()
sys.exit(1)
# specify the domain
domain = None
try:
domain = DomainTools.Domain(dict(options[0])['--domain'])
except DomainTools.InvalidDomain, iv:
print iv
sys.exit(1)
username = dict(options[0])['--user']
if not domain.hasPopUser(username):
print "Domain doesn't have pop user", username
sys.exit(1)
# specify the password
password = None
if dict(options[0]).has_key('--password'):
password = PasswordTools.check_password(dict(options[0])['--password'])
if (password == None):
password = PasswordTools.generate_password()
domain.updatePopPassword(username, password)