U
    n5{i…  ã                   @   s‚   d Z ddlZddlmZ ddlmZmZmZmZ ddl	m
Z
 ddlmZmZmZmZ ddlmZ ddlmZ G d	d
„ d
ejƒZdS )z‚
A Factory for SSH servers.

See also L{twisted.conch.openssh_compat.factory} for OpenSSH compatibility.

Maintainer: Paul Swartz
é    N)Úchain)ÚDictÚListÚOptionalÚTuple)Úerror)Ú_kexÚ
connectionÚ	transportÚuserauth)Úprotocol)ÚLoggerc                   @   s¸   e Zd ZU dZeeeeeeef  f  e	d< e
ƒ ZejZejejdœZddœdd„Zdd	„ Zd
d„ Zdd„ Zeeeeeeef  f  dœdd„Zeeeef dœdd„Zdd„ ZdS )Ú
SSHFactoryz$
    A Factory for SSH servers.
    Úprimes)ó   ssh-userauths   ssh-connectionN)Úreturnc                 C   sV   t | dƒs|  ¡ | _t | dƒs(|  ¡ | _| jr4| js>t d¡‚t | dƒsR|  ¡ | _dS )z4
        Check for public and private keys.
        Ú
publicKeysÚprivateKeyszno host keys, failingr   N)	ÚhasattrÚgetPublicKeysr   ÚgetPrivateKeysr   r   Z
ConchErrorÚ	getPrimesr   ©Úself© r   ú=/tmp/pip-unpacked-wheel-mnhg7r0q/twisted/conch/ssh/factory.pyÚstartFactory&   s    





zSSHFactory.startFactoryc                 C   sV   t j | |¡}tt dd„ | j ¡ D ƒ¡ƒ|_| j	sR| j
 d¡ dd„ |jD ƒ|_|S )a=  
        Create an instance of the server side of the SSH protocol.

        @type addr: L{twisted.internet.interfaces.IAddress} provider
        @param addr: The address at which the server will listen.

        @rtype: L{twisted.conch.ssh.transport.SSHServerTransport}
        @return: The built transport.
        c                 s   s   | ]}|  ¡ V  qd S ©N)ZsupportedSignatureAlgorithms)Ú.0Úkeyr   r   r   Ú	<genexpr>?   s    z+SSHFactory.buildProtocol.<locals>.<genexpr>zTdisabling non-fixed-group key exchange algorithms because we cannot find moduli filec                 S   s$   g | ]}t  |¡st  |¡r|‘qS r   )r   ZisFixedGroupZisEllipticCurve)r   ZkexAlgorithmr   r   r   Ú
<listcomp>H   s   
 
þz,SSHFactory.buildProtocol.<locals>.<listcomp>)r   ÚFactoryÚbuildProtocolÚlistr   Úfrom_iterabler   ÚvaluesZsupportedPublicKeysr   Ú_logÚinfoZsupportedKeyExchanges)r   ÚaddrÚtr   r   r   r#   3   s    

ÿÿÿþzSSHFactory.buildProtocolc                 C   s   t dƒ‚dS )zÕ
        Called when the factory is started to get the public portions of the
        servers host keys.  Returns a dictionary mapping SSH key types to
        public key strings.

        @rtype: L{dict}
        zgetPublicKeys unimplementedN©ÚNotImplementedErrorr   r   r   r   r   O   s    zSSHFactory.getPublicKeysc                 C   s   t dƒ‚dS )zê
        Called when the factory is started to get the  private portions of the
        servers host keys.  Returns a dictionary mapping SSH key types to
        L{twisted.conch.ssh.keys.Key} objects.

        @rtype: L{dict}
        zgetPrivateKeys unimplementedNr+   r   r   r   r   r   Y   s    zSSHFactory.getPrivateKeysc                 C   s   dS )zÎ
        Called when the factory is started to get Diffie-Hellman generators and
        primes to use.  Returns a dictionary mapping number of bits to lists of
        tuple of (generator, prime).
        Nr   r   r   r   r   r   c   s    zSSHFactory.getPrimes)Úbitsr   c                    sP   t t dœ‡ fdd„}| jdk	s&tdƒ‚t| j ¡ |d�}|d }t | j| ¡S )z‚
        Return a tuple of (g, p) for a Diffe-Hellman process, with p being as
        close to C{bits} bits as possible.
        )Úir   c                    s   t | ˆ  ƒS r   )Úabs)r.   ©r-   r   r   Úkeyfuncp   s    z&SSHFactory.getDHPrime.<locals>.keyfuncNz(Factory should have been started by now.)r   r   )Úintr   ÚAssertionErrorÚsortedÚkeysÚrandomÚchoice)r   r-   r1   Z
primesKeysZrealBitsr   r0   r   Ú
getDHPrimej   s
    zSSHFactory.getDHPrimec                 C   s    |dkst |dƒr| j| S dS )zí
        Return a class to use as a service for the given transport.

        @type transport:    L{transport.SSHServerTransport}
        @type service:      L{bytes}
        @rtype:             subclass of L{service.SSHService}
        r   ZavatarN)r   Úservices)r   r
   Zservicer   r   r   Ú
getServicex   s    zSSHFactory.getService)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r2   r   r   Ú__annotations__r   r'   r
   ZSSHServerTransportr   r   ZSSHUserAuthServerr	   ZSSHConnectionr9   r   r#   r   r   r   r8   r:   r   r   r   r   r      s   
 þ

&r   )r>   r6   Ú	itertoolsr   Útypingr   r   r   r   Ztwisted.conchr   Ztwisted.conch.sshr   r	   r
   r   Ztwisted.internetr   Ztwisted.loggerr   r"   r   r   r   r   r   Ú<module>   s   	