forked from linsomniac/tumgreyspf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tumgreyspf-stat
executable file
·95 lines (82 loc) · 2.53 KB
/
tumgreyspf-stat
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
#!/usr/bin/env python
#
# Display information about the entries in the greylist.
#
# Copyright (c) 2004-2007, Sean Reifschneider, tummy.com, ltd.
# All Rights Reserved
import os, re, string, syslog, sys, time
sys.path.append('/usr/local/lib/tumgreyspf')
import tumgreyspfsupp
###################
def syslogprint(s):
print s
syslog.syslog = syslogprint
##################################
def visit(config, dirname, fileList):
ospathisfile = os.path.isfile
ospathjoin = os.path.join
base = config['greylistBasedir']
rx = re.compile(r'^/?(\d+)/(\d+)/(\d+)/(\d+)/greylist/(.*)$')
if config['ignoreLastByte'] > 0:
rx = re.compile(r'^/?(\d+)/(\d+)/(\d+)/greylist/(.*)$')
didUnlink = 0
for file in fileList:
path = ospathjoin(dirname, file)
if not ospathisfile(path): continue
recipient = file
relative = dirname[len(base):]
m = rx.match(relative)
if not m:
print 'Unknown path "%s" found in greylist directory.' % relative
continue
ip = string.join(m.groups()[:-1], '.')
sender = m.groups()[-1]
# look up expration day
data = {
'envelope_sender' : tumgreyspfsupp.unquoteAddress(sender),
'envelope_recipient' : tumgreyspfsupp.unquoteAddress(recipient),
'client_address' : ip,
}
configData = tumgreyspfsupp.lookupConfig(config.get('configPath'),
data, config.copy())
expireTime = time.time() - (configData['GREYLISTEXPIREDAYS'] * 86400)
# check
statData = os.stat(path)
mtime = statData[8]
ctime = statData[9]
now = time.time()
# status information
stats = ''
if ctime < mtime:
stats = stats + 'Blocked,'
if mtime > now:
stats = stats + 'Pending,'
if stats:
stats = ' (%s)' % stats[:-1]
def prettyseconds(seconds):
for prettySeconds, prettyDescription in (
( 86400, 'd' ),
( 3600, 'h' ),
( 60, 'm' ),
):
if seconds > prettySeconds:
return('%d%s' % ( seconds / prettySeconds, prettyDescription ))
return('%ss' % seconds)
# display information
print ('IP=%s SENDER=%s RECIPIENT=%s STARTS=%s LAST=%s EXPIRESIN=%s%s'
% ( ip, sender, recipient, prettyseconds(now - mtime),
prettyseconds(now - ctime),
prettyseconds(int(expireTime - now)), stats ))
############################
# main code
config = tumgreyspfsupp.processConfigFile()
greylistBasedir = os.path.join(config['greylistDir'], 'client_address')
config['greylistBasedir'] = greylistBasedir
try:
os.path.walk(greylistBasedir, visit, config)
# ignore interrupts and errors writing stdout
except IOError, e:
if e.errno != 32: raise
except KeyboardInterrupt:
pass