e92aa550ff1891c77848ba7b0cab19e0091b0a62
[mirror/userdir-ldap.git] / ud-killcrypt
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3
4 import string, re, time, ldap, getopt, sys, pwd, os, posix;
5 from userdir_ldap import *;
6
7 # Main program starts here
8 User = pwd.getpwuid(posix.getuid())[0];
9 BindUser = User;
10 (options, arguments) = getopt.getopt(sys.argv[1:], "au:")
11 for (switch, val) in options:
12     if (switch == '-u'):
13         User = val;
14     if (switch == '-a'):
15         Anon = 1;
16                               
17 # Connect to the ldap server
18 l = ldap.open(LDAPServer);
19 print "Accessing LDAP directory as '" + User + "'";
20 Password = getpass(User + "'s password: ");
21 UserDn = "uid=" + User + "," + BaseDn;
22 l.simple_bind_s(UserDn,Password);
23
24 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"userpassword=*",\
25          ["uid","cn","mn","sn","userpassword"]);
26
27 Attrs.sort();
28 for x in Attrs:
29     if x[1].has_key("userpassword") == 0: continue;
30     Passwd = x[1]["userpassword"][0];
31     if string.find(Passwd,"*LK*") != -1 or string.find(Passwd,"}*") != -1:
32         continue;
33     if len(Passwd) > 21: continue;
34     
35     print EmailAddress(x);
36
37     UserDn = "uid=" + x[1]["uid"][0] + "," + BaseDn;
38     Password = GenPass();
39     Pass = "{crypt}" + HashPass(Password);
40     l.modify_s(UserDn,[(ldap.MOD_REPLACE,"userpassword",Pass)]);