989912948644a3a7fa38d362bce68ec7b56c4fbf
[mirror/userdir-ldap.git] / ud-ldapshow
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3 # Show some reports from the ldap database
4 # Call with nokey to generate a missing key report
5 # Call with noforward to generate a missing .forward report
6
7 import string, re, time, ldap, getopt, sys;
8 from userdir_ldap import *;
9
10 def ShowDups(Attrs,Len):
11    for x in Attrs:
12       if x[1].has_key("keyfingerprint") == 0:
13          continue;
14          
15       Count = 0;
16       for I in x[1]["keyfingerprint"]:
17          if len(I) == Len:
18             Count = Count + 1;
19       if Count > 1:
20          for I in x[1]["keyfingerprint"]:
21            if len(I) == Len:
22               print "%s: %s" % (EmailAddress(x),I);
23
24 # Main program starts here
25 # Process options
26 (options, arguments) = getopt.getopt(sys.argv[1:], "")
27 for (switch, val) in options:
28    if (switch == '-a'):
29       DoAdd = 1;
30
31 print "Connecting to LDAP directory";
32
33 # Connect to the ldap server
34 l = ldap.open(LDAPServer);
35 l.simple_bind_s("","");
36
37 if arguments[0] == "nokey":
38    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"(!(keyfingerprint=*))",\
39            ["uid","cn","sn","emailforward","comment"]);
40    Attrs.sort();
41    for x in Attrs:
42       print "Key Missing:",EmailAddress(x);
43       if GetAttr(x,"emailforward") != "":
44          print "  ->",GetAttr(x,"emailforward");
45       if GetAttr(x,"comment") != "":
46          print "  :",GetAttr(x,"comment");
47
48 if arguments[0] == "noforward":
49    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"(!(emailforward=*))",\
50            ["uid","cn","sn","emailforward","comment"]);
51    Attrs.sort();
52    for x in Attrs:
53       print "No Forward:",EmailAddress(x);
54
55 if arguments[0] == "badpriv":
56    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"(&(!(keyfingerprint=*))(privatesub=*))",\
57            ["uid","cn","sn","privatesub"]);
58    Attrs.sort();
59    for x in Attrs:
60       print EmailAddress(x)+": "+GetAttr(x,"privatesub");
61
62 if arguments[0] == "nopriv":
63    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"(&(keyfingerprint=*)(!(privatesub=*)))",\
64            ["uid","cn","sn","privatesub"]);
65    Attrs.sort();
66    for x in Attrs:
67       print "  ",EmailAddress(x)+": "+GetAttr(x,"privatesub");
68
69 if arguments[0] == "keymap":
70    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
71            ["uid","cn","sn","keyfingerprint"]);
72    Attrs.sort();
73    for x in Attrs:
74       if x[1].has_key("keyfingerprint"):
75          for I in x[1]["keyfingerprint"]:
76            print "%s: %s" % (EmailAddress(x),I);
77
78 if arguments[0] == "devcount":
79    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"(&(keyfingerprint=*)(gidnumber=800))",\
80            ["uid"]);
81    Count = 0;
82    for x in Attrs:
83       Count = Count + 1;
84    print "There are",Count,"developers as of",time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(time.time()));
85
86 if arguments[0] == "keystat":
87    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyfingerprint=*",\
88            ["keyfingerprint"]);
89    KeyCount = 0;
90    GPGCount = 0;
91    for x in Attrs:
92       if x[1].has_key("keyfingerprint"):
93          KeyCount = KeyCount + 1;
94          for I in x[1]["keyfingerprint"]:
95            if len(I) == 40:
96               GPGCount = GPGCount + 1;
97               break;
98    print "There are",KeyCount,"accounts with PGP2/5 keys and",GPGCount,"of them have PGP5 keys";
99
100 if arguments[0] == "multikeys":
101    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
102            ["uid","cn","sn","keyfingerprint"]);
103    Attrs.sort();
104    
105    
106    print "--- PGP Keys ---"
107    ShowDups(Attrs,32);
108    print "--- GPG Keys ---"
109    ShowDups(Attrs,40);
110