-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpgfilter.py
executable file
·176 lines (137 loc) · 4.67 KB
/
gpgfilter.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/python
#
# This script is run by exim
# In Debian this scripts is run by user Debian-exim
#
import sys
from gpg2 import encrypt
import pprint
from subprocess import Popen,PIPE
from redmine import RedmineMail
# message is piped to this script from stdin
lines = sys.stdin.readlines()
log = open('/var/log/exim4/mainlog','a')
debug = False
if debug:
log.write('gpgfilter: running as user '+Popen(['whoami'],stdout=PIPE).communicate()[0])
# only filter emails from certain senders
mail = ''.join(lines)
def escape(content):
return content.replace("=C3=A4","ae").replace("=C3=BC","ue").replace("=C3=B6","oe").replace("=C3=84","Ae").replace("=C3=96","Oe").replace("=C3=9C","Ue").replace("=C3=9F","ss")
if mail.find('[email protected]') != -1 :
# strip unencrypted Redmine headers
dont_show = ['X-Mailer: ', 'X-Redmine-', 'Subject: ','List-Id: ', 'Content-Transfer-Encoding: ']
i = 0
# for all lines:
while (i < len(lines)):
removed = False
# for all field not to show:
for j in range(len(dont_show)):
# does current line contain the not to be shown field?
if lines[i].find(dont_show[j]) > -1:
# found: remove
if debug:
log.write('gpgfilter: removed line "'+lines[i].replace('\n','')+'"\n')
lines.remove( lines[i] )
removed = True
# also cut following lines, if they belong to current line
while (len(lines[i]) > 0 and lines[i][0] == ' '):
if debug:
log.write('gpgfilter: also removed line "'+lines[i].replace('\n','')+'"\n')
lines.remove( lines[i] )
break
# if current line was removed, i already points to next line
if not removed:
i += 1
# separate headers from content
for k in range(len(lines)):
# the first empty line marks the end of header
# and beginning of text sections
if lines[k] == '' or lines[k] == '\n':
break
# array -> text
headers = ''.join(lines[:k])
content = ''.join(lines[k+1:])
rmail = RedmineMail(lines[k+1:])
headers += "Subject: [Redmine] "+rmail.getTracker()+" #"+rmail.getTicketNumber()+"\n"
# headers += "Subject: [Redmine]\n"
headers += "Content-Transfer-Encoding: 8bit\n"
# encrypt content
#gpg = gnupg.GPG(gnupghome='/var/spool/exim4/.gnupg/', gpgbinary="/usr/bin/gpg2")
#gpg.encoding = 'utf-8'
# TODO: derive recipients from To,CC,BCC and Received:for headers
# recipients = ["[email protected]", "[email protected]"]
print message
return
#if debug:
# log.write('Available keys:\n'+pprint.pformat(gpg.list_keys())+'\n')
# log.write(content+'\n')
#content = str(gpg.encrypt(content, recipients, always_trust=True))
content = escape(content)
content = encrypt(content, recipients)
if debug:
log.write(content)
else:
log.write('gpgfilter: Message encrypted for '+','.join(recipients)+'\n')
# re-assemble headers and content
message = headers+'\n'+content
# output manipulated message
print message
elif mail.find('[email protected]') != -1 :
# strip unencrypted Gitlab headers
dont_show = ['X-Mailer: ', 'Subject: ','List-Id: ']
i = 0
# for all lines:
while (i < len(lines)):
removed = False
# for all field not to show:
for j in range(len(dont_show)):
# does current line contain the not to be shown field?
if lines[i].find(dont_show[j]) > -1:
# found: remove
if debug:
log.write('gpgfilter: removed line "'+lines[i].replace('\n','')+'"\n')
lines.remove( lines[i] )
removed = True
# also cut following lines, if they belong to current line
while (len(lines[i]) > 0 and lines[i][0] == ' '):
if debug:
log.write('gpgfilter: also removed line "'+lines[i].replace('\n','')+'"\n')
lines.remove( lines[i] )
break
# if current line was removed, i already points to next line
if not removed:
i += 1
# separate headers from content
for k in range(len(lines)):
# the first empty line marks the end of header
# and beginning of text sections
if lines[k] == '' or lines[k] == '\n':
break
# array -> text
headers = ''.join(lines[:k])
content = ''.join(lines[k+1:])
headers += 'Subject: [Gitlab]\n'
# encrypt content
gpg = gnupg.GPG(gnupghome='/var/spool/exim4/.gnupg/')
# TODO: derive recipients from To,CC,BCC and Received:for headers
# recipients = ["[email protected]", "[email protected]"]
print message
return
if debug:
log.write('Available keys:\n'+pprint.pformat(gpg.list_keys())+'\n')
log.write(content+'\n')
content = str(gpg.encrypt(content, recipients, always_trust=True))
if debug:
log.write(content)
else:
log.write('gpgfilter: message encrypted\n')
# re-assemble headers and content
message = headers+'\n'+content
# output manipulated message
print message
else:
print mail.strip()
log.write('gpgfilter: not encrypting\n')
exit(0)
log.close()