This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset_password.py
97 lines (80 loc) · 3.47 KB
/
reset_password.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/python
import sys
sys.path.append("MemberDB")
import MemberDatabase
import Role
import Group
import Member
import helper
from optparse import OptionParser
import smtplib
from email.mime.text import MIMEText
import errno
import socket
mdb = MemberDatabase.MemberDatabase(helper.ldapcfg, helper.dbcfg, helper.logger)
l,s = mdb.get_connectors()
if __name__ == "__main__":
# Parse arguments
usage = """./reset_password.py <lidnummer> <passwordType> or ./resetpassword.py <username> <passwordType>
Password types are as follows:
0 -> 8 characters, uppercase, lowercase, digits, special
1 -> 9 characters, uppercase, lowercase
2 -> 11 characters, lowercase
3 -> 5 random Dutch words, lowercase"""
parser = OptionParser(usage)
(options, args) = parser.parse_args()
if len(args) != 2:
parser.error("I require two arguments.")
numberOrName = str(args[0])
try:
passwordType = int(str(args[1]))
except ValueError:
parser.error("passwordType should be a digit.")
# Check validity of arguments
if passwordType < 0 or passwordType > 3:
parser.error("passwordType should be 0, 1, 2 or 3.")
if Member.is_valid_lidnummer(numberOrName):
lidnummer = numberOrName
member = Member.Member(l,s,int(lidnummer))
if not member.exists():
logger.error("Could not find lidnummer in MemberDatabase. Aborting...")
sys.exit()
elif not member.is_user():
logger.error("Member is not a user. Promote him to user to set a password.")
sys.exit()
username = member.get_username()
elif Member.is_valid_username(numberOrName):
username = numberOrName
try:
member = Member.Member.from_username(l,s,username)
except Member.UsernameError:
logger.error("Could not find username in MemberDatabase. Aborting...")
sys.exit()
else:
parser.error("This is neither a valid lidnummer, nor a valid username. Aborting...")
# Calculate and set new password
fullName = member.get_full_name()
mail = member.get_mail()
password = member.generate_password(passwordType)
helper.logger.debug("Setting password for username %s (%s) to newly generated value." % (username, fullName))
member.set_password(password)
# Send new password to user
msg = MIMEText("""Beste %s,
Er is een nieuw wachtwoord ingesteld voor je MijnJD-account. Je inloggegevens vind je hieronder. Bewaar ze goed, en houd ze geheim. Als je je wachtwoord nog eens vergeet, kan de Algemeen Secretaris van het Landelijk Bestuur je een nieuw wachtwoord geven.
Gebruikersnaam: %s
Wachtwoord: %s
Als je nog vragen hebt over dit systeem, neem dan contact op met het ICT-team op [email protected].
Hartelijke groeten,
Het ICT-team""" % (fullName, username, password))
msg['Subject'] = "Wachtwoord-reset voor MijnJD-account"
msg['From'] = "Jonge Democraten ICT-team <[email protected]>"
msg['To'] = "%s <%s>" % (fullName, mail)
try:
sm = smtplib.SMTP(helper.mailcfg['host'])
sm.sendmail("[email protected]", mail, msg.as_string())
sm.quit()
except socket.error, v:
errorCode = v[0]
if errorCode == errno.ECONNREFUSED:
helper.logger.error("Could not send new password e-mail to %s: connection refused." % (fullName))
helper.logger.info("Performed a password reset for %s (%s), username %s " % (fullName, member.get_lidnummer(), username))