U
    c6{il  ã                   @   sÊ   d Z ddl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 ddlmZmZ ddlmZ dd	lmZ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)ÚserviceÚstrports)ÚmanholeÚmanhole_sshÚtelnet)Úinsults)Úkeys)ÚcheckersÚportal)Úprotocol)ÚfilepathÚusagec                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚmakeTelnetProtocolc                 C   s
   || _ d S ©N)r   )Úselfr   © r   ú=/tmp/pip-unpacked-wheel-hfbvu0bz/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   Ú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>3   ó    z,_StupidRealm.requestAvatar.<locals>.<lambda>)r   ZITelnetProtocolr"   r#   r$   ÚNotImplementedError)r   ZavatarIdZ
interfacesr   r   r   ÚrequestAvatar.   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   \   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   ÚpostOptions`   s    ÿzOptions.postOptions)r   r   r   ZoptParametersr   r7   r   r   r   r   r,   8   s2   û
û
üü

à#r,   c                 C   s6  t  ¡ }| d }|dkri }t | d ¡}| d r€t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   Nr1   r-   r/   r3   r4   r   )ÚgetDataDirectoryr5   r6   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._appdirsr8   r   ZFilePathÚchildr	   Z_getPersistentRSAKeyÚintZ
publicKeysZprivateKeys)ÚoptionsZsvcr   ÚcheckerZtelnetRealmZtelnetPortalZtelnetFactoryZtelnetServiceZsshRealmZ	sshPortalZ
sshFactoryZkeyDirr8   ZkeyLocationZsshKeyZ
sshServicer   r   r   ÚmakeServiceg   sB    ü








r=   N)Ú__doc__Zzope.interfacer   Ztwisted.applicationr   r   Ztwisted.conchr   r   r   Ztwisted.conch.insultsr   Ztwisted.conch.sshr	   Ztwisted.credr
   r   Ztwisted.internetr   Ztwisted.pythonr   r   r   r   ZIRealmr!   r,   r=   r   r   r   r   Ú<module>   s   
/