3 # This script tries to match key fingerprints from a keyring with user
4 # name in a directory. When an unassigned key is found a heuristic match
5 # against the keys given cn/sn and the directory is performed to try to get
6 # a matching. Generally this works about 90% of the time, matching is fairly
7 # strict. In the event a non-match a fuzzy sounds-alike search is performed
8 # and the results printed to aide the user.
10 # GPG is automatically invoked with the correct magic special options,
11 # pass the names of all the valid key rings on the command line.
13 # The output report will list what actions were taken. Keys that are present
14 # in the directory but not in the key ring will be removed from the
17 import string, re, time, ldap, getopt, sys, pwd, posix;
18 from userdir_gpg import *;
19 Output = "extrakeys.gpg";
22 AdminUser = pwd.getpwuid(posix.getuid())[0];
23 (options, arguments) = getopt.getopt(sys.argv[1:], "o:")
24 for (switch, val) in options:
27 elif (switch == '-m'):
29 elif (switch == '-a'):
32 if len(arguments) == 0:
33 print "Give some keyrings to probe";
36 # Popen GPG with the correct magic special options
37 Args = [GPGPath] + GPGBasicOptions;
39 Args.append("--keyring");
40 if string.find(x,"/") == -1:
44 Args.append("--list-sigs");
45 Args = Args + GPGSearchOptions + [" 2> /dev/null"]
46 print string.join(Args," ")
47 #Keys = os.popen(string.join(Args," "),"r");
48 Keys = os.popen("cat sigs","r");
50 # Loop over the GPG key file
53 print "Reading keys+sigs from keyring";
55 Line = Keys.readline();
59 Split = string.split(Line,":");
60 if len(Split) >= 8 and Split[0] == "pub":
61 HaveKeys[Split[4]] = "";
64 if len(Split) >= 5 and Split[0] == "sig":
65 NeedKeys[Split[4]] = "";
69 # Popen GPG with the correct magic special options
70 Args = [GPGPath] + GPGBasicOptions;
72 Args.append("--keyring");
73 if string.find(x,"/") == -1:
78 Args = Args + GPGSearchOptions + [" 2> /dev/null"]
79 Keys = os.popen(string.join(Args," "),"r");
81 print "Reading keys from output";
83 Line = Keys.readline();
87 Split = string.split(Line,":");
88 if len(Split) >= 8 and Split[0] == "pub":
89 HaveKeys[Split[4]] = "";
94 for x in NeedKeys.keys():
95 if not HaveKeys.has_key(x):
96 KeysToFetch.append("0x"+x);
98 print "Have %u keys and %u sigs, need %u keys"%(len(HaveKeys),len(NeedKeys),len(KeysToFetch));
101 Args.append("--keyserver 18.43.0.48");
102 Args.append("--recv-keys");
103 I = len(KeysToFetch);
108 print string.join(Args+KeysToFetch[I:OldI]," ")