-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsrs2envtol.py
69 lines (61 loc) · 1.75 KB
/
srs2envtol.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
#!/usr/bin/env 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',
'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')
)
srs.warn = lambda x: x # ignore case smash warning
del cp
def reverse(old_address):
# Munge ParseLocal recipient in the same manner as required
# in EnvFromSMTP.
use_address = re.compile(r'[<>]').sub('',old_address)
use_address = re.compile(r'\.$').sub('',use_address)
# Just try and reverse the address. If we succeed, return this
# new address; else, return the old address (quoted if it was
# a piped alias).
try:
use_address = srs.reverse(use_address)
while True:
try:
use_address = srs.reverse(use_address)
except: break
return use_address.replace('@','<@',1)+'.>'
except:
if use_address.startswith('|'):
return '"%s"' % old_address
else:
return old_address
if __name__ == "__main__":
import sys
# No funny business in our output, please
sys.stderr.close()
print(reverse(sys.argv[1]))