Protocols

x84.server

Package provides base server for x/84.

class x84.server.BaseServer[source]

Bases: object

Base class for server implementations.

LISTEN_BACKLOG = 5

Number of clients that can wait to be accepted

MAX_CONNECTIONS = 100

Maximum number of clients

client_count()[source]

Return number of active connections.

client_factory = None

Client factory should be a class defining what should be instantiated for the client instance.

classmethod client_factory_kwargs(instance)[source]

Return keyword arguments for the client_factory.

Method should be derived and modified, A dictionary may be substituted. The default return value is an empty dictionary.

:rtype dict

client_fds()[source]

Return list of client file descriptors.

client_list()[source]

Return list of connected clients.

clients = {}

Dictionary of active clients, (file descriptor, Client, ...)

clients_ready(ready_fds=None)[source]

Return list of clients with data ready to be receive.

Parameters:ready_fds (list) – file descriptors already known to be ready
connect_factory = None

Connect factory should be a class, derived from threading.Thread, that should be instantiated on-connect to perform negotiation and launch the bbs session upon success.

classmethod connect_factory_kwargs(instance)[source]

Return keyword arguments for the connect_factory.

Method should be derived and modified, A dictionary may be substituted. The default return value is an empty dictionary.

:rtype dict

env = {}

Dictionary of environment variables received by negotiation

threads = []

List of on-connect negotiating threads.

x84.client

Base classes for clients and connections of x/84.

class x84.client.BaseClient(sock, address_pair, on_naws=None)[source]

Bases: object

Base class for remote client implementations.

Instantiated by the corresponding BaseServer class.

Class initializer.

BLOCKSIZE_RECV = 64

maximum unit of data received for each call to socket_recv()

TTYPE_UNDETECTED = 'unknown'

terminal type identifier when not yet negotiated

addrport[source]

IP address and port of connection as string (ip:port).

close()[source]

Close connection with the client.

deactivate()[source]

Flag client for disconnection by engine loop.

duration()[source]

Time elapsed since connection was made.

fileno()[source]

File descriptor number of socket.

get_input()[source]

Receive input from client into self.recv_buffer.

Should be called conditionally when input_ready() returns True.

idle()[source]

Time elapsed since data was last received.

input_ready()[source]

Whether any data is buffered for reading.

is_active()[source]

Whether this connection is active (bool).

kind = None

Override in subclass: a general string identifier for the connecting protocol (for example, ‘telnet’, ‘ssh’, ‘rlogin’)

recv_ready()[source]

Subclass and implement: whether socket_recv() should be called.

:raises NotImplementedError

send()[source]

Send any data buffered and return number of bytes send.

Raises Disconnected:
 client has disconnected (cannot write to socket).
send_ready()[source]

Whether any data is buffered for delivery.

send_str(bstr)[source]

Buffer bytestring for client.

send_unicode(ucs, encoding='utf8')[source]

Buffer unicode string, encoded for client as ‘encoding’.

shutdown()[source]

Shutdown and close socket.

Called by event loop after client is marked by deactivate().

socket_recv()[source]

Receive data from socket, returns number of bytes received.

Raises Disconnect:
 client has disconnected.
Return type:int
class x84.client.BaseConnect(client)[source]

Bases: threading.Thread

Base class for client connect factories.

Class initializer.

banner()[source]

Write data on-connect, callback from run().

run()[source]

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.

stopped = False

whether this thread is completed. Set to True to cause an on-connect thread to forcefully exit.

x84.telnet

Telnet server for x84.

Limitations:

  • No linemode support, character-at-a-time only.
  • No out-of-band / data mark (DM) / sync supported
  • No flow control (^S, ^Q)

This is a modified version of miniboa retrieved from svn address http://miniboa.googlecode.com/svn/trunk/miniboa which is meant for MUD’s. This server would not be safe for most (linemode) MUD clients.

Changes from miniboa:

  • character-at-a-time input instead of linemode
  • encoding option on send
  • strict rejection of linemode
  • terminal type detection
  • environment variable support
  • GA and SGA
  • utf-8 safe
class x84.telnet.ConnectTelnet(client)[source]

Bases: x84.client.BaseConnect

Accept new Telnet Connection and negotiate options.

Class initializer.

TIME_NEGOTIATE = 2.5

maximum time elapsed allowed to begin on-connect negotiation

TIME_POLL = 0.1

polling duration during negotiation

TIME_WAIT_STAGE = 3.5

wait upto 3500ms for all stages of negotiation to complete

banner()[source]

This method is called after the connection is initiated.

This routine happens to communicate with a wide variety of network scanners when listening on the default port on a public IP address.

run()[source]

Negotiate and inquire about terminal type, telnet options, window size, and tcp socket options before spawning a new session.

class x84.telnet.TelnetClient(sock, address_pair, on_naws=None)[source]

