o
    ¯bú  ã                   @   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 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)Úerror)Ú_kexÚ
connectionÚ	transportÚuserauth)Úprotocol)ÚLoggerc                   @   sb   e Zd ZdZeƒ ZejZe	j
ejdœZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ ZdS )Ú
SSHFactoryz$
    A Factory for SSH servers.
    )ó   ssh-userauths   ssh-connectionc                 C   sZ   t | dƒs
|  ¡ | _t | dƒs|  ¡ | _| jr| jst d¡‚t | dƒs+|  ¡ | _dS dS )z4
        Check for public and private keys.
        Ú
publicKeysÚprivateKeyszno host keys, failingÚprimesN)	ÚhasattrÚgetPublicKeysr   ÚgetPrivateKeysr   r   Ú
ConchErrorÚ	getPrimesr   ©Úself© r   ú;/usr/lib/python3/dist-packages/twisted/conch/ssh/factory.pyÚstartFactory"   s   





ÿzSSHFactory.startFactoryc                 C   sB   t j | |¡}| j ¡ |_| js| 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.
        zTdisabling non-fixed-group key exchange algorithms because we cannot find moduli filec                 S   s$   g | ]}t  |¡st  |¡r|‘qS r   )r   ÚisFixedGroupÚisEllipticCurve)Ú.0ÚkexAlgorithmr   r   r   Ú
<listcomp>@   s    ýýÿz,SSHFactory.buildProtocol.<locals>.<listcomp>)
r   ÚFactoryÚbuildProtocolr   ÚkeysÚsupportedPublicKeysr   Ú_logÚinfoÚsupportedKeyExchanges)r   ÚaddrÚtr   r   r   r   /   s   
ÿþzSSHFactory.buildProtocolc                 C   ó   t dƒ‚)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 unimplemented©ÚNotImplementedErrorr   r   r   r   r   G   ó   zSSHFactory.getPublicKeysc                 C   r&   )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 unimplementedr'   r   r   r   r   r   Q   r)   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).

        @rtype: L{dict}
        Nr   r   r   r   r   r   [   s    zSSHFactory.getPrimesc                    s2   t | j ¡ ‡ fdd„d�}|d }t | j| ¡S )z¸
        Return a tuple of (g, p) for a Diffe-Hellman process, with p being as
        close to bits bits as possible.

        @type bits: L{int}
        @rtype:     L{tuple}
        c                    s   t | ˆ  ƒS )N)Úabs)Úi©Úbitsr   r   Ú<lambda>l   s    z'SSHFactory.getDHPrime.<locals>.<lambda>)Úkeyr   )Úsortedr   r   ÚrandomÚchoice)r   r-   Ú
primesKeysÚrealBitsr   r,   r   Ú
getDHPrimed   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
   ÚavatarN)r   Úservices)r   r   Úservicer   r   r   Ú
getServicep   s   
ÿzSSHFactory.getServiceN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r   ÚSSHServerTransportr   r   ÚSSHUserAuthServerr   ÚSSHConnectionr7   r   r   r   r   r   r5   r9   r   r   r   r   r	      s    þ

	r	   )r=   r1   Útwisted.conchr   Útwisted.conch.sshr   r   r   r   Útwisted.internetr   Útwisted.loggerr   r   r	   r   r   r   r   Ú<module>   s   	