def server_ssh_connect(self): """ Connect to the remote SMTP server over SSH and configure port forwarding with :py:class:`.SSHTCPForwarder` for tunneling SMTP traffic. :return: The connection status as one of the :py:class:`.ConnectionErrorReason` constants. """ server = smoke_zephyr.utilities.parse_server(self.config['ssh_server'], 22) username = self.config['ssh_username'] password = self.config['ssh_password'] remote_server = smoke_zephyr.utilities.parse_server(self.config['smtp_server'], 25) try: self._ssh_forwarder = SSHTCPForwarder( server, username, password, remote_server, private_key=self.config.get('ssh_preferred_key'), missing_host_key_policy=ssh_host_key.MissingHostKeyPolicy(self.application) ) self._ssh_forwarder.start() except errors.KingPhisherAbortError as error: self.logger.info("ssh connection aborted ({0})".format(error.message)) except paramiko.AuthenticationException: self.logger.warning('failed to authenticate to the remote ssh server') return ConnectionErrorReason.ERROR_AUTHENTICATION_FAILED except paramiko.SSHException as error: self.logger.warning("failed with ssh exception '{0}'".format(error.message)) except Exception: self.logger.warning('failed to connect to the remote ssh server', exc_info=True) else: self.smtp_server = self._ssh_forwarder.local_server return ConnectionErrorReason.SUCCESS return ConnectionErrorReason.ERROR_UNKNOWN
def _create_ssh_forwarder(self, server, username, password, window=None): """ Create and set the :py:attr:`~.KingPhisherClientApplication._ssh_forwarder` attribute. :param tuple server: The server information as a host and port tuple. :param str username: The username to authenticate to the SSH server with. :param str password: The password to authenticate to the SSH server with. :param window: The GTK window to use as the parent for error dialogs. :type window: :py:class:`Gtk.Window` :rtype: int :return: The local port that is forwarded to the remote server or None if the connection failed. """ window = window or self.get_active_window() title_ssh_error = 'Failed To Connect To The SSH Service' server_remote_port = self.config['server_remote_port'] try: self._ssh_forwarder = ssh_forward.SSHTCPForwarder( server, username, password, ('127.0.0.1', server_remote_port), private_key=self.config.get('ssh_preferred_key'), missing_host_key_policy=ssh_host_key.MissingHostKeyPolicy( self)) self._ssh_forwarder.start() except ssh_forward.KingPhisherSSHKeyError as error: gui_utilities.show_dialog_error('SSH Key Configuration Error', window, error.message) except errors.KingPhisherAbortError as error: self.logger.info("ssh connection aborted ({0})".format( error.message)) except paramiko.PasswordRequiredException: gui_utilities.show_dialog_error( title_ssh_error, window, 'The specified SSH key requires a password.') except paramiko.AuthenticationException: self.logger.warning( 'failed to authenticate to the remote ssh server') gui_utilities.show_dialog_error( title_ssh_error, window, 'The server responded that the credentials are invalid.') except paramiko.SSHException as error: self.logger.warning("failed with ssh exception '{0}'".format( error.args[0])) except socket.error as error: gui_utilities.show_dialog_exc_socket_error(error, window, title=title_ssh_error) except Exception as error: self.logger.warning('failed to connect to the remote ssh server', exc_info=True) gui_utilities.show_dialog_error( title_ssh_error, window, "An {0}.{1} error occurred.".format(error.__class__.__module__, error.__class__.__name__)) else: return self._ssh_forwarder.local_server self.emit('server-disconnected') return