U
    
W[                     @   s   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZmZmZ ddlmZ dd	lmZ G d
d dZG dd dZee	jG dd dZG dd dejZdd ZdS )z\
TAP plugin for creating telnet- and ssh-accessible manhole servers.

@author: Jp Calderone
    )implementer)protocol)servicestrports)portalcheckers)usagefilepath)manholemanhole_sshtelnet)insults)keysc                   @   s   e Zd Zdd Zdd ZdS )makeTelnetProtocolc                 C   s
   || _ d S N)r   )selfr    r   ;/usr/lib/python3/dist-packages/twisted/conch/manhole_tap.py__init__   s    zmakeTelnetProtocol.__init__c                 C   s   t j}| jf}t j|f| S r   )r   ZAuthenticatingTelnetProtocolr   ZTelnetTransport)r   Zauthargsr   r   r   __call__   s    zmakeTelnetProtocol.__call__N__name__
__module____qualname__r   r   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd Zdd ZdS )chainedProtocolFactoryc                 C   s
   || _ d S r   )	namespace)r   r   r   r   r   r   #   s    zchainedProtocolFactory.__init__c                 C   s   t tj| jS r   )r   ServerProtocolr
   ColoredManholer   r   r   r   r   r   &   s    zchainedProtocolFactory.__call__Nr   r   r   r   r   r   "   s   r   c                   @   s   e Zd Zdd Zdd ZdS )_StupidRealmc                 O   s   || _ || _|| _d S r   )protocolFactoryprotocolArgsprotocolKwArgs)r   protoakwr   r   r   r   -   s    z_StupidRealm.__init__c                 G   s0   t j|kr&t j| j| j| jdd fS t d S )Nc                   S   s   d S r   r   r   r   r   r   <lambda>7       z,_StupidRealm.requestAvatar.<locals>.<lambda>)r   ZITelnetProtocolr!   r"   r#   NotImplementedError)r   ZavatarIdZ
interfacesr   r   r   requestAvatar2   s    
z_StupidRealm.requestAvatarN)r   r   r   r   r*   r   r   r   r   r    +   s   r    c                	   @   s\   e Zd Zddddgddddgdd	d
dgddddgddddgddddggZdd Zdd ZdS )Options
telnetPorttNzMstrports description of the address on which to listen for telnet connectionssshPortszJstrports description of the address on which to listen for ssh connectionspasswdpz/etc/passwdz1name of a passwd(5)-format username/password file	sshKeyDir<USER DATA DIR>z2Directory where the autogenerated SSH key is kept.
sshKeyNamez
server.keyz&Filename of the autogenerated SSH key.
sshKeySizei   z,Size of the automatically generated SSH key.c                 C   s   t j|  d | d< d S )Nr   )r   r+   r   r   r   r   r   r   N   s    zOptions.__init__c                 C   s&   | d d kr"| d d kr"t dd S )Nr,   r.   z<At least one of --telnetPort and --sshPort must be specified)r   Z
UsageErrorr   r   r   r   postOptionsR   s    zOptions.postOptions)r   r   r   ZoptParametersr   r6   r   r   r   r   r+   <   s*   r+   c                 C   s6  t  }| d }|dkri }t| d }| d rttjtjt	j
|}t||g}t }t||_t | d |}|| | d r2t }t||_t||g}	t|	}
| d dkr| d }ndd	lm} | }t|| d
 }t|t| d }||
jd< ||
jd< t | d |
}|| |S )a;  
    Create a manhole server service.

    @type options: L{dict}
    @param options: A mapping describing the configuration of
    the desired service.  Recognized key/value pairs are::

        "telnetPort": strports description of the address on which
                      to listen for telnet connections.  If None,
                      no telnet service will be started.

        "sshPort": strports description of the address on which to
                   listen for ssh connections.  If None, no ssh
                   service will be started.

        "namespace": dictionary containing desired initial locals
                     for manhole connections.  If None, an empty
                     dictionary will be used.

        "passwd": Name of a passwd(5)-format username/password file.

        "sshKeyDir": The folder that the SSH server key will be kept in.

        "sshKeyName": The filename of the key.

        "sshKeySize": The size of the key, in bits. Default is 4096.

    @rtype: L{twisted.application.service.IService}
    @return: A manhole service.
    r   Nr0   r,   r.   r2   r3   r   )getDataDirectoryr4   r5   s   ssh-rsa) r   ZMultiServicer   ZFilePasswordDBr    r   ZTelnetBootstrapProtocolr   r   r
   r   r   ZPortalr   ZServerFactoryr   r   ZsetServiceParentr   ZTerminalRealmr   ZConchFactoryZtwisted.python._appdirsr7   r	   ZFilePathZchildr   Z_getPersistentRSAKeyintZ
publicKeysZprivateKeys)ZoptionsZsvcr   ZcheckerZtelnetRealmZtelnetPortalZtelnetFactoryZtelnetServiceZsshRealmZ	sshPortalZ
sshFactoryZkeyDirr7   ZkeyLocationZsshKeyZ
sshServicer   r   r   makeServiceY   sH    










r9   N)__doc__Zzope.interfacer   Ztwisted.internetr   Ztwisted.applicationr   r   Ztwisted.credr   r   Ztwisted.pythonr   r	   Ztwisted.conchr
   r   r   Ztwisted.conch.insultsr   Ztwisted.conch.sshr   r   r   ZIRealmr    r+   r9   r   r   r   r   <module>   s   	