o
    Òó·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                   @   ó   e Zd Zdd„ Zdd„ ZdS )ÚmakeTelnetProtocolc                 C   ó
   || _ d S ©N)r   )Úselfr   © r   ú_C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/conch/manhole_tap.pyÚ__init__   ó   
zmakeTelnetProtocol.__init__c                 C   s    t j}| jf}t j|g|¢R Ž 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      ó    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   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                   @   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   r/   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 )Nr0   r2   z<At least one of --telnetPort and --sshPort must be specified)r   Z
UsageErrorr$   r   r   r   ÚpostOptions`   s
   ÿÿzOptions.postOptionsN)r   r   r   ZoptParametersr   r:   r   r   r   r   r/   8   s    		à#r/   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!   Nr4   r0   r2   r6   r7   r   )ÚgetDataDirectoryr8   r9   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._appdirsr;   r   ZFilePathÚchildr	   Z_getPersistentRSAKeyÚintZ
publicKeysZprivateKeys)ÚoptionsZsvcr!   ÚcheckerZtelnetRealmZtelnetPortalZtelnetFactoryZtelnetServiceZsshRealmZ	sshPortalZ
sshFactoryZkeyDirr;   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   
/