Exemplo n.º 1
0
    def run(self):
        """
        Negotiate a connecting session.

        In the case of telnet and ssh, for example, negotiates and
        inquires about terminal type, telnet options, window size,
        and tcp socket options before spawning a new session.
        """
        try:
            self._set_socket_opts()
            self.banner()
            if self.client.is_active():
                return spawn_client_session(client=self.client)
        except (Disconnected, socket.error) as err:
            self.log.debug('Connection closed: %s', err)
        finally:
            self.stopped = True
        self.client.deactivate()
Exemplo n.º 2
0
Arquivo: client.py Projeto: gofore/x84
    def run(self):
        """
        Negotiate a connecting session.

        In the case of telnet and ssh, for example, negotiates and
        inquires about terminal type, telnet options, window size,
        and tcp socket options before spawning a new session.
        """
        try:
            self._set_socket_opts()
            self.banner()
            if self.client.is_active():
                return spawn_client_session(client=self.client)
        except (Disconnected, socket.error) as err:
            self.log.debug("Connection closed: %s", err)
        finally:
            self.stopped = True
        self.client.deactivate()
Exemplo n.º 3
0
Arquivo: rlogin.py Projeto: hick/x84
    def run(self):
        """
        Perform rfc1282 (rlogin) connection establishment.

        Determine terminal type, telnet options, window size,
        and tcp socket options before spawning a new session.
        """
        try:
            self._set_socket_opts()

            self.banner()

            # Receive on-connect data-value pairs, may raise ValueError.
            data = self.get_connect_data()

            # parse into dict,
            parsed = self.parse_connect_data(data)
            for key, value in parsed.items():
                if value:
                    self.log.debug('{client.addrport}: {key}={value}'
                                   .format(client=self.client,
                                           key=key, value=value))

            # and apply to session-local self.client.env.
            self.apply_environment(parsed)

            # The server returns a zero byte to indicate that it has received
            # these strings and is now in data transfer mode.
            if self.client.is_active():
                self.client.send_str(bytes('\x00'))

                # The remote server indicates to the client that it can accept
                # window size change information by requesting a window size
                # message (as out of band data) just after connection
                # establishment and user identification exchange.  The client
                # should reply to this request with the current window size.
                #
                # Disabled: neither SyncTERM or BSD rlogin honors this, and
                # we haven't got any code to parse it. Its in the RFC but ..
                self.client.send_urgent_str(bytes('\x80'))

            matrix_kwargs = {}
            username = parsed.get('server-user-name', 'new')
            if check_new_user(username):
                # new@ login may be allowed
                matrix_kwargs['new'] = True
            if check_bye_user(username):
                # rlogin as 'bye', 'logoff', etc. not allowed
                raise ValueError('Bye user {0!r} used by rlogin'
                                 .format(username))
            if check_anonymous_user(username):
                # anonymous@ login may be allowed
                matrix_kwargs['anonymous'] = True

            if self.client.is_active():
                return spawn_client_session(client=self.client,
                                            matrix_kwargs=matrix_kwargs)
        except socket.error as err:
            self.log.debug('{client.addrport}: connection closed: {err}'
                           .format(client=self.client, err=err))
        except EOFError:
            self.log.debug('{client.addrport}: EOF from client'
                           .format(client=self.client))
        except Exception as err:
            self.log.debug('{client.addrport}: connection closed: {err}'
                           .format(client=self.client, err=err))
        finally:
            self.stopped = True
        self.client.deactivate()
Exemplo n.º 4
0
Arquivo: ssh.py Projeto: rostob/x84
    def run(self):
        """ Accept new Ssh connect in thread. """
        try:
            self.client.transport = paramiko.Transport(self.client.sock)
            self.client.transport.load_server_moduli()
            self.client.transport.add_server_key(self.server_host_key)
            ssh_session = SshSessionServer(client=self.client)
            from x84.bbs import get_ini
            if get_ini(section='sftp', key='enabled', getter='getboolean'):
                self.client.transport.set_subsystem_handler(
                    'sftp', paramiko.SFTPServer, X84SFTPServer,
                    ssh_session=ssh_session)

            def detected():
                """ Whether shell or SFTP session has been detected. """
                return (ssh_session.shell_requested.isSet() or
                        ssh_session.sftp_requested.isSet())

            self.client.transport.start_server(server=ssh_session)

            st_time = time.time()
            while self._timeleft(st_time):
                self.client.channel = self.client.transport.accept(1)
                if self.client.channel is not None:
                    break
                if not self.client.transport.is_active():
                    self.log.debug('{client.addrport}: transport closed.'
                                   .format(client=self.client))
                    self.client.deactivate()
                    return
            else:
                self.log.debug('{client.addrport}: no channel requested'
                               .format(client=self.client))
                self.client.deactivate()
                return

            first_log = False
            while not detected() and self._timeleft(st_time):
                if not first_log:
                    self.log.debug('{client.addrport}: waiting for '
                                   'shell or subsystem request.'
                                   .format(client=self.client))
                    first_log = True

                if not self.client.is_active():
                    self.log.debug('{client.addrport}: transport closed '
                                   'while waiting for shell or subsystem '
                                   'request.'
                                   .format(client=self.client))
                    self.client.deactivate()
                    return
                time.sleep(self.TIME_POLL)

            if detected():
                matrix_kwargs = {attr: getattr(ssh_session, attr)
                                 for attr in ('anonymous', 'new', 'username',)}
                return spawn_client_session(client=self.client,
                                            matrix_kwargs=matrix_kwargs)

        except (paramiko.SSHException, socket.error) as err:
            self.log.debug('{client.addrport}: connection closed: {err}'
                           .format(client=self.client, err=err))
        except EOFError:
            self.log.debug('{client.addrport}: EOF from client'
                           .format(client=self.client))
        except Exception as err:
            self.log.debug('{client.addrport}: connection closed: {err}'
                           .format(client=self.client, err=err))
        else:
            self.log.debug('{client.addrport}: shell not requested'
                           .format(client=self.client))
        finally:
            self.stopped = True
        self.client.deactivate()
