Add password checking via a python wrapper
[mirror/userdir-ldap-cgi.git] / password-qualify-check
1 #!/usr/bin/python
2
3 # check password quality using cracklib given a new password, optionally the
4 # old password, and a list of ldap/gecos words via stdin, each on a line by
5 # itself (send an empty line if you want to skip the old password check)
6
7 # Copyright (c) 2008 Peter Palfrader
8
9 import crack, sys, tempfile, os
10
11 def cleanup(dir):
12   if not dir.startswith('/tmp/pwcheck-'):
13     raise ValueError, 'cleanup got a weird dir to remove: '+dir
14   for f in 'dict.hwm dict.pwd dict.pwi wordlist wordlist-cleaned'.split(' '):
15     p = dir+'/'+f
16     if os.path.exists(p):
17       os.remove(p)
18   if os.path.exists(dir):
19     os.rmdir(dir)
20
21
22
23 newpass = sys.stdin.readline().strip()
24 oldpass = sys.stdin.readline().strip()
25 ldapwords = map( lambda x: x.strip(), sys.stdin.readlines())
26
27 if oldpass == "":
28   oldpass = None
29
30
31 crack.min_length = 11
32
33 # check against the default dictionary
34 try:
35   crack.VeryFascistCheck(newpass, oldpass)
36 except ValueError, e:
37   print e
38   sys.exit(1)
39
40 # and against a dictionary created from the ldap info on this user
41 if len(ldapwords) > 0:
42   tmpdir = tempfile.mkdtemp('', 'pwcheck-')
43   F = open(tmpdir+'/wordlist', "w")
44   for w in ldapwords:
45     F.write(w+"\n");
46   for w1 in ldapwords:
47     for w2 in ldapwords:
48       F.write(w1+w2+"\n");
49       F.write(w1[0]+w2+"\n");
50   F.close()
51
52   r = os.system("/usr/sbin/crack_mkdict "+tmpdir+"/wordlist > "+tmpdir+"/wordlist-cleaned")
53   if r != 0:
54     print "crack_mkdict returned non-zero exit status %d."%(r)
55     cleanup(tmpdir)
56     sys.exit(1)
57   r = os.system("/usr/sbin/crack_packer "+tmpdir+"/dict < "+tmpdir+"/wordlist-cleaned > /dev/null")
58   if r != 0:
59     print "crack_packer returned non-zero exit status %d."%(r)
60     cleanup(tmpdir)
61     sys.exit(1)
62
63   try:
64     crack.VeryFascistCheck(newpass, None, tmpdir+"/dict")
65   except ValueError, e:
66     print "ldap data based check: "+str(e)
67     cleanup(tmpdir)
68     sys.exit(1)
69
70   cleanup(tmpdir)
71
72 sys.exit(0)