Sync with -common tree again
[mirror/userdir-ldap.git] / ud-gpgsigfetch
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3
4 import re, time, ldap, getopt, sys, pwd, os;
5 from userdir_gpg import *;
6 Output = "extrakeys.gpg";
7
8 # Process options
9 AdminUser = pwd.getpwuid(os.getuid())[0];
10 (options, arguments) = getopt.getopt(sys.argv[1:], "o:")
11 for (switch, val) in options:
12    if (switch == '-o'):
13       Output = val
14
15 if len(arguments) == 0:
16    print "Give some keyrings to probe";
17    os.exit(0);
18
19 # Popen GPG with the correct magic special options
20 Args = [GPGPath] + GPGBasicOptions;
21 for x in arguments:
22    Args.append("--keyring");
23    if x.find("/") == -1:
24       Args.append("./"+x);
25    else:
26       Args.append(x);
27 Args.append("--fast-list-mode");
28 Args.append("--list-sigs");
29 Args = Args + GPGSearchOptions + [" 2> /dev/null"]
30 Keys = os.popen(" ".join(Args),"r");
31
32 # Loop over the GPG key file
33 HaveKeys = {};
34 NeedKeys = {};
35 print "Reading keys+sigs from keyring";
36 while(1):
37    Line = Keys.readline();
38    if Line == "":
39       break;
40    
41    Split = Line.split(":");
42    if len(Split) >= 8 and Split[0] == "pub":
43       HaveKeys[Split[4]] = "";
44       continue;
45
46    if len(Split) >= 5 and Split[0] == "sig":
47       NeedKeys[Split[4]] = "";
48       continue;
49 Keys.close();
50
51 # Popen GPG with the correct magic special options
52 Args = [GPGPath] + GPGBasicOptions;
53 for x in [Output]:
54    Args.append("--keyring");
55    if x.find("/") == -1:
56       Args.append("./"+x);
57    else:
58       Args.append(x);
59 OldArgs = Args;      
60 Args = Args + GPGSearchOptions + [" 2> /dev/null"]
61 Keys = os.popen(" ".join(Args),"r");
62
63 print "Reading keys from output ring";
64 while(1):
65    Line = Keys.readline();
66    if Line == "":
67       break;
68    
69    Split = Line.split(":");
70    if len(Split) >= 8 and Split[0] == "pub":
71       HaveKeys[Split[4]] = "";
72       continue;
73 Keys.close();
74
75 KeysToFetch = [];
76 for x in NeedKeys.keys():
77    if not HaveKeys.has_key(x):
78       KeysToFetch.append("0x"+x);
79
80 print "Have %u keys and %u sigs, need %u keys"%(len(HaveKeys),len(NeedKeys),len(KeysToFetch));
81
82 Args = OldArgs;
83 Args.append("--keyserver 18.43.0.48");
84 Args.append("--recv-keys");
85 I = len(KeysToFetch);
86 while (I > 0):
87    OldI = I;
88    I = I - 20;
89    if I < 0: I = 0;
90    print " ".join(Args+KeysToFetch[I:OldI])
91    Fetcher = os.popen(" ".join(Args+KeysToFetch[I:OldI]),"r");
92    while(1):
93       Line = Fetcher.readline();
94       if Line == "":
95          break;
96       print Line;
97    Fetcher.close();