Bases: x84.client.BaseClient

Represents a remote Telnet Client, instantiated from TelnetServer.

SB_MAXLEN = 65534

maximum size of telnet subnegotiation string, allowing for a fairly large value for NEW_ENVIRON.

check_local_option(option)[source]

Test the status of local negotiated Telnet options.

check_remote_option(option)[source]

Test the status of remote negotiated Telnet options.

recv_ready()[source]

Returns True if data is awaiting on the telnet socket.

request_do_binary()[source]

Tell the DE that we would like them to input binary 8-bit (utf8).

request_do_env()[source]

Request to Negotiate About Window Size. See RFC 1073.

request_do_naws()[source]

Request to Negotiate About Window Size. See RFC 1073.

request_do_sga()[source]

Request to Negotiate SGA. See ...

request_do_ttype()[source]

Begins TERMINAL-TYPE negotiation

request_env()[source]

Request sub-negotiation NEW_ENVIRON. See RFC 1572.

request_ttype()[source]

Sends IAC SB TTYPE SEND IAC SE

request_will_binary()[source]

Tell the DE that we would like to use binary 8-bit (utf8).

request_will_echo()[source]

Tell the DE that we would like to echo their text. See RFC 857.

request_will_sga()[source]

Request DE to Suppress Go-Ahead. See RFC 858.

send_unicode(ucs, encoding='utf8')[source]

Buffer unicode string, encoded for client as ‘encoding’.

socket_recv()[source]

Called by TelnetServer.poll() when recv data is ready. Read any data on socket, processing telnet commands, and buffering all other bytestrings to self.recv_buffer. If data is not received, or the connection is closed, x84.bbs.exception.Disconnected is raised.

class x84.telnet.TelnetOption[source]

Bases: object

Simple class used to track the status of an extended Telnet option.

Attributes and their state values:

  • local_option: UNKNOWN (default), True, or False.
  • remote_option: UNKNOWN (default), True, or False.
  • reply_pending: True or Fale.

Set attribute defaults on init.

class x84.telnet.TelnetServer(config)[source]

Bases: x84.server.BaseServer

Poll sockets for new connections and sending/receiving data from clients.

Create a new Telnet Server.

Parameters:config (ConfigParser.ConfigParser) – configuration section [telnet], with options 'addr', 'port'
client_factory

alias of TelnetClient

connect_factory

alias of ConnectTelnet

x84.telnet.debug_option(func)[source]

This function is a decorator that debug prints the ‘from’ address for callables decorated with this. This helps during telnet negotiation, to understand which function sets or checks local or remote option states.

x84.telnet.name_option(option)[source]

Perform introspection of global CONSTANTS for equivalent values, and return a string that displays its possible meanings

x84.ssh

x84.rlogin

rlogin server for x84.

This only exists to demonstrate alternative client protocols rather than only ssh or telnet. rlogin is a very insecure and not recommended!

class x84.rlogin.ConnectRLogin(client)[source]

Bases: x84.client.BaseConnect

rlogin protocol connection handler.

Takes care of the (initial) handshake, terminal and session setup.

Class initializer.

TIME_NEGOTIATE = 5.0

maximum time elapsed allowed for on-connect negotiation

TIME_POLL = 0.1

poll interval for on-connect negotiation

apply_environment(parsed)[source]

Cherry-pick rlogin values into client environment variables.

Parameters:parsed (dict) – values identified by class method parse_connect_data()
Return type:None
get_connect_data()[source]

Receive four null-terminated strings transmitted by client on-connect.

Returns:bytes received, containing at least 4 NUL-terminated strings.
Return type:str
Raises ValueError:
 on-connect data timeout or bandwidth exceeded.
parse_connect_data(data)[source]

Parse and return raw data received by client on-connect.

Parameters:data (str) – bytes received by class method get_connect_data().
Returns:dictionary containing pertinent key/values
Return type:dict
run()[source]

Perform rfc1282 (rlogin) connection establishment.

Determine rlogin on-connect data, rlogin may only negotiate session user name and terminal type.

class x84.rlogin.RLoginClient(sock, address_pair, on_naws=None)[source]

Bases: x84.client.BaseClient

rlogin protocol client handler.

recv_ready()[source]

Whether data is awaiting on the telnet socket.

send()[source]

Send any data buffered and return number of bytes send.

Raises Disconnected:
 client has disconnected (cannot write to socket).
send_ready()[source]

Whether any data is buffered for delivery.

send_urgent_str(bstr)[source]

Buffer urgent (OOB) message to client from bytestring.

class x84.rlogin.RLoginServer(config)[source]

Bases: x84.server.BaseServer

RLogin/RSH protocol server.

Class initializer.

client_factory

alias of RLoginClient

client_fds()[source]

Return list of rlogin client file descriptors.

connect_factory

alias of ConnectRLogin

x84.sftp

x84.webserve