Moving away from string exceptions
[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 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 = connectLDAP()
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] == "missing":
99    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,\
100    "(&(!(|(activity-pgp=*)(activity-from=*)))(&(keyFingerPrint=*)(gidNumber=800)))",\
101            ["uid","cn","sn","mn"]);
102    Attrs.sort();
103    for x in Attrs:
104       print EmailAddress(x);
105
106 if arguments[0] == "keystat":
107    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyFingerPrint=*",\
108            ["keyFingerPrint"]);
109    KeyCount = 0;
110    GPGCount = 0;
111    for x in Attrs:
112       if x[1].has_key("keyFingerPrint"):
113          KeyCount = KeyCount + 1;
114          for I in x[1]["keyFingerPrint"]:
115            if len(I) == 40:
116               GPGCount = GPGCount + 1;
117               break;
118    print "There are",KeyCount,"accounts with PGP2/5 keys and",GPGCount,"of them have PGP5 keys";
119
120 if arguments[0] == "multikeys":
121    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
122            ["uid","cn","sn","keyFingerPrint"]);
123    Attrs.sort();
124    
125    
126    print "--- PGP Keys ---"
127    ShowDups(Attrs,32);
128    print "--- GPG Keys ---"
129    ShowDups(Attrs,40);
130