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)
7 # Copyright (c) 2008 Peter Palfrader
9 import sys, tempfile, os
11 import crack as cracklib
16 if not dir.startswith('/tmp/pwcheck-'):
17 raise ValueError, 'cleanup got a weird dir to remove: '+dir
18 for f in 'dict.hwm dict.pwd dict.pwi wordlist wordlist-cleaned'.split(' '):
22 if os.path.exists(dir):
29 for b in "/usr/sbin/crack_mkdict /usr/sbin/cracklib-format".split(' '):
33 for b in "/usr/sbin/crack_packer /usr/sbin/cracklib-packer".split(' '):
37 if crack_mkdict is None or crack_packer is None:
38 print "Could not find crack formater or packer"
42 newpass = sys.stdin.readline().strip()
43 oldpass = sys.stdin.readline().strip()
44 ldapwords = map( lambda x: x.strip(), sys.stdin.readlines())
50 cracklib.min_length = 11
52 # check against the default dictionary
54 cracklib.VeryFascistCheck(newpass, oldpass, '/var/cache/cracklib/cracklib_dict')
59 # and against a dictionary created from the ldap info on this user
60 if len(ldapwords) > 0:
61 tmpdir = tempfile.mkdtemp('', 'pwcheck-')
62 F = open(tmpdir+'/wordlist', "w")
68 F.write(w1[0]+w2+"\n");
71 r = os.system(crack_mkdict+" "+tmpdir+"/wordlist > "+tmpdir+"/wordlist-cleaned")
73 print "crack_mkdict returned non-zero exit status %d."%(r)
76 r = os.system(crack_packer+" "+tmpdir+"/dict < "+tmpdir+"/wordlist-cleaned > /dev/null")
78 print "crack_packer returned non-zero exit status %d."%(r)
83 cracklib.VeryFascistCheck(newpass, None, tmpdir+"/dict")
85 print "ldap data based check: "+str(e)