-
Notifications
You must be signed in to change notification settings - Fork 6
/
ppolicy.tap
153 lines (137 loc) · 5.33 KB
/
ppolicy.tap
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# TAP start file
#
# Copyright (c) 2005 JAS
#
# Author: Petr Vokac <[email protected]>
#
# $Id$
#
import os, sys, imp
import signal
import socket
import logging
from ppolicy.log import TwistedHandler
from ppolicy.protocol import PPolicyFactory, CommandFactory
from twisted.application import internet, service
from twisted.internet import reactor, protocol
# default config
config = {
# 'basePath' : '/path/to/root/of/ppolicy', # tar xcf ppolicy-2.x.tar.gz
'configFile' : '/etc/postfix/ppolicy.conf',
'stateFile' : '/etc/postfix/ppolicy.state',
'logLevel' : logging.WARN,
'usePsyco' : True,
'admin' : 'postmaster',
'domain' : socket.gethostname(),
'databaseAPI' : 'MySQLdb',
'database' : { 'host' : 'localhost',
'port' : 3306,
'db' : 'ppolicy',
'user' : 'ppolicy',
'passwd' : 'secret',
'cp_min' : 3, # connection pool min conns
'cp_max' : 5, # connection pool max conns
'cp_noisy': 0, # noisy connection pool logging
},
'commandPort' : 10030,
'ppolicyPort' : 10031,
'cacheEngine' : 'local',
'cacheSize' : 10000,
'cacheServers' : [ '127.0.0.1:11211' ],
'connLimit' : 100,
'returnOnConnLimit': ('450', 'reached connection limit to ppolicy, retry later'),
'returnOnFatalError': ('450', 'fatal error when checking SMTP data, retry later'),
'check' : lambda x, y, z: ('dunno', ''),
'modules' : {},
}
# logging
twistedHandler = TwistedHandler()
#twistedHandler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s](%(module)s:%(lineno)d) %(message)s", "%d %b %H:%M:%S"))
twistedHandler.setFormatter(logging.Formatter("[%(relativeCreated)9.0f][%(levelname)s](%(module)s:%(lineno)d) %(message)s"))
logging.getLogger().addHandler(twistedHandler)
logging.getLogger().setLevel(config['logLevel'])
logging.getLogger().info("command: %s" % " ".join(sys.argv))
logging.getLogger().info("version: 2.7.0-0beta22")
# import configuration file
if not os.path.isfile(config['configFile']):
msg = "Configuration file %s doesn't exist" % config['configFile']
logging.getLogger().error(msg)
sys.stderr.write("%s\n" % msg)
sys.exit(1)
if config.get('basePath') != None:
if not os.path.isdir(config['basePath']) or not os.path.isdir("%s/ppolicy" % config['basePath']):
msg = "Invalid base path %s (doesn't contain ppolicy modules)" % config['basePath']
logging.getLogger().error(msg)
sys.stderr.write("%s\n" % msg)
sys.exit(1)
sys.path.append(config['basePath'])
def read_config(signum, frame):
if signum != 0:
msg = "Configuration reloading is not yet supported"
logging.getLogger().error(msg)
sys.stderr.write("%s\n" % msg)
return
try:
if not os.path.isfile(config['configFile']):
logging.getLogger().error("Configuration file %s doesn't exist" % config['configFile'])
return
ppolicy_config = imp.load_source("ppolicy_config", config['configFile'])
except ImportError, err:
msg = "Error importing config file %s: %s" % (config['configFile'], err)
logging.getLogger().error(msg)
sys.stderr.write("%s\n" % msg)
sys.exit(1)
except Exception, err:
msg = "Error loading config: %s" % err
logging.getLogger().error(msg)
sys.stderr.write("%s\n" % msg)
sys.exit(1)
# get configuration from config file
logging.getLogger().info("reading configuration from %s" % config['configFile'])
for key in dir(ppolicy_config):
if key[:2] == '__': continue
val = getattr(ppolicy_config, key)
if type(val).__name__ == 'module': continue
if config.has_key(key):
config[key] = val
else:
logging.getLogger().warn("Unknown configuration option: %s" % key)
logging.getLogger().setLevel(config['logLevel'])
logging.getLogger().info("current configuration: %s" % config)
# reload configuration on SIGHUP
signal.signal(signal.SIGHUP, read_config)
# read initial configuration
read_config(0, None)
if config.get('usePsyco', True):
# Import Psyco if available
try:
import psyco
psyco.full()
logging.getLogger().warn("Using Psyco to optimize python code")
except ImportError:
logging.getLogger().warn("Psyco is not available")
pass
# start twisted application
reactor.suggestThreadPoolSize(40)
# Create a MultiService
multiService = service.MultiService()
# Create PPolicy service
ppolicyFactory = PPolicyFactory(config)
if type(config['ppolicyPort']) not in [ type([]), type(()) ]:
ppolicyPort = [ config['ppolicyPort'] ]
else:
ppolicyPort = config['ppolicyPort']
for port in ppolicyPort:
ppolicyService = internet.TCPServer(port, ppolicyFactory)
ppolicyService.setServiceParent(multiService)
# Create command service
commandFactory = CommandFactory(ppolicyFactory)
commandService = internet.TCPServer(config['commandPort'], commandFactory)
commandService.setServiceParent(multiService)
# Create an application
application = service.Application("PPolicyServer")
# Connect MultiService to the application
multiService.setServiceParent(application)