-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathiredapd.py
104 lines (84 loc) · 3.23 KB
/
iredapd.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
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
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
import os
import sys
import pwd
import asyncore
# Always remove 'settings.pyc'.
_pyc = os.path.abspath(os.path.dirname(__file__)) + '/settings.pyc'
if os.path.exists(_pyc):
try:
os.remove(_pyc)
except:
pass
del _pyc
# Import config file (settings.py) and modules
import settings
from libs import __version__, daemon, utils
from libs.channel import DaemonSocket
from libs.logger import logger
# Plugin directory.
plugin_dir = os.path.abspath(os.path.dirname(__file__)) + '/plugins'
sys.path.append(plugin_dir)
if settings.backend not in ['ldap', 'mysql', 'pgsql']:
sys.exit("Invalid backend, it must be ldap, mysql or pgsql.")
def main():
# Set umask.
os.umask(0o077)
# Establish SQL database connections.
db_conns = utils.get_required_db_conns()
# Initialize policy daemon.
logger.info("Starting iRedAPD (version: {}, backend: {}), "
"listening on {}:{}.".format(
__version__, settings.backend,
settings.listen_address, settings.listen_port))
local_addr = (settings.listen_address, int(settings.listen_port))
DaemonSocket(local_addr=local_addr,
db_conns=db_conns,
policy_channel='policy',
plugins=settings.plugins)
if (settings.srs_secrets and settings.srs_domain):
logger.info("Starting SRS sender rewriting channel, listening on "
"{}:{}".format(settings.listen_address, settings.srs_forward_port))
local_addr = (settings.listen_address, int(settings.srs_forward_port))
DaemonSocket(local_addr=local_addr,
db_conns=db_conns,
policy_channel='srs_sender')
logger.info("Starting SRS recipient rewriting channel, listening on "
"{}:{}".format(settings.listen_address, settings.srs_reverse_port))
local_addr = (settings.listen_address, int(settings.srs_reverse_port))
DaemonSocket(local_addr=local_addr,
db_conns=db_conns,
policy_channel='srs_recipient')
else:
logger.info("No SRS domain and/or secret strings in settings.py, not loaded.")
# Run this program as daemon.
if '--foreground' not in sys.argv:
try:
daemon.daemonize(no_close=True)
except Exception as e:
logger.error("Error in daemon.daemonize: {}".format(repr(e)))
# Write pid number into pid file.
f = open(settings.pid_file, 'w')
f.write(str(os.getpid()))
f.close()
# Get uid/gid of daemon user.
p = pwd.getpwnam(settings.run_as_user)
uid = p.pw_uid
gid = p.pw_gid
# Run as daemon user
os.setgid(gid)
os.setuid(uid)
# Starting loop.
try:
if sys.version_info >= (3, 4):
# workaround for the "Bad file descriptor" issue on Python 2.7, gh-161
asyncore.loop(use_poll=True)
else:
# fixes the "Unexpected communication problem" issue on Python 2.6 and 3.0
asyncore.loop(use_poll=False)
except KeyboardInterrupt:
pass
except Exception as e:
logger.error("Error in asyncore.loop: {}".format(repr(e)))
if __name__ == '__main__':
main()