posix -> os
[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] == "echelon":
87    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,\
88    "(&(|(activity-pgp=*)(activity-from=*))(&(keyfingerprint=*)(gidnumber=800)))",\
89            ["activity-pgp","activity-from"]);
90    Count = 0;
91    PGPCount = 0;
92    for x in Attrs:
93       Count = Count + 1;
94       if x[1].has_key("activity-pgp"):
95          PGPCount = PGPCount + 1;
96    print "Echelon has seen",Count,"developers, with",PGPCount,"PGP confirms as of",time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(time.time()));
97
98 if arguments[0] == "keystat":
99    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyfingerprint=*",\
100            ["keyfingerprint"]);
101    KeyCount = 0;
102    GPGCount = 0;
103    for x in Attrs:
104       if x[1].has_key("keyfingerprint"):
105          KeyCount = KeyCount + 1;
106          for I in x[1]["keyfingerprint"]:
107            if len(I) == 40:
108               GPGCount = GPGCount + 1;
109               break;
110    print "There are",KeyCount,"accounts with PGP2/5 keys and",GPGCount,"of them have PGP5 keys";
111
112 if arguments[0] == "multikeys":
113    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
114            ["uid","cn","sn","keyfingerprint"]);
115    Attrs.sort();
116    
117    
118    print "--- PGP Keys ---"
119    ShowDups(Attrs,32);
120    print "--- GPG Keys ---"
121    ShowDups(Attrs,40);
122