U
    
W[                     @   sn   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZmZmZmZ ddl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
    )divisionabsolute_import)protocol)log)error)_kex	transportuserauth
connectionNc                   @   s\   e Zd Zd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   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, failingprimesN)	hasattrgetPublicKeysr   getPrivateKeysr   r   Z
ConchError	getPrimesr   self r   ;/usr/lib/python3/dist-packages/twisted/conch/ssh/factory.pystartFactory!   s    





zSSHFactory.startFactoryc                 C   s@   t j| |}| j |_| js<t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   ZisFixedGroupZisEllipticCurve).0ZkexAlgorithmr   r   r   
<listcomp>>   s   

z,SSHFactory.buildProtocol.<locals>.<listcomp>)
r   FactorybuildProtocolr   keysZsupportedPublicKeysr   r   msgZsupportedKeyExchanges)r   Zaddrtr   r   r   r   /   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   E   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   P   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).

        @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>m       z'SSHFactory.getDHPrime.<locals>.<lambda>)keyr   )sortedr   r   randomZchoice)r   r%   Z
primesKeysZrealBitsr   r$   r   
getDHPrimee   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   servicer   r   r   
getServicer   s    zSSHFactory.getServiceN)__name__
__module____qualname____doc__r   ZSSHServerTransportr   r	   ZSSHUserAuthServerr
   ZSSHConnectionr,   r   r   r   r   r   r+   r.   r   r   r   r   r      s   
r   )r2   Z
__future__r   r   Ztwisted.internetr   Ztwisted.pythonr   Ztwisted.conchr   Ztwisted.conch.sshr   r   r	   r
   r*   r   r   r   r   r   r   <module>   s   