-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixKeystore.py
124 lines (111 loc) · 4.33 KB
/
mixKeystore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python
import subprocess
import logging, logging.config
try:
from lamson.cron import getCronTab, CronEvent
except:
def getCronTab():
return None
from mixKey import *
from mixConfig import getRemailerConfig
_mixKeyStore = None
def getKeyStore():
global _mixKeyStore
if not _mixKeyStore:
_mixKeyStore = loadFreshKeystore()
return _mixKeyStore
def loadFreshKeystore():
_mixKeyStore = MixKeyStore()
# This assumes only one private key will be found in the file.
try:
f = open(getRemailerConfig()['filelocations']['secring.mix'], 'r')
except IOError:
f = open('../../' + getRemailerConfig()['filelocations']['secring.mix'], 'r')
privateKeyLines = f.readlines()
f.close()
_mixKeyStore.addKey(privateKeyLines, getRemailerConfig('remailerkeypassword'))
try:
f = open(getRemailerConfig()['filelocations']['pubring.mix'], 'r')
except IOError:
f = open('../../' + getRemailerConfig()['filelocations']['pubring.mix'], 'r')
lines = f.readlines()
f.close()
key = []
for l in lines:
l = l.strip()
if l == "-----End Mix Key-----":
key.append(l)
_mixKeyStore.addKey(key)
key = []
else:
key.append(l)
return _mixKeyStore
def refreshStats():
devnull = open("/dev/null", "w")
subprocess.call(["wget", getRemailerConfig('statsdirectory') + "mlist.txt"], stderr=devnull)
subprocess.call(["wget", getRemailerConfig('statsdirectory') + "rlist.txt"], stderr=devnull)
subprocess.call(["wget", getRemailerConfig('statsdirectory') + "pubring.mix"], stderr=devnull)
subprocess.call(["wget", getRemailerConfig('statsdirectory') + "pgp-all.asc"], stderr=devnull)
subprocess.call(["mv", "mlist.txt", "rlist.txt", "pubring.mix", "pgp-all.asc", "app/data/"])
devnull.close()
def refreshKeyStore():
global _mixKeyStore
_mixKeyStore = loadFreshKeystore()
class MixKeyStore:
keystore = {}
def __init__(self):
cron = getCronTab()
if cron:
cron.add(CronEvent(refreshStats, 0, range(0,24)))
else:
logging.debug("No Cron Object found, cannot add refresh stats cron job")
def addKey(self, keylines, passphrase=""):
key = parseMixKey(keylines, passphrase)
if key.KeyId in self.keystore:
if isinstance(self.keystore[key.KeyId], PublicMixKey) and isinstance(key, PrivateMixKey):
logging.info("Replacing Public Key " + key.KeyId + " in self.keystore with Private Key")
key.loadPublicCapabilities(self.keystore[key.KeyId])
self.keystore[key.KeyId] = key
else:
self.keystore[key.KeyId].loadPublicCapabilities(key)
logging.info("Loading Public Key Properties of " + key.KeyId + " into Private Key in self.keystore ")
else:
self.keystore[key.KeyId] = key
logging.info("Adding Key " + key.KeyId + " to self.keystore")
def getKey(self, keyid):
if keyid in self.keystore:
return self.keystore[keyid]
else:
return None
def getPublicKey(self, keyid):
if keyid in self.keystore and isinstance(self.keystore[keyid], PrivateMixKey):
return self.keystore[keyid].getPublicMixKey()
elif keyid in self.keystore and isinstance(self.keystore[keyid], PublicMixKey):
return self.keystore[keyid]
else:
return None
def getPrivateKey(self, keyid):
if keyid in self.keystore and isinstance(self.keystore[keyid], PrivateMixKey):
return self.keystore[keyid]
else:
return None
def listPrivateKeys(self):
keys = []
for k in self.keystore:
if isinstance(self.keystore[k], PrivateMixKey):
keys.append(self.keystore[k])
return keys
def listPublicKeys(self):
keys = []
for k in self.keystore:
if isinstance(self.keystore[k], PublicMixKey):
keys.append(self.keystore[k])
else:
keys.append(self.getPublicKey(k))
return keys
if __name__ == "__main__":
logging.config.fileConfig("../../config/test_logging.conf")
keys = getKeyStore().listPublicKeys()
keys.sort()
for k in keys:
print k.getMixKeyHeader()