From da5aa67ac923f544be411208a5dd514988bf9f45 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Fri, 7 Oct 2005 04:43:12 +0000 Subject: [PATCH] - implemented change pop/imap password git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@136 a67ec6bc-e5d5-0310-a910-815c51eb3124 --- backend/changepoppassword.py | 59 ++++++++++++++++++++++++++++++++++++ backend/gvadm/DomainTools.py | 12 ++++++-- 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100755 backend/changepoppassword.py diff --git a/backend/changepoppassword.py b/backend/changepoppassword.py new file mode 100755 index 0000000..a9fe262 --- /dev/null +++ b/backend/changepoppassword.py @@ -0,0 +1,59 @@ +#!/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= --user= [--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) diff --git a/backend/gvadm/DomainTools.py b/backend/gvadm/DomainTools.py index 45c86f4..ceeba7c 100644 --- a/backend/gvadm/DomainTools.py +++ b/backend/gvadm/DomainTools.py @@ -143,12 +143,20 @@ Password: %(password)s""" % {'domain': self.domain, def hasPopUser(self, username): """Checks whether the specified POP3/IMAP user exists in the domain.""" - return ([user for user in domain.listPopUsers() if (user == username)]) + return ([user for user in self.listPopUsers() if (user == username)]) def updatePopPassword(self, username, password=PasswordTools.generate_password()): """Updates the password of the given POP3/IMAP user.""" if self.hasPopUser(username): - print("will update password of user %s to %s" % (username, password)) + crypted = PasswordTools.md5_crypt_password(password) + + cr = self.cnx.cursor() + cr.execute("UPDATE mailpasswd SET clear=%(clear)s, crypt=%(crypt)s WHERE id=%(user)s" % { + 'clear': psycopg.QuotedString(password), + 'crypt': psycopg.QuotedString(crypted), + 'user': psycopg.QuotedString(username)}) + self.cnx.commit() + print("updated password of user %s to %s" % (username, password)) else: raise InvalidPopUser(self, username)