def missing_host_key(self, client, hostname, key):
    host_key_fingerprint = ('sha256:' + base64.b64encode(hashlib.new('sha256', key.asbytes()).digest()).decode('utf-8'))
    host_keys = paramiko.hostkeys.HostKeys()
    host_keys_modified = False
    known_hosts_file = self.application.config.get('ssh_known_hosts_file', os.path.join(GLib.get_user_config_dir(), 'king-phisher', 'known_hosts'))
    if os.access(known_hosts_file, os.R_OK):
        host_keys.load(known_hosts_file)
    if host_keys.lookup(hostname):
        if host_keys.check(hostname, key):
            self.logger.debug('accepting known ssh host key {0} {1} {2}'.format(hostname, key.get_name(), host_key_fingerprint))
            return
        self.logger.warning('ssh host key does not match known value for {0}'.format(hostname))
        dialog = HostKeyWarnDialog(self.application, hostname, key)
        if (dialog.interact() != Gtk.ResponseType.ACCEPT):
            raise errors.KingPhisherAbortError(('bad ssh host key for ' + hostname))
    else:
        dialog = HostKeyAcceptDialog(self.application, hostname, key)
        if (dialog.interact() != Gtk.ResponseType.ACCEPT):
            raise errors.KingPhisherAbortError(('unknown ssh host key not accepted by the user for ' + hostname))
        host_keys.add(hostname, key.get_name(), key)
        host_keys_modified = True
    if host_keys_modified:
        try:
            host_keys.save(known_hosts_file)
            os.chmod(known_hosts_file, 384)
        except (IOError if its.py_v2 else PermissionError):
            self.logger.warning('failed to save the known_hosts file and set its permissions')
def missing_host_key(self, client, hostname, key):
    host_key_fingerprint = ('sha256:' + base64.b64encode(hashlib.new('sha256', key.asbytes()).digest()).decode('utf-8'))
    host_keys = paramiko.hostkeys.HostKeys()
    host_keys_modified = False
    known_hosts_file = self.application.config.get('ssh_known_hosts_file', os.path.join(GLib.get_user_config_dir(), 'king-phisher', 'known_hosts'))
    if os.access(known_hosts_file, os.R_OK):
        host_keys.load(known_hosts_file)
    if host_keys.lookup(hostname):
        if host_keys.check(hostname, key):
            self.logger.debug('accepting known ssh host key {0} {1} {2}'.format(hostname, key.get_name(), host_key_fingerprint))
            return
        self.logger.warning('ssh host key does not match known value for {0}'.format(hostname))
        dialog = HostKeyWarnDialog(self.application, hostname, key)
        if (dialog.interact() != Gtk.ResponseType.ACCEPT):
            raise errors.KingPhisherAbortError(('bad ssh host key for ' + hostname))
    else:
        dialog = HostKeyAcceptDialog(self.application, hostname, key)
        if (dialog.interact() != Gtk.ResponseType.ACCEPT):
            raise errors.KingPhisherAbortError(('unknown ssh host key not accepted by the user for ' + hostname))
        host_keys.add(hostname, key.get_name(), key)
        host_keys_modified = True
    if host_keys_modified:
        try:
            host_keys.save(known_hosts_file)
            os.chmod(known_hosts_file, 384)
        except (IOError if its.py_v2 else PermissionError):
            self.logger.warning('failed to save the known_hosts file and set its permissions')