示例#1
0
def server_from_config(config, handler_klass=None, plugin_manager=None):
	"""
	Build a server from a provided configuration instance. If *handler_klass* is
	specified, then the object must inherit from the corresponding
	KingPhisherServer base class.

	:param config: Configuration to retrieve settings from.
	:type config: :py:class:`smoke_zephyr.configuration.Configuration`
	:param handler_klass: Alternative handler class to use.
	:type handler_klass: :py:class:`.KingPhisherRequestHandler`
	:param plugin_manager: The server's plugin manager instance.
	:type plugin_manager: :py:class:`~king_phisher.server.plugins.ServerPluginManager`
	:return: A configured server instance.
	:rtype: :py:class:`.KingPhisherServer`
	"""
	handler_klass = (handler_klass or KingPhisherRequestHandler)
	addresses = get_bind_addresses(config)

	if not len(addresses):
		raise errors.KingPhisherError('at least one address to listen on must be specified')

	ssl_certfile = None
	ssl_keyfile = None
	if config.has_option('server.ssl_cert'):
		ssl_certfile = config.get('server.ssl_cert')
		if not os.access(ssl_certfile, os.R_OK):
			logger.critical("setting server.ssl_cert file '{0}' not found".format(ssl_certfile))
			raise errors.KingPhisherError('invalid ssl configuration, missing file')
		logger.info("using default ssl cert file '{0}'".format(ssl_certfile))
		if config.has_option('server.ssl_key'):
			ssl_keyfile = config.get('server.ssl_key')
			if not os.access(ssl_keyfile, os.R_OK):
				logger.critical("setting server.ssl_key file '{0}' not found".format(ssl_keyfile))
				raise errors.KingPhisherError('invalid ssl configuration, missing file')

	if any([address[2] for address in addresses]) and ssl_certfile is None:
		raise errors.KingPhisherError('an ssl certificate must be specified when ssl is enabled')
	if ssl_certfile is None:
		ssl_hostnames = []
	else:
		ssl_hostnames = get_ssl_hostnames(config)

	try:
		server = KingPhisherServer(config, plugin_manager, handler_klass, addresses=addresses, ssl_certfile=ssl_certfile, ssl_keyfile=ssl_keyfile)
	except socket.error as error:
		error_number, error_message = error.args
		error_message = "socket error #{0} ({1})".format((error_number or 'NOT-SET'), error_message)
		if error_number == 98:
			logger.error('failed to bind server to address (socket error #98)')
		logger.error(error_message, exc_info=True)
		raise errors.KingPhisherError(error_message)
	if config.has_option('server.server_header'):
		server.server_version = config.get('server.server_header')
		logger.info("setting the server version to the custom header: '{0}'".format(config.get('server.server_header')))
	for hostname, ssl_certfile, ssl_keyfile in ssl_hostnames:
		logger.info("setting configuration for ssl hostname: {0} with cert: {1}".format(hostname, ssl_certfile))
		server.add_sni_cert(hostname, ssl_certfile=ssl_certfile, ssl_keyfile=ssl_keyfile)

	signals.server_initialized.send(server)
	return server
示例#2
0
def server_from_config(config, handler_klass=None, plugin_manager=None):
	"""
	Build a server from a provided configuration instance. If *handler_klass* is
	specified, then the object must inherit from the corresponding
	KingPhisherServer base class.

	:param config: Configuration to retrieve settings from.
	:type config: :py:class:`smoke_zephyr.configuration.Configuration`
	:param handler_klass: Alternative handler class to use.
	:type handler_klass: :py:class:`.KingPhisherRequestHandler`
	:param plugin_manager: The server's plugin manager instance.
	:type plugin_manager: :py:class:`~king_phisher.server.plugins.ServerPluginManager`
	:return: A configured server instance.
	:rtype: :py:class:`.KingPhisherServer`
	"""
	handler_klass = (handler_klass or KingPhisherRequestHandler)
	addresses = get_bind_addresses(config)

	if not len(addresses):
		raise errors.KingPhisherError('at least one address to listen on must be specified')

	ssl_certfile = None
	ssl_keyfile = None
	if config.has_option('server.ssl_cert'):
		ssl_certfile = config.get('server.ssl_cert')
		if not os.access(ssl_certfile, os.R_OK):
			logger.critical("setting server.ssl_cert file '{0}' not found".format(ssl_certfile))
			raise errors.KingPhisherError('invalid ssl configuration, missing file')
		logger.info("using default ssl cert file '{0}'".format(ssl_certfile))
		if config.has_option('server.ssl_key'):
			ssl_keyfile = config.get('server.ssl_key')
			if not os.access(ssl_keyfile, os.R_OK):
				logger.critical("setting server.ssl_key file '{0}' not found".format(ssl_keyfile))
				raise errors.KingPhisherError('invalid ssl configuration, missing file')

	if any([address[2] for address in addresses]) and ssl_certfile is None:
		raise errors.KingPhisherError('an ssl certificate must be specified when ssl is enabled')
	if ssl_certfile is None:
		ssl_hostnames = []
	else:
		ssl_hostnames = get_ssl_hostnames(config)

	try:
		server = KingPhisherServer(config, plugin_manager, handler_klass, addresses=addresses, ssl_certfile=ssl_certfile, ssl_keyfile=ssl_keyfile)
	except socket.error as error:
		error_number, error_message = error.args
		error_message = "socket error #{0} ({1})".format((error_number or 'NOT-SET'), error_message)
		if error_number == 98:
			logger.error('failed to bind server to address (socket error #98)')
		logger.error(error_message, exc_info=True)
		raise errors.KingPhisherError(error_message) from None
	if config.has_option('server.server_header'):
		server.server_version = config.get('server.server_header')
		logger.info("setting the server version to the custom header: '{0}'".format(config.get('server.server_header')))
	for hostname, ssl_certfile, ssl_keyfile in ssl_hostnames:
		logger.info("setting configuration for ssl hostname: {0} with cert: {1}".format(hostname, ssl_certfile))
		server.add_sni_cert(hostname, ssl_certfile=ssl_certfile, ssl_keyfile=ssl_keyfile)

	signals.server_initialized.send(server)
	return server