コード例 #1
0
ファイル: test_plugin_manager.py プロジェクト: abec/vsmtpd
class PluginManagerTestCase(TestCase):

    def setUp(self):
        plugins = os.path.join(os.path.dirname(__file__), 'pluginsdir')
        self.manager = PluginManager([plugins])

    def test_simple_valid_plugin(self):
        plugin_cls = self.manager.load('simple_valid_plugin')

    def test_simple_invalid_plugin(self):
        self.assertRaises(PluginNotFoundError, self.manager.load,
            'simple_invalid_plugin')

    def test_subdirectory_valid_plugin(self):
        self.manager.load('queue.simple_valid_plugin')

    def test_subdirectory_invalid_plugin(self):
        self.assertRaises(PluginNotFoundError, self.manager.load,
            'queue.simple_invalid_plugin')

    def test_missing_plugin(self):
        self.assertRaises(PluginNotFoundError, self.manager.load,
            'missing_pugin')

    def test_missing_subdirectory_plugin(self):
        self.assertRaises(PluginNotFoundError, self.manager.load,
            'queue.missing_pugin')
コード例 #2
0
ファイル: common.py プロジェクト: abec/vsmtpd
 def __init__(self, methodName='runTest'):
     super(PluginTestCase, self).__init__(methodName=methodName)
     dirname = os.path.dirname
     plugins = os.path.join(dirname(dirname(dirname(__file__))),
                            'plugins')
     manager = PluginManager([plugins])
     self.plugin = manager.load(self.plugin_name)
コード例 #3
0
ファイル: daemon.py プロジェクト: abec/vsmtpd
class Vsmtpd(object):

    def __init__(self, options, args):
        self.options = options
        self.args = args
        self.pool = None
        self.workers = []

        # Load the configuration for the server
        self.load_config()

        # If we positive connection limit create a Pool with that limit
        connection_limit = self.config.getint('connection_limit')
        if connection_limit > 0:
            self.pool = Pool(connection_limit)
            log.info('Limiting connections to %d', connection_limit)

        # Create the hook manager
        self.hook_manager = HookManager()

        # Create the plugin manager
        plugin_path = self.config.get('plugin_path').split(':')
        self.plugin_manager = PluginManager(plugin_path)

    def fire(self, hook_name, *args, **kwargs):
        return self.hook_manager.dispatch_hook(hook_name, *args, **kwargs)

    def handle(self, socket, address):
        connection = Connection(self, socket, address)
        connection.run_hooks('pre_connection', connection)
        connection.accept()
        connection.run_hooks('post_connection', connection)

    def load_config(self):
        self._config = load_config(self.options.config or 'vsmtpd.cfg', {
            'vsmtpd': {
                'port': 25,
                'interface': None,
                'backlog': 50,
                'workers': 0,
                'size_limit': 0,
                'helo_host': None,
                'connection_limit': 100,
                'spool_dir': '/var/spool/vsmtpd',
                'keyfile': None,
                'certfile': None,
                'cert_reqs': None,
                # FIXME: Provide a default secure (SSLV3/TLSV1) cipher setup
                'ssl_version': None,
                'ca_certs': None,
                'suppress_ragged_eofs': None,
                'do_handshake_on_connect': None,
                # FIXME: Provide a default secure (SSLV3/TLSV1) cipher setup
                'ciphers': None,
                'plugin_path': '/usr/share/vsmtpd/plugins'
            }
        })
        self.config = ConfigWrapper(self._config, 'vsmtpd')

    def load_plugins(self):
        log.info('Loading plugins...')
        # Load the plugins
        for section in self._config.sections():
            if not section.startswith('plugin:'):
                continue
            plugin_name = section.split(':', 1)[1]
            try:
                plugin_cls = self.plugin_manager.load(plugin_name)
            except Exception as e:
                log.fatal("Failed to load plugin '%s'", plugin_name)
                log.exception(e)
                exit(1)

            try:
                if self._config.options(section):
                    plugin = plugin_cls(ConfigWrapper(self._config, section))
                else:
                    plugin = plugin_cls()
                plugin.plugin_name = plugin_name
            except Exception as e:
                log.fatal("Failed to initialise plugin '%s'", plugin_name)
                log.exception(e)
                exit(1)

            self.hook_manager.register_object(plugin)

    def reload(self):
        """
        Reload the configuration.
        """

    def start(self):
        """
        Starts the vsmtpd server in either master or worker mode.
        """

        # Install the signal handlers
        signal.signal(signal.SIGTERM, self.stop)
        signal.signal(signal.SIGHUP, self.reload)
        signal.signal(signal.SIGINT, self.stop)

        workers = self.config.getint('workers')
        backlog = self.config.getint('backlog')

        addr = ('0.0.0.0', 2500)

        if backlog < 1:
            backlog = 50

        log.info('Starting server on %s port %d', *addr)

        if workers <= 0:
            set_cmdline('vsmtpd: master')
            self._start(addr, backlog)

        # Open the socket for master/worker operation.
        self.sock = socket.socket()
        self.sock.bind(addr)
        self.sock.listen(backlog)
        self.sock.setblocking(0)

        # Spawn the worker servers
        for i in xrange(0, workers):
            self._start_slave()

        # Set the process title
        set_cmdline('vsmtpd: master')

        # Wait for the children to die
        try:
            os.waitpid(-1, 0)
        except OSError:
            pass

    def _start(self, listener, backlog=None):
        """
        Starts the vsmtpd server.
        """

        self.server = StreamServer(listener, self.handle, backlog=backlog,
            spawn=self.pool)
        self.server.serve_forever()

    def _start_slave(self):
        """
        Starts a new slave worker process.
        """
        pid = os.fork()
        if pid == 0:
            # Set up the command line and logger id
            set_cmdline('vsmtpd: worker')
            log.connection_id = 'worker'

            # Call event_reinit()
            gevent.reinit()

            # Start vsmtpd
            self._start(self.sock)
        else:
            log.info('Worker spawned PID %d', pid)
            self.workers.append(pid)

    def stop(self, *args):
        """
        Shuts down the vsmtpd server and any workers running.
        """
        # Shut down the server or the socket, depending on which is running
        if self.workers:
            self.sock.shutdown(socket.SHUT_RDWR)
            self.sock.close()
            for pid in self.workers:
                os.kill(pid, signal.SIGTERM)
        else:
            self.server.stop()

        # Finally exit successfully
        sys.exit()