forked from sdgathman/pysrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envfrom2srs.py
78 lines (72 loc) · 2.08 KB
/
envfrom2srs.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
#!/usr/bin/python3
# sendmail program map for SRS
#
# Use only if absolutely necessary. It is *very* inefficient and
# a security risk.
#
# Copyright (c) 2004-2010 Business Management Systems. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Python itself.
import SRS
import re
try:
from configparser import ConfigParser, DuplicateSectionError
except:
from ConfigParser import ConfigParser, DuplicateSectionError
# get SRS parameters from milter configuration
cp = ConfigParser({
'secret': 'shhhh!',
'maxage': '8',
'hashlength': '8',
'fwdomain': 'mydomain.com',
'separator': '='
})
cp.read(["/etc/mail/pysrs.cfg"])
try:
cp.add_section('srs')
except DuplicateSectionError:
pass
srs = SRS.new(
secret=cp.get('srs','secret'),
maxage=cp.getint('srs','maxage'),
hashlength=cp.getint('srs','hashlength'),
separator=cp.get('srs','separator'),
alwaysrewrite=True # pysrs.m4 can skip calling us for local domains
)
fwdomain = cp.get('srs','fwdomain')
del cp
# Our original envelope-from may look funny on entry
# of this Ruleset:
#
# admin<@asarian-host.net.>
#
# We need to preprocess it some:
def forward(old_address):
if old_address == '<@>':
return old_address
use_address = re.compile(r'[<>]').sub('',old_address)
use_address = re.compile(r'\.$').sub('',use_address)
# Ok, first check whether we already have a signed SRS address;
# if so, just return the old address: we do not want to double-sign
# by accident!
#
# Else, gimme a valid SRS signed address, munge it back the way
# sendmail wants it at this point; or just return the old address,
# in case nothing went.
try:
new_address = srs.reverse(use_address)
return old_address
except:
try:
new_address = srs.forward(use_address,fwdomain)
return new_address.replace('@','<@',1)+'.>'
except:
return old_address
if __name__ == "__main__":
import sys
# No funny business in our output, please
sys.stderr.close()
if len(sys.argv) > 2:
fwdomain = sys.argv[-2]
print(forward(sys.argv[-1]))