o
    ¯bl  ã                   @   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                   @   ó   e Zd Zdd„ Zdd„ ZdS )ÚmakeTelnetProtocolc                 C   ó
   || _ d S ©N)r   )Úselfr   © r   ú;/usr/lib/python3/dist-packages/twisted/conch/manhole_tap.pyÚ__init__   ó   
zmakeTelnetProtocol.__init__c                 C   s    t j}| jf}t j|g|¢R Ž S r   )r   ÚAuthenticatingTelnetProtocolr   ÚTelnetTransport)r   ÚauthÚargsr   r   r   Ú__call__   s   zmakeTelnetProtocol.__call__N©Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r      ó    r   c                   @   r   )ÚchainedProtocolFactoryc                 C   r   r   )Ú	namespace)r   r#   r   r   r   r       r   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"      r!   r"   c                   @   r   )Ú_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|v rt j| j| ji | j¤Ždd„ fS tƒ ‚)Nc                   S   s   d S r   r   r   r   r   r   Ú<lambda>3   s    z,_StupidRealm.requestAvatar.<locals>.<lambda>)r   ÚITelnetProtocolr(   r)   r*   ÚNotImplementedError)r   ÚavatarIdÚ
interfacesr   r   r   ÚrequestAvatar.   s   
ýz_StupidRealm.requestAvatarN)r   r   r    r   r3   r   r   r   r   r'   '   s    r'   c                   @   sD   e Zd Zg d¢g d¢g d¢g d¢g d¢g d¢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ÚsNzJstrports description of the address on which to listen for ssh connections)ÚpasswdÚpz/etc/passwdz1name of a passwd(5)-format username/password file)Ú	sshKeyDirNú<USER DATA DIR>z2Directory where the autogenerated SSH key is kept.)Ú
sshKeyNameNz
server.keyz&Filename of the autogenerated SSH key.)Ú
sshKeySizeNi   z,Size of the automatically generated SSH key.c                 C   s   t j | ¡ d | d< d S )Nr#   )r   r4   r   r&   r   r   r   r   \   s   zOptions.__init__c                 C   s*   | d d u r| d d u rt  d¡‚d S d S )Nr5   r7   z<At least one of --telnetPort and --sshPort must be specified)r   Ú
UsageErrorr&   r   r   r   ÚpostOptions`   s
   ÿÿzOptions.postOptionsN)r   r   r    ÚoptParametersr   r@   r   r   r   r   r4   8   s    		à#r4   c                 C   s4  t  ¡ }| d }|du ri }t | d ¡}| d r@ttjtjt	j
|ƒ}t ||g¡}t ¡ }t|ƒ|_t  | d |¡}| |¡ | d r˜t ¡ }t|ƒ|_t ||g¡}	t |	¡}
| d dkrd| 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#   Nr9   r5   r7   r;   r<   r   )ÚgetDataDirectoryr=   r>   s   ssh-rsa) r   ÚMultiServicer
   ÚFilePasswordDBr'   r   ÚTelnetBootstrapProtocolr   r$   r   r%   r   ÚPortalr   ÚServerFactoryr   r   ÚsetServiceParentr   ÚTerminalRealmr"   ÚConchFactoryÚtwisted.python._appdirsrB   r   ÚFilePathÚchildr	   Ú_getPersistentRSAKeyÚintÚ
publicKeysÚprivateKeys)ÚoptionsÚsvcr#   ÚcheckerÚtelnetRealmÚtelnetPortalÚtelnetFactoryÚtelnetServiceÚsshRealmÚ	sshPortalÚ
sshFactoryÚkeyDirrB   ÚkeyLocationÚsshKeyÚ
sshServicer   r   r   ÚmakeServiceg   sB   ü







r`   N)Ú__doc__Úzope.interfacer   Útwisted.applicationr   r   Útwisted.conchr   r   r   Útwisted.conch.insultsr   Útwisted.conch.sshr	   Útwisted.credr
   r   Útwisted.internetr   Útwisted.pythonr   r   r   r"   ÚIRealmr'   r4   r`   r   r   r   r   Ú<module>   s   
/