-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathupdate_static_lists.py
More file actions
368 lines (282 loc) · 11.9 KB
/
update_static_lists.py
File metadata and controls
368 lines (282 loc) · 11.9 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import argparse
import logging
from os.path import basename
from pathlib import Path
import re
from typing import Union
import config
import psycopg
from src import services
from src.db import get_db_pool
from src.sender import get_static_sender
logger = logging.getLogger(__name__)
dry_run = False
def process_senders(cursor: psycopg.Cursor, app_config: config.Config) -> None:
logger.debug("Clearing down old static sender data")
if not dry_run:
cursor.execute(
"""
TRUNCATE
senders_static
RESTART IDENTITY
"""
)
email_lists = [
("confirmlist", "accept"),
("allowlists", "accept"),
("whitelists", "accept"),
("rejectlists", "reject"),
("blacklists", "reject"),
("discardlists", "discard"),
]
for config_name, action in email_lists:
config_lists = app_config.get(config_name, [])
if isinstance(config_lists, str):
config_lists = [config_lists]
for list_name in config_lists:
logger.info("Processing list (type: %(type)s; file: %(file_name)s)", {
"type": action,
"file_name": list_name
})
source_name = basename(list_name)
add_email_sender_entries(cursor, list_name, action, source_name)
regex_lists = [
("allowregex", "accept"),
("whiteregex", "accept"),
("rejectregex", "reject"),
("blackregex", "reject"),
("discardregex", "discard"),
]
for config_name, action in regex_lists:
for list_name in app_config.get(config_name, []):
logger.info("Processing regex list (type: %(type)s; file: %(file_name)s)", {
"type": action,
"file_name": list_name
})
source_name = basename(list_name)
add_pattern_sender_entries(cursor, list_name, action, source_name)
def add_email_sender_entries(cursor, list_name: str, action: str, source_name: str) -> None:
try:
with open(list_name, "r") as f:
for entry in f:
add_sender_entry(cursor, entry.strip(), action, source_name)
except (FileNotFoundError, PermissionError) as e:
logger.warning("Skipping invalid email list %(filename)s (%(source)s): %(reason)s", {
"source": source_name,
"filename": list_name,
"reason": str(e)
})
def add_pattern_sender_entries(cursor, list_name: str, action: str, source_name: str) -> None:
try:
with open(list_name, "r") as f:
line_counter = 0
for entry in f:
line_counter += 1
try:
stripped_entry = entry.strip()
re.compile(stripped_entry)
add_sender_entry(cursor, stripped_entry, action, source_name, "P")
except re.error as e:
logger.warning("Skipping invalid entry on %(line_counter)d of %(filename)s (%(source)s): %(entry)s -- %(reason)s", {
"line_counter": line_counter,
"filename": list_name,
"source": source_name,
"entry": stripped_entry,
"reason": e.msg
})
except (FileNotFoundError, PermissionError) as e:
logger.error("Skipping invalid pattern list %(filename)s (%(source)s): %(reason)s", {
"source": source_name,
"filename": list_name,
"reason": str(e)
})
def add_sender_entry(cursor, sender: str, action: str, source_name: str, sender_type: str = "E", reference: str = None) -> None:
values = {
"sender": sender,
"action": action,
"source_name": source_name,
"type": sender_type,
"reference": reference,
}
logger.debug("Adding %(type)s entry for %(sender)s from %(source_name)s as %(action)s with %(reference)s", values)
if not dry_run:
cursor.execute(
"""
INSERT INTO senders_static
(sender, action, source, ref, type)
VALUES
(%(sender)s, %(action)s, %(source_name)s, %(reference)s, %(type)s)
ON CONFLICT (sender)
DO UPDATE SET action=%(action)s, source=%(source_name)s, ref=%(reference)s, type=%(type)s
""",
values
)
def process_in_progress(cursor: psycopg.Cursor, app_config: config.Config) -> None:
logger.debug("Clearing down old static stash data")
if not dry_run:
cursor.execute(
"""
TRUNCATE
stash_static
RESTART IDENTITY
"""
)
mail_cache_dir = app_config.get("mail_cache_dir", None)
if mail_cache_dir:
logger.info("Processing in-progress confirmations by scanning cache: %(cache_dir)s", {"cache_dir": mail_cache_dir})
process_cache_directory(cursor, mail_cache_dir)
def process_cache_directory(cursor: psycopg.Cursor, cache_dir: str) -> None:
senders = {}
cache_path = Path(cache_dir)
for entry in cache_path.iterdir():
if not entry.is_file():
continue
result = process_cache_file(str(entry))
if not result:
logger.warning("Could not process %(filename)s. Skipping", {"filename": str(entry)})
continue
(from_email, recipients, message) = result
reference = entry.name
if "@" not in from_email:
logger.warning("%(filename)s has no valid FROM. Probably an autogenerated message. Skipping", {"filename": str(entry)})
continue
if from_email not in senders:
this_sender = get_static_sender(from_email, cursor)
senders[from_email] = this_sender
else:
this_sender = senders[from_email]
if not dry_run:
this_sender.stash_message(message, recipients, reference)
def process_cache_file(filename: str) -> Union[False, tuple[str, list[str], str]]:
try:
with open(filename) as f:
message = f.read()
(headers, body) = message.split("\n\n", maxsplit=1)
sender_match = re.match(r"From ([^ ]+)", headers)
recipient_match = re.search(r"^X-Original-To: (.+)$", headers, re.MULTILINE | re.IGNORECASE)
if not sender_match or not recipient_match:
return False
return (sender_match[1], [recipient_match[1]], message)
except (FileNotFoundError, PermissionError):
return False
def process_challenges(cursor: psycopg.Cursor, app_config: config.Config) -> None:
logger.debug("Clearing down old challenge data")
if not dry_run:
cursor.execute(
"""
TRUNCATE
challenges
RESTART IDENTITY
"""
)
challenge_lists = [
("challengelists", "challenge"),
("nochallengelists", "ignore"),
]
for config_name, action in challenge_lists:
config_lists = app_config.get(config_name, [])
if isinstance(config_lists, str):
config_lists = [config_lists]
for list_name in config_lists:
logger.info("Processing challenge list (type: %(type)s; file: %(file_name)s)", {
"type": action,
"file_name": list_name
})
source_name = basename(list_name)
add_email_challenge_entries(cursor, list_name, action, source_name)
regex_lists = [
("challengeregex", "challenge"),
("nochallengeregex", "ignore"),
]
for config_name, action in regex_lists:
for list_name in app_config.get(config_name, []):
logger.info("Processing challenge regex list (type: %(type)s; file: %(file_name)s)", {
"type": action,
"file_name": list_name
})
source_name = basename(list_name)
add_pattern_challenge_entries(cursor, list_name, action, source_name)
def add_email_challenge_entries(cursor, list_name: str, action: str, source_name: str) -> None:
try:
with open(list_name, "r") as f:
for entry in f:
add_challenge_entry(cursor, entry.strip(), action, source_name)
except (FileNotFoundError, PermissionError) as e:
logger.warning("Skipping invalid email challenge list %(filename)s (%(source)s): %(reason)s", {
"source": source_name,
"filename": list_name,
"reason": str(e)
})
def add_pattern_challenge_entries(cursor, list_name: str, action: str, source_name: str) -> None:
try:
with open(list_name, "r") as f:
line_counter = 0
for entry in f:
line_counter += 1
try:
stripped_entry = entry.strip()
re.compile(stripped_entry)
add_challenge_entry(cursor, stripped_entry, action, source_name, "P")
except re.error as e:
logger.warning("Skipping invalid entry on %(line_counter)d of %(filename)s (%(source)s): %(entry)s -- %(reason)s", {
"line_counter": line_counter,
"filename": list_name,
"source": source_name,
"entry": stripped_entry,
"reason": e.msg
})
except (FileNotFoundError, PermissionError) as e:
logger.error("Skipping invalid pattern challenge list %(filename)s (%(source)s): %(reason)s", {
"source": source_name,
"filename": list_name,
"reason": str(e)
})
def add_challenge_entry(cursor, challenge: str, action: str, source_name: str, challenge_type: str = "E") -> None:
values = {
"challenge": challenge,
"action_to_take": action,
"source_name": source_name,
"challenge_type": challenge_type,
}
logger.debug("Adding %(challenge_type)s entry for %(challenge)s from %(source_name)s as challenge %(action_to_take)s", values)
if not dry_run:
cursor.execute(
"""
INSERT INTO challenges
(challenge, action_to_take, source, challenge_type)
VALUES
(%(challenge)s, %(action_to_take)s, %(source_name)s, %(challenge_type)s)
ON CONFLICT (challenge)
DO UPDATE SET action_to_take=%(action_to_take)s, source=%(source_name)s, challenge_type=%(challenge_type)s
""",
values
)
def main():
global dry_run
parser = argparse.ArgumentParser(
prog="update_static_lists",
description="Admin script to convert the file-based lists into the database"
)
parser.add_argument("-c", "--config-file", default="/etc/postconfirm.cfg", type=argparse.FileType())
parser.add_argument("-n", "--dry-run", action='store_true', help="Do not actually modify the data")
parser.add_argument("--skip-senders")
parser.add_argument("--skip-in-progress")
parser.add_argument("--skip-challenges")
args = parser.parse_args()
# Load the configuration
app_config = config.Config(args.config_file)
services["app_config"] = app_config
# Set up the root logger
logging.basicConfig(level=app_config.get('log.level', logging.WARNING))
dry_run = args.dry_run
with get_db_pool(app_config["db"], "db").connection() as connection:
with connection.cursor() as cursor:
if not args.skip_senders:
process_senders(cursor, app_config)
if not args.skip_in_progress:
process_in_progress(cursor, app_config)
if not args.skip_challenges:
process_challenges(cursor, app_config)
connection.commit()
if __name__ == "__main__":
main()