Exemplo n.º 5
0
Arquivo: rlogin.py Projeto: hick/x84
    def run(self):
        """
        Perform rfc1282 (rlogin) connection establishment.

        Determine terminal type, telnet options, window size,
        and tcp socket options before spawning a new session.
        """
        try:
            self._set_socket_opts()

            self.banner()

            # Receive on-connect data-value pairs, may raise ValueError.
            data = self.get_connect_data()

            # parse into dict,
            parsed = self.parse_connect_data(data)
            for key, value in parsed.items():
                if value:
                    self.log.debug('{client.addrport}: {key}={value}'.format(
                        client=self.client, key=key, value=value))

            # and apply to session-local self.client.env.
            self.apply_environment(parsed)

            # The server returns a zero byte to indicate that it has received
            # these strings and is now in data transfer mode.
            if self.client.is_active():
                self.client.send_str(bytes('\x00'))

                # The remote server indicates to the client that it can accept
                # window size change information by requesting a window size
                # message (as out of band data) just after connection
                # establishment and user identification exchange.  The client
                # should reply to this request with the current window size.
                #
                # Disabled: neither SyncTERM or BSD rlogin honors this, and
                # we haven't got any code to parse it. Its in the RFC but ..
                self.client.send_urgent_str(bytes('\x80'))

            matrix_kwargs = {}
            username = parsed.get('server-user-name', 'new')
            if check_new_user(username):
                # new@ login may be allowed
                matrix_kwargs['new'] = True
            if check_bye_user(username):
                # rlogin as 'bye', 'logoff', etc. not allowed
                raise ValueError(
                    'Bye user {0!r} used by rlogin'.format(username))
            if check_anonymous_user(username):
                # anonymous@ login may be allowed
                matrix_kwargs['anonymous'] = True

            if self.client.is_active():
                return spawn_client_session(client=self.client,
                                            matrix_kwargs=matrix_kwargs)
        except socket.error as err:
            self.log.debug(
                '{client.addrport}: connection closed: {err}'.format(
                    client=self.client, err=err))
        except EOFError:
            self.log.debug('{client.addrport}: EOF from client'.format(
                client=self.client))
        except Exception as err:
            self.log.debug(
                '{client.addrport}: connection closed: {err}'.format(
                    client=self.client, err=err))
        finally:
            self.stopped = True
        self.client.deactivate()
Exemplo n.º 6
0
    def run(self):
        """ Accept new Ssh connect in thread. """
        try:
            self.client.transport = paramiko.Transport(self.client.sock)
            self.client.transport.load_server_moduli()
            self.client.transport.add_server_key(self.server_host_key)
            ssh_session = SshSessionServer(client=self.client)
            from x84.bbs import get_ini
            if get_ini(section='sftp', key='enabled', getter='getboolean'):
                self.client.transport.set_subsystem_handler(
                    'sftp', paramiko.SFTPServer, X84SFTPServer,
                    ssh_session=ssh_session)

            def detected():
                """ Whether shell or SFTP session has been detected. """
                return (ssh_session.shell_requested.isSet() or
                        ssh_session.sftp_requested.isSet())

            self.client.transport.start_server(server=ssh_session)

            st_time = time.time()
            while self._timeleft(st_time):
                self.client.channel = self.client.transport.accept(1)
                if self.client.channel is not None:
                    break
                if not self.client.transport.is_active():
                    self.log.debug('{client.addrport}: transport closed.'
                                   .format(client=self.client))
                    self.client.deactivate()
                    return
            else:
                self.log.debug('{client.addrport}: no channel requested'
                               .format(client=self.client))
                self.client.deactivate()
                return

            first_log = False
            while not detected() and self._timeleft(st_time):
                if not first_log:
                    self.log.debug('{client.addrport}: waiting for '
                                   'shell or subsystem request.'
                                   .format(client=self.client))
                    first_log = True

                if not self.client.is_active():
                    self.log.debug('{client.addrport}: transport closed '
                                   'while waiting for shell or subsystem '
                                   'request.'
                                   .format(client=self.client))
                    self.client.deactivate()
                    return
                time.sleep(self.TIME_POLL)

            if detected():
                matrix_kwargs = {attr: getattr(ssh_session, attr)
                                 for attr in ('anonymous', 'new', 'username',)}
                return spawn_client_session(client=self.client,
                                            matrix_kwargs=matrix_kwargs)

        except (paramiko.SSHException, socket.error) as err:
            self.log.debug('{client.addrport}: connection closed: {err}'
                           .format(client=self.client, err=err))
        except EOFError:
            self.log.debug('{client.addrport}: EOF from client'
                           .format(client=self.client))
        except Exception as err:
            self.log.debug('{client.addrport}: connection closed: {err}'
                           .format(client=self.client, err=err))
        else:
            self.log.debug('{client.addrport}: shell not requested'
                           .format(client=self.client))
        finally:
            self.stopped = True
        self.client.deactivate()
