File: tumgreyspf-clean

package info (click to toggle)
tumgreyspf 1.36-4.1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 352 kB
  • ctags: 143
  • sloc: python: 1,788; perl: 86; sh: 58; makefile: 45
file content (87 lines) | stat: -rwxr-xr-x 2,444 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
#
#  Walk through the greylist directory and clean out old entries, based on
#  the values in the config file.
#
#  Copyright (c) 2004-2007, Sean Reifschneider, tummy.com, ltd.
#  All Rights Reserved
#  <jafo@tummy.com>

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/(.*)$')
	rxIp = re.compile(r'^/?(\d+)/(\d+)/(\d+)/((\d+)/)?check_file')
	didUnlink = 0
	for file in fileList:
		path = ospathjoin(dirname, file)
		if not ospathisfile(path): continue

		recipient = file
		relative = path[len(base):]
		if configGlobal['greylistByIPOnly'] > 0:
			m = rxIp.match(relative)
		else:
			m = rx.match(relative)
		if not m:
			print 'Unknown path "%s" found in greylist directory.' % relative
			continue

		#  get IP information
		groups = m.groups()
		ipList = list(groups[:3])
		if groups[3] != None: ipList.append(groups[4])
		else: ipList.append('0')

		ip = string.join(ipList, '.')
		sender = groups[5]

		#  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 for expiration
		statData = os.stat(path)
		mtime = statData[8]
		ctime = statData[9]
		expiredAuth = ctime < mtime and (time.time() - mtime) > (12 * 3600)
		expiredAfterAuth = ctime < expireTime

		#  check for expiration
		if expiredAuth or expiredAfterAuth:
			os.remove(path)
			didUnlink = 1

	#  remove this directory and it's parents if empty
	if didUnlink:
		newDirname = dirname
		while len(newDirname) > len(base):
			try: os.rmdir(newDirname)
			except OSError: break
			newDirname = os.path.dirname(newDirname)


############################
#  main code
config = tumgreyspfsupp.processConfigFile()
greylistBasedir = os.path.join(config['greylistDir'], 'client_address')
config['greylistBasedir'] = greylistBasedir
os.path.walk(greylistBasedir, visit, config)