Exemplo n.º 7
0
Arquivo: ssh.py Projeto: hick/x84
    def run(self):
        """
        Accept new Ssh connect in thread.
        """
        try:
            self.client.transport = paramiko.Transport(self.client.sock)
            self.client.transport.load_server_moduli()
            self.client.transport.add_server_key(self.server_host_key)
            ssh_session = SshSessionServer(client=self.client)

            def detected():
                return ssh_session.shell_requested.isSet()

            self.client.transport.start_server(server=ssh_session)

            st_time = time.time()
            while self._timeleft(st_time):
                self.client.channel = self.client.transport.accept(1)
                if self.client.channel is not None:
                    break
                if not self.client.transport.is_active():
                    self.log.debug('{client.addrport}: transport closed.'
                                   .format(client=self.client))
                    self.client.deactivate()
                    return
            else:
                self.log.debug('{client.addrport}: no channel requested'
                               .format(client=self.client))
                self.client.deactivate()
                return

            self.log.debug('{client.addrport}: waiting for shell request'
                           .format(client=self.client))

            while not detected() and self._timeleft(st_time):
                if not self.client.is_active():
                    self.client.deactivate()
                    return
                time.sleep(self.TIME_POLL)

            if detected():
                matrix_kwargs = {attr: getattr(ssh_session, attr)
                                 for attr in ('anonymous', 'new', 'username')}
                return spawn_client_session(client=self.client,
                                            matrix_kwargs=matrix_kwargs)

        except (paramiko.SSHException, socket.error) as err:
            self.log.debug('{client.addrport}: connection closed: {err}'
                           .format(client=self.client, err=err))
        except EOFError:
            self.log.debug('{client.addrport}: EOF from client'
                           .format(client=self.client))
        except Exception as err:
            self.log.debug('{client.addrport}: connection closed: {err}'
                           .format(client=self.client, err=err))
        else:
            self.log.debug('{client.addrport}: shell not requested'
                           .format(client=self.client))
        finally:
            self.stopped = True
        self.client.deactivate()
Exemplo n.º 8
0
    def run(self):
        """
        Accept new Ssh connect in thread.
        """
        try:
            self.client.transport = paramiko.Transport(self.client.sock)
            self.client.transport.load_server_moduli()
            self.client.transport.add_server_key(self.server_host_key)
            ssh_session = SshSessionServer(client=self.client)

            def detected():
                return ssh_session.shell_requested.isSet()

            self.client.transport.start_server(server=ssh_session)

            st_time = time.time()
            while self._timeleft(st_time):
                self.client.channel = self.client.transport.accept(1)
                if self.client.channel is not None:
                    break
                if not self.client.transport.is_active():
                    self.log.debug('{client.addrport}: transport closed.'
                                   .format(client=self.client))
                    self.client.deactivate()
                    return
            else:
                self.log.debug('{client.addrport}: no channel requested'
                               .format(client=self.client))
                self.client.deactivate()
                return

            self.log.debug('{client.addrport}: waiting for shell request'
                           .format(client=self.client))

            while not detected() and self._timeleft(st_time):
                if not self.client.is_active():
                    self.client.deactivate()
                    return
                time.sleep(self.TIME_POLL)

            if detected():
                matrix_kwargs = {attr: getattr(ssh_session, attr)
                                 for attr in ('anonymous', 'new', 'username')}
                return spawn_client_session(client=self.client,
                                            matrix_kwargs=matrix_kwargs)

        except (paramiko.SSHException, socket.error) as err:
            self.log.debug('{client.addrport}: connection closed: {err}'
                           .format(client=self.client, err=err))
        except EOFError:
            self.log.debug('{client.addrport}: EOF from client'
                           .format(client=self.client))
        except Exception as err:
            self.log.debug('{client.addrport}: connection closed: {err}'
                           .format(client=self.client, err=err))
        else:
            self.log.debug('{client.addrport}: shell not requested'
                           .format(client=self.client))
        finally:
            self.stopped = True
        self.client.deactivate()